#include <iostream>
#include<bits/stdc++.h>
#include <map>
#include <iterator>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long int ll;
ll mod = 1000000007;
ll hmaxii=100000000;
vector<ll>storage;
ll arr[5000][5000];
//ll answer[1000001];
ll upMax=0,downMin=0;
ll ans=0;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; }
ll power(ll a,ll b){
if(b==0){
return 1;
}
ll temp = power(a,b/2);
temp = (temp*temp)%mod;
if(b%2==1){
temp = (temp*a)%mod;
}
return temp%mod;
}
bool sorting(const pair<ll,ll>&a,const pair<ll,ll>&b){
if(a.first==b.first){
return (a.second>b.second);
}
else{
return (a.first<b.first);
}
}
void solution(){
string ss;ll p;
ll fact[21];fact[0]=1;
map<char,int>counter;
cin>>ss;cin>>p;
for(int i=1;i<21;i++){
fact[i] = (i*fact[i-1])%mod;
}
set<char>distinct;
for(int i=0;i<ss.length();i++){
distinct.insert(ss[i]);counter[ss[i]]++;
}
ll n = distinct.size();auto it = distinct.begin();
ll arr[n+1][ss.length()+1][p];//arr[0][0][0]=1;
memset(arr,0,sizeof(arr));arr[0][0][0]=1;
for(ll i=1;i<=n;i++){
ll x = counter[*it];
for(ll len = 0;len<=x;len++){
ll add = power(fact[len],mod-2);
ll ascii = int(*it);
for(ll j=len;j<=ss.length();j++){
for(ll k=0;k<p;k++){
arr[i][j][(k+ascii*len)%p] = (arr[i][j][(k+ascii*len)%p] + (arr[i-1][j-len][k]*(power(fact[len],mod-2)))%mod)%mod;
}
}
}
it++;
}
ll ans=0;
for(ll i=1;i<=ss.length();i++){
ll mul = fact[i];
//cout<<(arr[n][i][0]*(fact[i]))%mod<<endl;
ans = (ans + (arr[n][i][0]*mul)%mod)%mod;
}
cout<<ans<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//sieve();
//vector<ll>temp = getFactorization(24);
//cout<<temp.size()<<endl;
/*ll powe[64];
powe[0]=1;
ll i=1;
while(i<64){
powe[i] = powe[i-1]*2;
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wogICAgfQoKICAgIGxsIGFucz0wOwogICAgZm9yKGxsIGk9MTtpPD1zcy5sZW5ndGgoKTtpKyspewogICAgICAgIGxsIG11bCA9IGZhY3RbaV07CiAgICAgICAgLy9jb3V0PDwoYXJyW25dW2ldWzBdKihmYWN0W2ldKSklbW9kPDxlbmRsOwogICAgICAgIGFucyA9IChhbnMgKyAoYXJyW25dW2ldWzBdKm11bCklbW9kKSVtb2Q7CiAgICB9CiAgICBjb3V0PDxhbnM8PGVuZGw7Cn0KCgppbnQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShOVUxMKTsKICAgIC8vc2lldmUoKTsKICAgIC8vdmVjdG9yPGxsPnRlbXAgPSBnZXRGYWN0b3JpemF0aW9uKDI0KTsKICAgIC8vY291dDw8dGVtcC5zaXplKCk8PGVuZGw7CiAgICAvKmxsIHBvd2VbNjRdOwogICAgcG93ZVswXT0xOwogICAgbGwgaT0xOwogICAgd2hpbGUoaTw2NCl7CiAgICAgICAgcG93ZVtpXSA9IHBvd2VbaS0xXSoyOwogICAgICAgIGkrKzsKICAgIH0qLwogICAgbGwgdGltZXM7CiAgICAvL2Npbj4+dGltZXM7CiAgICB0aW1lcz0xOwogICAgZm9yKGxsIGk9MDtpPHRpbWVzO2krKyl7CiAgICAgICAgc29sdXRpb24oKTsKICAgIH0KICAgIHJldHVybiAwOwp9