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

using namespace std;

long long n, m, p;



int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(false);

	cin >> m >> n >> p;
	vector < vector < long long > >  DP_up;
	vector < vector < long long > >  DP_down;
	vector < long long >   DP_total;
	vector < vector < long long > > pref_up;
	vector < vector < long long > > pref_down;
	vector < vector < long long > > superpref_up;
	vector < vector < long long > > superpref_down;

	DP_up.resize(m);
	DP_down.resize(m);
	DP_total.resize(m);
	pref_up.resize(m);
	pref_down.resize(m);
	superpref_down.resize(m);
	superpref_up.resize(m);
	for(int a = 0; a < m; a++){
		DP_up[a].resize(n);
		DP_down[a].resize(n);
		pref_up[a].resize(n + 1);
		pref_down[a].resize(n + 1);
		superpref_up[a].resize(n + 1);
		superpref_down[a].resize(n + 1);
	}
	//cout << "A" << endl;

	for (int i = 0; i < m; ++i)
	{
		pref_up[i][0] = 0;
		pref_down[i][0] = 0;
		superpref_up[i][0] = 0;
		superpref_down[i][0] = 0;
		DP_total[i] = 0;
		for (int j = 0; j < n; ++j)
		{
			DP_up[i][j] = 0;
			DP_down[i][j] = 0;
			if(i == 0){

				DP_up[i][j] = (n - j) % p;
				superpref_up[i][j + 1] = (superpref_up[i][j] + DP_up[i][j] * (n - j)) % p;
				DP_down[i][j] = (j + 1) % p;
				superpref_down[i][j + 1] = (superpref_down[i][j] + DP_down[i][j] * (j + 1)) % p;
				DP_total[i] = (DP_total[i] + DP_up[i][j]) % p;
				pref_up[i][j + 1] = (pref_up[i][j] + DP_up[i][j]) % p;
				pref_down[i][j + 1] = (pref_down[i][j] + DP_down[i][j]) % p;
			}
		}
	}



	for (int i = 1; i < m; ++i)
	{
		pref_up[i][0] = 0;
		pref_down[i][0] = 0;
		for (int j = 0; j < n; ++j)
		{
			// Obliczam DP_up
			DP_up[i][j] = (superpref_up[i - 1][n] - superpref_up[i - 1][j]) % p;

			long long to_substract = 0;

			// Liczymy te, które kończą się na dole lub zaczynają na górze

			// najpierw te, które zaczynają się na górze lub równe
			to_substract = (to_substract + pref_up[i - 1][n] - pref_up[i - 1][j]) % p;
			
			// ... potem te, które kończą się na dole



			to_substract = (to_substract + pref_down[i - 1][j] - pref_down[i - 1][0]) % p;

			

			DP_up[i][j] = (DP_up[i][j] + ((DP_total[i - 1] - to_substract) * (n - j)) % p ) % p;
			superpref_up[i][j + 1] = (superpref_up[i][j] + DP_up[i][j] * (n - j)) % p;
			pref_up[i][j + 1] = (pref_up[i][j] + DP_up[i][j]) % p;
			DP_total[i] = (DP_total[i] + DP_up[i][j]) % p;


			// Następnie obliczam DP_down

			DP_down[i][j] = (superpref_down[i - 1][j + 1] - superpref_down[i - 1][0]) % p;


			//cout << "a: " << DP_down[i][j] << endl;

			to_substract = (pref_down[i - 1][j + 1] - pref_down[i - 1][0]) % p;

			// Liczymy te, które zaczynają się na dole lub kończą na górze


			// ... potem te, które zaczynają się na górze lub równe



			to_substract += (pref_up[i - 1][n] - pref_up[i - 1][j + 1]) % p;


			//cout << "c: " << to_substract << endl;

			DP_down[i][j] = (DP_down[i][j] + ((DP_total[i - 1] - to_substract) * (j + 1))) % p;
			superpref_down[i][j + 1] = (superpref_down[i][j] + DP_down[i][j] * (j + 1)) % p;
			pref_down[i][j + 1] = (pref_down[i][j] + DP_down[i][j]) % p;

			//cout << "DP_up[" << i << "][" << j << "] = " << DP_up[i][j] << endl;
			//cout << "DP_down[" << i << "][" << j << "] = " << DP_down[i][j] << endl;
			

		}
		//cout << "DP_total[" << i << "] = " << DP_total[i] << endl;
	}


	cout << (DP_total[m - 1] + p) % p << endl;
}