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
#include <bits/stdc++.h>
using namespace std;

const int N = 1e7;

int n, m;
long long p;
vector<vector<long long> > d, u;


int main()
{
	scanf("%d%d%lld", &n, &m, &p);
	d.resize(n + 1);
	u.resize(n + 1);
	d[0].resize(m + 2);
	u[0].resize(m + 2);
	d[0][1] = u[0][m] = 1;
	for (int i = 1; i <= n; i++) {
		d[i].resize(m + 2);
		u[i].resize(m + 2);
		long long sumup = 0;
		for (int j = 1; j <= m; j++) {
			sumup = (sumup + d[i - 1][j]) % p;
		}
		long long all = sumup;
		long long sumdown = 0;
		long long pref = 0;
		for (long long j = 1; j <= m; j++) {
			sumup = (sumup - d[i - 1][j] + p) % p;
			sumdown = (sumdown + u[i - 1][j - 1]) % p;
			pref = (pref + sumdown) % p;
			u[i][j] = (j * sumup + pref) % p;
			u[i][j] = (j*all - u[i][j] + p) % p;
		}
		
		sumdown = 0;
		for (int j = 1; j <= m; j++) {
			sumdown = (sumdown + u[i - 1][j]) % p;
		}
		sumup = 0;
		pref = 0;
		for (long long j = m; j > 0; j--) {
			sumdown = (sumdown - u[i - 1][j] + p) % p;
			sumup = (sumup + d[i - 1][j + 1]) % p;
			pref = (pref + sumup) % p;
			d[i][j] = ((long long)(m - j + 1) * sumdown + pref) % p;
			d[i][j] = ((long long)(m - j + 1) * all - d[i][j] + p) % p;
		}
	}
	long long res = 0;
	for (int i = 1; i <= m; i++) {
		res = (res + d[n][i]) % p;
	}
	printf("%lld\n", res);
			
}