#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  int64_t K;
  cin >> K;
  vector<vector<int64_t>> dp(4, vector<int64_t>(4 * N + 10, 0));
  dp[0][0] = 1;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3 * N + 1; j++) {
      dp[i + 1][j + 1] += dp[i][j];
      dp[i + 1][j + N + 1] -= dp[i][j];
    }
    for (int j = 0; j < 3 * N + 1; j++) {
      dp[i + 1][j + 1] += dp[i + 1][j];
    }
  }
  int x = 0;
  for (int i = 0; i < 3 * N + 1; i++) {
    if (K <= dp[3][i]) {
      x = i;
      break;
    }
    K -= dp[3][i];
  }
  for (int i = 1; i < N + 1; i++) {
    int mi = max(1, x - i - N);
    int ma = min(N, x - i - 1);
    if (mi > ma) continue;
    if (K > ma - mi + 1) {
      K -= ma - mi + 1;
      continue;
    }
    int j = mi + K - 1;
    int k = x - i - j;
    cout << i << ' ' << j << ' ' << k << '\n';
    return 0;
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7,INF=1e18;
#define ll long long
#define line cout<<"\n";
#define flush cout<<endl;
#define pll pair<ll,ll>
#define x first
#define y second
#define p_all(arr) for(auto i:arr){cout<<i<<" ";}cout<<"\n";
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    ll t=1;
    // cin>>t;
    for(ll test=1;test<=t;test++)
    {
        string s;
        ll k;
        cin>>s>>k;
        ll n=s.size();
        vector<ll> num(n);
        for(int i=0;i<n;i++)
        {
            if(s[i]<='9')
            {
                num[i]=s[i]-'0';
            }
            else
            {
                num[i]=10+s[i]-'A';
            }
        }
        ll dp[n+1][17][2];
        memset(dp,0,sizeof(dp));
        dp[0][1][1]=num[0]-1;
        for(int i=0;i<n;i++)
        dp[i][0][1]=1;
        set<ll> set1;
        set1.insert(num[0]);
        dp[0][1][0]=1;
        for(int i=1;i<n;i++)
        {
            ll c1=0,c2=num[i];
            for(auto v:set1)
            {
                if(v<num[i])
                c1++;
            }
            c2-=c1;
            set1.insert(num[i]);
            dp[i][set1.size()][0]=1;
            for(ll l=0;l<=16;l++)
            {
                dp[i][l][1]=(dp[i][l][1]+l*dp[i-1][l][1])%mod;
                if(l<16)
                {
                    if(l==0)
                    dp[i][l+1][1]=(dp[i][l+1][1]+(15LL-l)*dp[i-1][l][1])%mod;
                    else
                    dp[i][l+1][1]=(dp[i][l+1][1]+(16LL-l)*dp[i-1][l][1])%mod;
                }
                dp[i][l][1]=(dp[i][l][1]+c1*dp[i-1][l][0])%mod;
                if(l<16)
                dp[i][l+1][1]=(dp[i][l+1][1]+c2*dp[i-1][l][0])%mod;
            }
        }
        cout<<(dp[n-1][k][0]+dp[n-1][k][1])%mod<<"\n";
    }
    return 0;
}