#line 87 "ColorfulGardenHard.cpp"
#include <iostream>
#include <string>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <stack>
#include <map>
#include<unordered_map>
#include <set>
#include <cstring>
#include <iomanip>
#include <list>
#include <bitset>
//#include<bits/stdc++.h>
#define itn int
#define mp make_pair
#define endl "\n"
using namespace std;
typedef long long ll;
int mod = 1e9 + 7 ;
ll powmod( ll a,ll b) { ll res= 1 ; if ( a>= mod) a% = mod; for ( ; b; b>>= 1 ) { if ( b& 1 ) res= res* a; if ( res>= mod) res% = mod; a= a* a; if ( a>= mod) a% = mod; } return res; }
ll gcd( ll a , ll b) { return b== 0 ? a: gcd( b,a% b) ; }
string s1,s2;
ll dp[ 100010 ] [ 30 ] ,n;
ll func( ll mask,ll cur)
{
ll bits= ( ll) __builtin_popcount( mask) ;
if ( bits== n) return 1 ;
if ( dp[ mask] [ cur] ! = - 1 )
return dp[ mask] [ cur] ;
ll temp[ 30 ] ;
memset ( temp,0 ,sizeof ( temp) ) ;
ll i,j;
for ( j= 0 ; j< n; j++ )
{
if ( mask& ( 1 << j) )
continue ;
else temp[ s1[ j] - 'a' ] ++ ;
}
ll tempans= 0 ;
for ( i= 0 ; i< 26 ; i++ )
{
if ( temp[ i] > 0 && i! = cur && s2[ bits] - 'a' ! = i)
{
//aacout<<"i="<<i<<endl;
for ( j= 0 ; j< n; j++ )
{
if ( mask& ( 1 << j) )
{
//cout<<"j in mask="<<j<<endl;
continue ;
}
else
{
if ( s1[ j] - 'a' == i)
{
//cout<<"i="<<i<<endl;
tempans= ( tempans+ func( mask| ( 1 << j) ,i) ) % mod;
break ;
}
}
}
}
}
return dp[ mask] [ cur] = tempans;
}
class ColorfulGardenHard {
public :
int count( string garden, string forbid) {
s1= garden; s2= forbid;
ll i,j,m,ans= 0 ;
memset ( dp,- 1 ,sizeof ( dp) ) ;
ll temp[ 27 ] ;
memset ( temp,0 ,sizeof ( temp) ) ;
for ( i= 0 ; i< s1.length ( ) ; i++ )
{
temp[ s1[ i] - 'a' ] ++ ;
}
n= s1.length ( ) ;
for ( i= 0 ; i< 26 ; i++ )
{
if ( temp[ i] > 0 )
{
for ( j= 0 ; j< n; j++ )
{
if ( s1[ j] - 'a' == i && s2[ 0 ] - 'a' ! = i)
{
//cout<<"i="<<i<<endl;
ans= ( ans+ func( 1 << j,i) ) % mod;
//cout<<endl<<endl<<endl;
break ;
}
}
}
}
return ans;
}
} ;
// Powered by FileEdit
I2xpbmUgODcgIkNvbG9yZnVsR2FyZGVuSGFyZC5jcHAiCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGNsaW1pdHM+CiNpbmNsdWRlIDxjc3RkaW8+CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDx1dGlsaXR5PgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxzdGFjaz4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGU8dW5vcmRlcmVkX21hcD4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxpb21hbmlwPgojaW5jbHVkZSA8bGlzdD4KI2luY2x1ZGUgPGJpdHNldD4KLy8jaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgojZGVmaW5lIGl0biBpbnQKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBlbmRsICJcbiIKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBsb25nIGxvbmcgbGw7CmludCBtb2QgPSAxZTkgKyA3IDsKbGwgcG93bW9kKGxsIGEsbGwgYikge2xsIHJlcz0xO2lmKGE+PW1vZClhJT1tb2Q7Zm9yKDtiO2I+Pj0xKXtpZihiJjEpcmVzPXJlcyphO2lmKHJlcz49bW9kKXJlcyU9bW9kO2E9YSphO2lmKGE+PW1vZClhJT1tb2Q7fXJldHVybiByZXM7fQpsbCBnY2QobGwgYSAsIGxsIGIpe3JldHVybiBiPT0wP2E6Z2NkKGIsYSViKTsgfQpzdHJpbmcgczEsczI7CmxsIGRwWzEwMDAxMF1bMzBdLG47CgpsbCBmdW5jKGxsIG1hc2ssbGwgY3VyKQp7CiAgICBsbCBiaXRzPShsbClfX2J1aWx0aW5fcG9wY291bnQobWFzayk7CiAgICBpZihiaXRzPT1uKSByZXR1cm4gMTsKICAgIGlmKGRwW21hc2tdW2N1cl0hPS0xKQogICAgICAgIHJldHVybiBkcFttYXNrXVtjdXJdOwogICAgbGwgdGVtcFszMF07CiAgICAKICAgIG1lbXNldCh0ZW1wLDAsc2l6ZW9mKHRlbXApKTsKICAgIGxsIGksajsKICAgIGZvcihqPTA7ajxuO2orKykKICAgIHsKICAgICAgICBpZihtYXNrJigxPDxqKSkKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgZWxzZSB0ZW1wW3MxW2pdLSdhJ10rKzsKICAgIH0KICAgIAogICAgbGwgdGVtcGFucz0wOwogICAgZm9yKGk9MDtpPDI2O2krKykKICAgIHsKICAgICAgICBpZih0ZW1wW2ldPjAgJiYgaSE9Y3VyICYmIHMyW2JpdHNdLSdhJyE9aSkKICAgICAgICB7CiAgICAgICAgICAgIC8vYWFjb3V0PDwiaT0iPDxpPDxlbmRsOwogICAgICAgICAgICBmb3Ioaj0wO2o8bjtqKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmKG1hc2smKDE8PGopKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIC8vY291dDw8ImogaW4gbWFzaz0iPDxqPDxlbmRsOwogICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgIGlmKHMxW2pdLSdhJz09aSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIC8vY291dDw8Imk9Ijw8aTw8ZW5kbDsKICAgICAgICAgICAgICAgICAgICAgICAgdGVtcGFucz0odGVtcGFucytmdW5jKG1hc2t8KDE8PGopLGkpKSVtb2Q7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBkcFttYXNrXVtjdXJdPXRlbXBhbnM7Cn0KCmNsYXNzIENvbG9yZnVsR2FyZGVuSGFyZCB7CglwdWJsaWM6CglpbnQgY291bnQoc3RyaW5nIGdhcmRlbiwgc3RyaW5nIGZvcmJpZCkgewogICAgICAgIHMxPWdhcmRlbjsgczI9Zm9yYmlkOwogICAgICAgIGxsIGksaixtLGFucz0wOwogICAgICAgIG1lbXNldChkcCwtMSxzaXplb2YoZHApKTsKICAgICAgICBsbCB0ZW1wWzI3XTsKICAgICAgICBtZW1zZXQodGVtcCwwLHNpemVvZih0ZW1wKSk7CiAgICAgICAgZm9yKGk9MDtpPHMxLmxlbmd0aCgpO2krKykKICAgICAgICB7CiAgICAgICAgICAgIHRlbXBbczFbaV0tJ2EnXSsrOwogICAgICAgIH0KICAgICAgICBuPXMxLmxlbmd0aCgpOwogICAgICAgIGZvcihpPTA7aTwyNjtpKyspCiAgICAgICAgewogICAgICAgICAgICBpZih0ZW1wW2ldPjApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGZvcihqPTA7ajxuO2orKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZihzMVtqXS0nYSc9PWkgJiYgczJbMF0tJ2EnIT1pKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgLy9jb3V0PDwiaT0iPDxpPDxlbmRsOwogICAgICAgICAgICAgICAgICAgICAgICBhbnM9KGFucytmdW5jKDE8PGosaSkpJW1vZDsKICAgICAgICAgICAgICAgICAgICAgICAgLy9jb3V0PDxlbmRsPDxlbmRsPDxlbmRsOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIGFuczsKCgl9Cn07CgoKCgoKCgoKCgoKCgoKCi8vIFBvd2VyZWQgYnkgRmlsZUVkaXQK