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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int p;

void dod(int& x, int y) {
  x += y;
	if (x >= p) x -= p;
}

int mn(long long a, int b) {
	return a * b % p;
}

int main() {
  int n,m;
	scanf("%d %d %d",&n,&m,&p);
	vector<int> d(m), dg(m, 1);
	vector<int> sd(m+1);
	for(int ii = 0; ii < n; ii++) {
		for(int i = m-1; i >= 0; i--) {
			sd[i] = sd[i+1];
			dod(sd[i], mn(d[i], m-i));
		}
		int pd = 0;
		for(int j = 0; j < m; j++) {
			dod(pd, dg[j]);
			dod(pd, mn(d[j], j));
			d[j] = mn(dg[j], m-j);
			dod(d[j], sd[j+1]);
			dg[j] = mn(pd, m-j);
			dod(dg[j], mn(sd[j+1], j+1));
//			printf("%d %d :: d = %d dg = %d\n", ii,j,d[j],dg[j]);
		}
	}
	int ret = 0;
	for (int j = 0; j < m; j++) {
		dod(ret, d[j]);
	}
	printf("%d\n",ret);
	return 0;
}