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
///memento MODULO
///MEMENTO MEMO, MEMENTO LONG LONG
#include <bits/stdc++.h>

#define DEBUG if(0)
#define COUT cout << "\e[36m"
#define ENDL "\e[39m" << endl
#define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] "

using namespace std;
typedef long long LL;

int n, m, p;

vector<vector<vector<LL>>> suff;

LL getSuff(int i, int x, int y)
{
    return ((x>= 0 && y>= 0 && x < m && y < m)? suff[i%2][x][y] : 0ll);
}




int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m >> p;
    //fence.resize(n, vector<short int>(m));
    vector<vector<vector<LL>>> dp(2, vector<vector<LL>>(m, vector<LL>(m)));
    suff.resize(2, vector<vector<LL>>(m, vector<LL>(m)));
    for (int i = 0; i < m; ++i)
    {
        for (int j = i; j < m; ++j)
        {
            dp[0][i][j] = 1;
        }
    }

    for (int i = 1; i < n; ++i)
    {
        for (int x = 0; x < m; ++x)
        {
            for (int y = m-1; y >= 0; --y)
            {
                suff[i%2][x][y] = dp[(i+1)%2][x][y]+getSuff(i, x-1, y)
                    +getSuff(i, x, y+1)- getSuff(i, x-1, y+1) + p;
                suff[i%2][x][y] %= p;
            }
        }
        for (int x = 0; x < m; ++x)
        {
            for (int y = x; y < m; ++y)
            {
                dp[i%2][x][y] = suff[i%2][y][x];

            }
        }
        DEBUG for (int x = 0; x < m; ++x)
        {
            for (int y = 0; y < m; ++y)
            {
                COUT << suff[i%2][x][y] << " ";
            }
            COUT << ENDL;
        }
        DEBUG COUT << "DP:" << ENDL;
        DEBUG for (int x = 0; x < m; ++x)
        {
            for (int y = 0; y < m; ++y)
            {
                COUT << dp[i%2][x][y] << " ";
            }
            COUT << ENDL;
        }
        DEBUG COUT << ENDL << ENDL;
    }

    LL ans = 0;
    for (int i = 0; i < m; ++i)
    {
        for (int j = i; j < m; ++j)
        {
            ans += dp[(n+1)%2][i][j];
            ans %= p;
        }
    }
    cout << ans << "\n";
    return 0;
}