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

vector<int> s, s1, s2;
int sum;

void solve() {
	int n, m, M;
	cin >> n >> m >> M;
	s.resize(m + 1, 0);
	s1.resize(m + 1, 0);
	s2.resize(m + 1, 0);
	s[m] = 1;
	for (int i = 0; i < n; i++) {
		sum = s[m];
		for (int j = 1; j <= m; j++) {
			s1[j] = s[j] * j;
			s1[j] += s1[j - 1];
			s1[j] %= M;
		}
		for (int j = 1; j <= m; j++) {
			s2[j] = s[m - j] * j;
			s2[j] += s2[j - 1];
			s2[j] %= M;
		}
		for (int j = 1; j <= m; j++) {
			s[j] += s[j - 1];
			s[j] %= M;
		}
		for (int j = 1; j <= m; j++) {
			s[j] = j * (j + 1) / 2 % M * sum - j * s[j] + s1[j] - s2[j];
			s[j] %= M;
			s[j] += M;
			s[j] %= M;
		}
	}
	cout << s[m];
	return;
}


int32_t main() {
	int t = 1;
	//cin >> t;
	while(t--)
		solve();
	return 0;
}