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
#include <bits/stdc++.h>
#define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define debug(x) cerr<<#x<<" "<<x<<endl
#define ll long long 
#define st first
#define nd second
using namespace std;

ll n, m, p, tab[10000007], all, x, y, res;
vector <ll> V;

ll szybkie_potegowanie(ll n, ll k) {
	if (k == 0LL) return 1;
	if (k % 2LL == 0LL) {
		ll s = szybkie_potegowanie(n, k / 2LL);
		return (s * s) % p;
	}
	else return (n * szybkie_potegowanie(n, k - 1LL)) % p;

}

int main()
{
	qio;
	cin >> n >> m >> p;
	all = (m * m + m) / 2LL;
	for (ll i = 1; i <= m; i++) {
		for (ll j = i; j <= m;j++) {
			x = (j - i) * (j - i + 1) / 2LL;
			y = (m - j) * (m - j + 1) / 2LL;
			V.push_back(all - x - y);
		}
	}

	for (ll i = 0; i < V.size(); i++) {
		res = (res + szybkie_potegowanie(V[i], n - 1)) % p;
	}

	cout << res << endl;
}