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

using namespace std;

typedef long long ll;

int main()
{
    ios_base::sync_with_stdio(0);
    ll n, m, MOD;
    cin >> n >> m >> MOD;
    if (n == 1)
    {
        cout << (m * (m - 1) / 2 + m) % MOD;
        return 0;
    }
    vector< vector<ll> > dpD1(2, vector<ll>(m));
    vector< vector<ll> > dpD2(2, vector<ll>(m));
    vector< vector<ll> > dpD3(2, vector<ll>(m));
    vector< vector<ll> > dpG1(2, vector<ll>(m));
    vector< vector<ll> > dpG2(2, vector<ll>(m));
    vector< vector<ll> > dpG3(2, vector<ll>(m));
    for (int i = 0; i < m; ++i)
    {
        dpD1[0][i] = (i + 1) % MOD;
        dpG1[0][i] = (m - i) % MOD;
    }
    dpD2[0][0] = dpD1[0][0];
    for (int i = 1; i < m; ++i)
        dpD2[0][i] = (dpD2[0][i - 1] + dpD1[0][i]) % MOD;
    dpG2[0][m - 1] = dpG1[0][m - 1];
    for (int i = m - 2; i >= 0; --i)
        dpG2[0][i] = (dpG2[0][i + 1] + dpG1[0][i]) % MOD;
    dpD3[0][0] = dpD2[0][0];
    for (int i = 1; i < m; ++i)
        dpD3[0][i] = (dpD3[0][i - 1] + dpD2[0][i]) % MOD;
    dpG3[0][m - 1] = dpG2[0][m - 1];
    for (int i = m - 2; i >= 0; --i)
        dpG3[0][i] = (dpG3[0][i + 1] + dpG2[0][i]) % MOD;
    for (ll i = 1; i < n; ++i)
    {
        int akt = i % 2;
        int pop = akt ^ 1;
        for (ll j = 0; j < m; ++j)
        {
            ll W = dpD2[pop][m - 1];
            ll G = 0;
            if (j != m - 1)
                G = dpG2[pop][j + 1];
            ll D = 0;
            if (j != 0)
                D = dpD3[pop][j - 1];
            ll wyn = W * (j + 1) % MOD;
            wyn -= G * (j + 1) % MOD;
            if (wyn < 0)
                wyn += MOD;
            wyn -= D;
            if (wyn < 0)
                wyn += MOD;
            dpD1[akt][j] = wyn;
            D = 0;
            if (j != 0)
                D = dpD2[pop][j - 1];
            G = 0;
            if (j != m - 1)
                G = dpG3[pop][j + 1];
            wyn = W * (m - j) % MOD;
            wyn -= D * (m - j) % MOD;
            if (wyn < 0)
                wyn += MOD;
            wyn -= G;
            if (wyn < 0)
                wyn += MOD;
            dpG1[akt][j] = wyn;
        }
        dpD2[akt][0] = dpD1[akt][0];
        for (int j = 1; j < m; ++j)
            dpD2[akt][j] = (dpD2[akt][j - 1] + dpD1[akt][j]) % MOD;
        dpD3[akt][0] = dpD2[akt][0];
        for (int j = 1; j < m; ++j)
            dpD3[akt][j] = (dpD3[akt][j - 1] + dpD2[akt][j]) % MOD;
        dpG2[akt][m - 1] = dpG1[akt][m - 1];
        for (int j = m - 2; j >= 0; --j)
            dpG2[akt][j] = (dpG2[akt][j + 1] + dpG1[akt][j]) % MOD;
        dpG3[akt][m - 1] = dpG2[akt][m - 1];
        for (int j = m - 2; j >= 0; --j)
            dpG3[akt][j] = (dpG3[akt][j + 1] + dpG2[akt][j]) % MOD;
    }
    cout << dpD2[(n - 1) % 2][m - 1];
    return 0;
}