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
#include <iostream>
using namespace std;
long long t1[10000010];
long long t2[10000010];
long long t3[10000010];
long long i,j;
long long n,m,p;
void oblicz_sume(){
    for(i = 0 ; i <= m; i++){
        long long x = i * (i + 1) / 2;
        if(x >= p){
            x %= p;
        }
        t1[i] = x;
    }
}
void change(){
    for(j = 0 ; j <= m; j++){
        long long x = t1[j];
        t1[j] = t2[j];
        t2[j] = x;
        t3[j] = (t3[j-1] + x);
        if(t3[j] >= p)
            t3[j] %= p;
    }
    t1[j] = 0;
}
int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    cin >> p;
    oblicz_sume();
    for(i = 1 ; i < n; i++){
        change();
        for(j = 1; j <= m; j++){
            long long x = t2[m] - t2[m-j];
            if(x >= p)
                x %= p;
            else if(x < 0)
                x += p;
            x *= j;
            if(x >= p)
                x %= p;
            x += t1[j-1];
            x -= t3[j-1];
            if(x >= p)
                x %= p;
            else if(x < 0)
                x += p;
            t1[j] = x;
        }
    }
    if(t1[m] < 0)
        t1[m] += p;
    t1[m] %= p;
    cout << t1[m] << '\n';
}