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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define FORE(i, t) for(__typeof(t.begin())i=t.begin();i!=t.end();++i)
#define SZ(x) int((x).size())
#define REP(i, n) for(int i=0,_=(n);i<_;++i)
#define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i)
#define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1e9 + 9;

const int MAX_N = 1003;
const int MAX_K = 11;

int F[MAX_N][MAX_K][3];
int G[MAX_N][MAX_K][3];
int BINOM[MAX_N][MAX_N];

int p;

int binom(int n, int k) {
//    cout << "binom: " << n << " " << k << "\n";
    if (k < 0 || n < 0) {
        return 0;
    }
    if (BINOM[n][k] != -1) {
        return BINOM[n][k];
    }
    if (k == 0 || k == n) {
        return BINOM[n][k] = 1;
    }
    if (k > n) {
        return BINOM[n][k] = 0;
    }
    return BINOM[n][k] = (binom(n - 1, k - 1) + binom(n - 1, k)) % p;
}

int f(int n, int k, int s);

int fsum(int n, int k, int s) {
    if (k < 0 || n < 0 || k >= MAX_K) {
        return 0;
    }
    if (G[n][k][s] != -1) {
        return G[n][k][s];
    }
    if (k == 0) {
        return G[n][k][s] = f(n, k, s);
    }
    return G[n][k][s] = (f(n, k, s) + fsum(n, k - 1, s)) % p;
}

int f(int n, int k, int s) {
    if (k < 0 || n < 0 || k >= MAX_K) {
        return 0;
    }
//    cout << "f: " << n << " " << k << " " << s << "\n";
    if (F[n][k][s] != -1) {
        return F[n][k][s];
    }

    if (s == 0) {
        if (n == 1 && k == 0) {
            return F[n][k][s] = 1;
        }
        if (n == 0 || k == 0 || k > n) {
            return F[n][k][s] = 0;
        }
    } else {
        if (n == 0 && k == 0) {
            return F[n][k][s] = 1;
        }
        if (n == 1 && k == 1) {
            return F[n][k][s] = 1;
        }
        if (n == 0 || k == 0 || k > n) {
            return F[n][k][s] = 0;
        }
    }

    int result = 0;
    REP (i, n) {
        int left = i;
        int right = n - i - 1;
        int b = binom(n - 1, left);

        int part = 0;
        if (s == 2) {
            part = (part + ((1LL * f(left, k - 1, 2) * f(right, k - 1, 2)) % p)) % p;
            part = (part + ((1LL * fsum(left, k - 1, 2) * f(right, k, 2)) % p)) % p;
            part = (part + ((1LL * f(left, k, 2) * fsum(right, k - 1, 2)) % p)) % p;
        } else if (s == 1) {
            part = (part + ((1LL * f(left, k - 1, 2) * fsum(right, k - 1, 1)) % p)) % p;
            part = (part + ((1LL * fsum(left, k - 1, 2) * f(right, k, 1)) % p)) % p;
        } else {
            part = (part + ((1LL * f(left, k, 1) * f(right, k, 1)) % p)) % p;
            part = (part + ((1LL * fsum(left, k - 1, 1) * f(right, k, 1)) % p)) % p;
            part = (part + ((1LL * f(left, k, 1) * fsum(right, k - 1, 1)) % p)) % p;
        }


        result = (result + ((1LL * b * part) % p)) % p;
    }
    return F[n][k][s] = result;
}


void inline one() {
    int n, k;
    cin >> n >> k >> p;
    REP (i, n + 1) {
        REP(j, n + 1) {
            BINOM[i][j] = -1;
        }
        REP(j, MAX_K) {
            REP(s, 3) {
                F[i][j][s] = -1;
                G[i][j][s] = -1;
            }
        }
    }

//    REP (i, n + 1) {
//        cout << " binom=" << i << "\n";
//        cout << "   ";
//        REP(j, n + 1) {
//            cout << binom(i, j) << " ";
//        }
//        cout << "\n";
//    }


//    REP(s, 3) {
//        cout << "=== s=" << s << " ===\n";
//        REP (i, n + 1) {
//            cout << " n=" << i << "\n";
//            cout << "   ";
//            REP(j, n + 1) {
//                cout << f(i, j, s) << " ";
//            }
//            cout << "\n";
//        }
//    }


    int result = f(n, k, 0);
    cout << result << "\n";

//    FOR(i, 1, 100) {
//        ll suma = 0;
//        FOR(j, 1, 11) {
//            suma = (suma + f(i, j, 0)) % p;
//        }
//        cout << i << " " << suma << "\n";
//    }

//    int checksum = 0;
//    FOR (i, 1, n) {
//        FOR(j, 1, k) {
//            int wynik = f(i, j, 0);
//            checksum = (checksum + (1LL * wynik * i * j) % p) % p;
//        }
//    }
//    cout << checksum << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    //int z; cin >> z; while(z--)
    one();
}