#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
// auto mod int
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {if ((x += a.x) >= mod) x -= mod; return *this;}
  mint& operator-=(const mint a) {if ((x += mod-a.x) >= mod) x -= mod; return *this;}
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

const int N=200005,K=17;
mint dp[N][K];
bool u[N][K];
int n,k;
mint f(int i,int j){
  if(i==0) return j==k;
  if(j>k) return 0;
  if(u[i][j]) return dp[i][j];
  u[i][j] = true;
  return dp[i][j] = f(i-1,j)*j+f(i-1,j+1)*(16-j);
}
int main() {
  string s;
  cin >> s >> k;
  n = s.size();
  
  mint ans;
  set<int> t;
  rep(i,n){
    int a = [&]{
      auto c=s[i];
      if(isdigit(c)) return c-'0';
      return c-'A'+10;
    }();
    rep(ai,a){
      int ki = t.size()+1;
      if(t.count(ai)) --ki;
      if(ki==1&&ai==0) continue;
      ans += f(n-1-i,ki);
    }
    t.insert(a);
  }
  rep(i,n-1) ans += f(i,1)*(16-1);
  if(t.size()==k) ans += 1;
  cout << ans << endl;
}

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

ll p[20][200005];
ll dp[200005][20][2][2];
ll com[20][20];

ll sub(int r, int k, bool *b){
    int c = 0;
    for(int i = 0; i < 16; i++) c += b[i];
    if(c > k || r + c < k) return 0;
    ll res = 0;
    for(int i = 0; i <= k - c; i++){
        if(i % 2 == 0) res = (res + com[k - c][i] * p[k - i][r]) % MOD;
        else res = (res + MOD - com[k - c][i] * p[k - i][r] % MOD) % MOD;
    }
    return res * com[16 - c][k - c] % MOD;
}

int main()
{
    string n;
    int k;
    cin >> n >> k;
    reverse(n.begin(), n.end());
    int l = n.size();
    int a[200005];
    for(int i = 0; i < l; i++){
        if(n[i] >= '0' && n[i] <= '9') a[i] = n[i] - '0';
        else a[i] = n[i] - 'A' + 10;
    }
    a[0]++;
    for(int i = 1; i < l; i++){
        if(a[i - 1] == 16){
            a[i - 1] = 0;
            a[i]++;
        }
    }
    if(a[l - 1] == 16){
        a[l - 1] = 0;
        a[l++] = 1;
    }
    reverse(a, a + l);
    for(int j = 0; j <= k; j++){
        p[j][0] = 1;
        for(int i = 1; i < l; i++) p[j][i] = p[j][i - 1] * j % MOD;
    }
    for(int i = 0; i <= 16; i++){
        com[i][0] = 1;
        for(int j = 1; j <= i; j++) com[i][j] = (com[i - 1][j - 1] + com[i - 1][j]) % MOD;
    }
    dp[0][0][0][0] = 1;
    for(int i = 1; i < l; i++){
        for(int j = 1; j <= 16; j++){
            dp[i][j][0][0] = (dp[i - 1][j][0][0] * j + dp[i - 1][j - 1][0][0] * (16 - j)) % MOD;
            dp[i][j][1][0] = (dp[i - 1][j][1][0] * (j - 1) + dp[i - 1][j - 1][1][0] * (16 - j + 1) + 
                              dp[i - 1][j][1][1] * (j - 1) + dp[i - 1][j - 1][1][1] * (16 - j + 1)) % MOD;
            dp[i][j][1][1] = (dp[i - 1][j - 1][0][0] + dp[i - 1][j][1][0] + dp[i - 1][j][1][1]) % MOD;
        }
    }
    bool b[20]{0};
    ll ans = 0;
    for(int i = 0; i < l; i++){
        for(int j = 0; j < a[i]; j++){
            if(i == 0 && j == 0){
                for(int o = 0; o < l; o++) ans = (ans + dp[o][k][0][0] + dp[o][k][1][0]) % MOD;
                continue;
            }
            bool t = b[j];
            b[j] = true;
            ans = (ans + sub(l - 1 - i, k, b)) % MOD;
            b[j] = t;
        }
        b[a[i]] = true;
    }
    cout << ans << endl;
}
