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
#include <iostream>
#include <algorithm>
using namespace std;


int n, m;
long long p;


int main() {
    std::ios::sync_with_stdio(false);
    cin >> n >> m >> p;
    long long res1[m], res2[m];
    long long *prev = res1, *next = res2;
    for(int lvl = 0; lvl < m; ++lvl) {
        prev[lvl] = m - lvl;
    }
    long long sums[m], woos[m];
    for(int i = 1; i < n; ++i) {
        for(int lvl = 0; lvl < m; ++lvl) { // reset 
            next[lvl] = sums[lvl] = woos[lvl] = 0;
        }
        sums[0] = prev[0];
        woos[m - 1] = prev[m - 1];
        for(int lvl = 1; lvl < m; ++lvl) { // prefix sums and the other thing for speedup
            sums[lvl] = sums[lvl - 1] + prev[lvl];
            sums[lvl] %= p;
            woos[m - lvl - 1] = woos[m - lvl] + ((lvl + 1) * prev[m - lvl - 1]) % p;
            woos[m - lvl - 1] %= p;
        }
        for(int lvl = 0; lvl < m; ++lvl) { // main calculations
            if(lvl > 0) {
                next[lvl] = p + (sums[lvl - 1] - (sums[m - 1] - sums[m - lvl - 1])) * (m - lvl);
                next[lvl] %= p;
            }
            next[lvl] += woos[lvl];
            next[lvl] %= p;
        }
        long long *tmp = prev; // switch pointers
        prev = next;
        next = tmp;
    }
    long long total = 0;
    for(int lvl = 0; lvl < m; ++lvl) {
        total += prev[lvl];
        total %= p;
    }
    cout << total << endl;
}