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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int MAX = 1010;

struct Problem {
    int N, K, P;
    vector<vector<int>> B, L, C;

    Problem(int N, int K, int P): N(N), K(min(11LL, K)), P(P) {
        B.resize(N + 1, vector<int>(N + 1));
        L.resize(N + 1, vector<int>(N + 1));
        C.resize(N + 1, vector<int>(N + 1));
        for (int i = 0; i <= N; ++i) {
            for (int j = 0; j <= N; ++j) {
                C[i][j] = 0;
            }
            for (int j = 0; j <= K; ++j) {
                L[i][j] = 0;
                B[i][j] = 0;
            }
        }
    }

    void prep() {
        C[0][0] = 1;
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j < i; ++j) {
                C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P;
            }
            C[i][0] = C[i][i] = 1;
        }  

        for (int k = 0; k <= K; ++k) {
            L[0][k] = B[0][k] = 1;
        }

        for (int n = 1; n <= N; ++n) {
            for (int k = 1; k <= K; ++k) {
                int l = 0;
                for (int i = 0; i < n; ++i) {
                    int t = B[i][k-1] * L[n - 1 - i][k] % P;
                    t = t * C[n-1][i] % P;

                    l = (l + t) % P;
                }
                L[n][k] = l;

                int b = 0;
                for (int i = 0; i < n; ++i) {
                    int t = B[i][k - 1] * B[n - 1 - i][k] % P;
                    t = (t + B[i][k] * B[n - 1 - i][k - 1]) % P;
                    t = (t - (B[i][k - 1] * B[n - 1 - i][k - 1] % P) + P) % P;

                    t = t * C[n-1][i] % P;

                    b = (b + t) % P;
                }
                B[n][k] = b;
            }
        }
    }

    int go() {
        int res = 0;
        for (int i = 0; i < N; ++i) {
            int t = L[i][K] * L[N - 1 - i][K] % P;
            t = t * C[N-1][i] % P;

            res = (res + t) % P;
        }

        for (int i = 0; i < N; ++i) {
            int t = L[i][K - 1] * L[N - 1 - i][K - 1] % P;
            t = t * C[N-1][i] % P;

            res = (res + P - t) % P;
        }

        return res;
    }
};

signed main() {
    ios_base::sync_with_stdio(false);

    int N, K, P;

    cin >> N >> K >> P;

    Problem p(N, K, P);
    p.prep();

    cout << p.go() << "\n";

/*
    Problem q(1000, 11, 100000000 + 7);
    q.prep();
    int res = 0;
    for (int i = 1; i <= 1000; ++i) {
        for (int j = 1; j <= 10; ++j) {
            q.N = i;
            q.K = j;
            res = (res + i * j * q.go()) % (100000000 + 7);
        }
    }
    cerr << res << "\n";
    */
}