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

using namespace std;
typedef long long ll;
ll p;
const int N = 10 * 1000 * 1000 + 7;
ll bez[2][N], z[2][N], sp[N], sk[N];

void Malowanie()
{
	int i, j, n, m;
	ll s;
	cin >> m >> n >> p;
	for(i = 1; i <= n; ++i)
	{
		bez[0][i] = 1LL;
		sp[i] = 0LL;
		sk[i] = 0LL;
	}
	for(j = 1; j <= m; ++j)
	{
		s = 0LL;
		for(i = 1; i <= n; ++i)
		{
			bez[1][i] = (ll)(n - i + 1) * (sp[i - 1] + sk[i] + 1LL) % p;
			z[1][i] = ((ll)(n - i + 1) * (ll)(i - 1) % p) * (i - 1) * (sk[i]) % p;
			cout << j << " " << i << " " << bez[1][i] << " " << z[1][i] << "\n";
		}
		for(i = 1; i <= n; ++i)
			sp[i] = (((ll)i * (bez[0][i])) % p + sp[i - 1]) % p;
		for(i = n; i >= 0; --i)
		{
			sk[i] = (((n - i + 1) * (bez[0][i] + z[0][i]) % p) + sk[i + 1]) % p;
			bez[0][i] = bez[1][i];
			z[0][i] = z[1][i];
		}
	}
	s = 0LL;
	for(i = 1; i <= n; ++i)
	{
		s += z[0][i] + bez[0][i];
		s %= p;
	}
	cout << s << "\n";
}	


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	Malowanie();
	
	return 0;
}