
// Problem : D - Multiset Mean
// Contest : AtCoder - AtCoder Regular Contest 104
// URL : https://atcoder.jp/contests/arc104/tasks/arc104_d
// Memory Limit : 1024 MB
// Time Limit : 4000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;

int N, K;
long long MOD;
const long long CUTOFF = 501000;

void add(long long &a, long long b){
	a += b;
	a -= (a >= MOD ? MOD : 0);
}

void sub(long long &a, long long b){
	a -= b;
	a += (a < 0 ? MOD : 0);
}

long long dp[2001005];
long long tmp[2001005];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> N >> K >> MOD;
	dp[CUTOFF] = K+1;
	for(int n = 1; n<N; n++){
		for(int r = 0; r<n; r++){
			long long tot = 0;
			for(int i = r+CUTOFF; i<=2*CUTOFF; i += n){
				if(i - n * (K+1) >= 0){
					sub(tot, tmp[i - n * (K+1)]);
				}
				tmp[i] = dp[i];
				add(tot, dp[i]);
				dp[i] = tot;
			}
		}
	}
	for(int n = 1; n<=N; n++){
		cout << (dp[CUTOFF] + MOD - 1)%MOD << "\n";
		/*
		for(int i = 0; i<=2*CUTOFF; i++){
			cout << "idx: " << (i-CUTOFF) << " " << dp[i] << "\n";
		}
		cout << "\n";		
		*/
		for(int r = 0; r<N-n; r++){
			long long tot = 0;
			for(int i = r; i <= 2*CUTOFF; i += N - n){
				if(i - (N - n) * (K+1) >= 0){
					sub(tot, dp[i-(N-n)*(K+1)]);
				}
				sub(dp[i], tot);
				add(tot, dp[i]);
			}
		}
		/*
		for(int i = 0; i<=2*CUTOFF; i++){
			cout << "idx: " << (i-CUTOFF) << " " << dp[i] << "\n";
		}
		cout << "\n";
		*/
		for(int r = 0; r<n; r++){
			long long tot = 0;
			for(int i = 2*CUTOFF/n*n+r; i>=0; i -= n){
				if(i + n * (K+1) <= 2 * CUTOFF){
					sub(tot, tmp[i + n * (K+1)]);
				}
				tmp[i] = dp[i];
				add(tot, dp[i]);
				dp[i] = tot;				
			}
		}
	}
}
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-30
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rrep(i,r,l) for(int i=r-1;i>=l;i--)
#define vi vector<int>
#define vvi vector<vector<int> >
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
typedef long long ll;
const int maxn=2e5+5;
char s[maxn];
int n,k,a[maxn];
ll d[maxn][17][2][2];
ll dfs(int cur,int st,int cnt,int up,int lead)
{
	if(cnt>k) return 0;
	if(cur>n) return cnt==k&&!lead;
	ll& ret=d[cur][cnt][up][lead];
	if(ret>=0) return ret;
	ret=0;
	int r=up?a[cur]:15;
	rep(i,0,r+1){
		ret=(ret+dfs(cur+1,(!i&&lead)?0:st|(1<<i),(!i&&lead)?cnt:cnt+!(1&(st>>i)),up&&(i==r),lead&&(!i)))%mod;
	}
	return ret;
}
int main()
{
	scanf("%s %d",s+1,&k);
	n=strlen(s+1);
	rep(i,1,n+1){
		if(s[i]>='A') a[i]=s[i]-'A'+10;
		else a[i]=s[i]-'0';
	}
	memset(d,-1,sizeof(d));
	printf("%lld\n",dfs(1,0,0,1,1));
	return 0;
}