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
#include <iostream>
#include <vector>

using namespace std;
long long kon1[10000007];
long long kon2[10000007];
long long pref[10000007];
long long spref[10000007];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    long long n, m, p;
    cin >> n >> m >> p;
    for(int i = 0; i < m; i++){
        kon1[i] = (i+1)%p;
        kon2[i] = 0;
    }
    pref[0] = 0;
    spref[0] = 0;
    for(int i = 1; i <= m; i++){
        pref[i] = (pref[i-1]+kon1[i-1])%p;
    }
    for(int i = 1; i <= m; i++){
        spref[i] = (spref[i-1]+pref[i])%p;
    }
    for(int i = 1; i < n; i++){
        for(int j = 0; j < m; j++){
            kon2[j] = ((j+1)*(pref[m]-pref[m-j-1]) - spref[j])%p;
            //cout << kon2[j] << " ";
        }
        for(int j = 1; j <= m; j++){
            kon1[j-1] = kon2[j-1];
            pref[j] = (pref[j-1]+kon1[j-1])%p;
            spref[j] = (spref[j-1]+pref[j])%p;
        }
    }
    if(pref[m] < 0){
        pref[m] += p;
    }
    cout << pref[m];
    return 0;
}