1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <cstdint>
#include <cstdlib>
using namespace std;


int64_t modulo;

int64_t binoms[1024][1024];

int64_t binomial(int64_t n, int64_t k){
	return binoms[n][k];
}

void fillBinomials(){
	for(int64_t i = 0; i < 1024; ++i){
		for(int64_t j = 0; j <= i; ++j){
			if(j == 0 || j == i){
				binoms[i][j] = 1;
			} else {
				binoms[i][j] = (binoms[i-1][j-1] + binoms[i-1][j]) % modulo;
			}
		}
	}
}

struct Values{
	int64_t BB;
	int64_t BE;
};

// poss[K][N]
// number of permutations of length N that reduce to 0 elements in K steps
// 		* with 2 reductor walls
//		* with 1 reductor wall
Values value[16][1024] = {0};

// Sum of values for all K's up to K for a given N
Values sumValue[16][1024] = {0};

void setStartingValues(){
	value[0][0].BB = 1;
	value[0][0].BE = 1;
	for(int64_t k = 0; k < 11; ++k){
		sumValue[k][0].BB = 1;
		sumValue[k][0].BE = 1;
	}
}

bool debug(int k, int n, int x = 0){
	return false;
	return k == 1 && n == 2;
}

void updateBBvalue(int64_t k, int64_t n){
	
	// n is the size of the NEW setup
	// so `highPos < n`, not `highPos <= n`
	for(int64_t highPos = 0; highPos < n; ++highPos){
		int64_t leftsize = highPos;
		int64_t rightsize = n - highPos - 1;
		
		if(debug(k, n)){
			cout << "K:           " << k << endl;
			cout << "N:           " << n << endl;
			cout << "LEFTSIZE:    " << leftsize << endl;
			cout << "RIGHTTSIZE:  " << leftsize << endl;
			cout << "VAL K  Left: " << value[k][leftsize].BB << endl;
			cout << "VAL K  Rite: " << value[k][rightsize].BB << endl;
			cout << "VAL K- Left: " << value[k-1][leftsize].BB << endl;
			cout << "VAL K- Rite: " << value[k-1][rightsize].BB << endl;
			cout << "SVL K- Left: " << sumValue[k-1][leftsize].BB << endl;
			cout << "SVL K- Rite: " << sumValue[k-1][rightsize].BB << endl;
		}
		
		int64_t rlonger = (sumValue[k-1][leftsize].BB * value[k][rightsize].BB) % modulo;
		int64_t llonger = (value[k][leftsize].BB * sumValue[k-1][rightsize].BB) % modulo;
		int64_t rlequal = (value[k-1][leftsize].BB * value[k-1][rightsize].BB) % modulo;
		
		if(debug(k, n)){
			cout << rlonger << endl;
			cout << llonger << endl;
			cout << rlequal << endl;
		}
		
		int64_t baseResult = (rlonger + llonger + rlequal) % modulo;
		value[k][n].BB += (binomial(n-1, leftsize) * baseResult) % modulo;
		value[k][n].BB %= modulo;
	}
	
	sumValue[k][n].BB = (sumValue[k-1][n].BB + value[k][n].BB) % modulo;
}


void updateBEvalue(int64_t k, int64_t n){
	
	for(int64_t highPos = 0; highPos < n; ++highPos){
		int64_t leftsize = highPos;
		int64_t rightsize = n - highPos - 1;
		
		int64_t rlonger = (sumValue[k-1][leftsize].BB * value[k][rightsize].BE) % modulo;
		int64_t llonger = 0;
		if(k >= 2) {
			llonger = (value[k-1][leftsize].BB * sumValue[k-2][rightsize].BE) % modulo;
		}
		int64_t rlequal = (value[k-1][leftsize].BB * value[k-1][rightsize].BE) % modulo;
		
		if(debug(k, n)){
			cout << rlonger << endl;
			cout << llonger << endl;
			cout << rlequal << endl;
		}
		
		int64_t baseResult = (rlonger + llonger + rlequal) % modulo;
		value[k][n].BE += (binomial(n-1, leftsize) * baseResult) % modulo;
		value[k][n].BE %= modulo;
	}
	
	sumValue[k][n].BE = (sumValue[k-1][n].BE + value[k][n].BE) % modulo;
}

int64_t EEvalue(int64_t k, int64_t n){
	
	int64_t EEvalue = 0;
	
	for(int64_t highPos = 0; highPos < n; ++highPos){
		int64_t leftsize = highPos;
		int64_t rightsize = n - highPos - 1;
		
		int64_t rlonger = (sumValue[k-1][leftsize].BE * value[k][rightsize].BE) % modulo;
		int64_t llonger = (value[k][leftsize].BE * sumValue[k-1][rightsize].BE) % modulo;
		int64_t rlequal = (value[k][leftsize].BE * value[k][rightsize].BE) % modulo;
		
		if(debug(k, n, 1)){
			cout << rlonger << endl;
			cout << llonger << endl;
			cout << rlequal << endl;
		}
		
		int64_t baseResult = (rlonger + llonger + rlequal) % modulo;
		EEvalue += (binomial(n-1, leftsize) * baseResult);
		EEvalue %= modulo;
	}
	
	return EEvalue;
}

int64_t kee, nee;

int32_t main(){
	cin >> nee >> kee >> modulo;
	
	if (kee > 15) {
		cout << 0 << endl;
		return 0;
	}
	
	fillBinomials();	
	setStartingValues();
	
	for(int64_t k = 1; k < 11; ++k){
		for(int64_t n = 1; n < 1001; ++n){
			updateBBvalue(k, n);
		}
	}
	
	for(int64_t k = 1; k < 11; ++k){
		for(int64_t n = 1; n < 1001; ++n){
			updateBEvalue(k, n);
		}
	}
	cout << EEvalue(kee, nee) << endl;
	
	return 0;
}