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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <algorithm>
#include <cstdio>
#include <numeric>


const int MAX_N = 1009;
const int MAX_K = 1009;


long long f[MAX_N];
void gen_fact(const long long p)
{
    f[0] = 1LL;
    f[1] = 1LL;
    for(int i = 2; i < MAX_N; ++i)
        f[i] = (f[i-1] * i) % p;
}

long long fact(const long long n, const long long p)
{
    return f[n];
}


long long inv(const long long a, const long long p)
{
    long long q;

    long long u = 1LL;
    long long w = a;
    long long x = 0LL;
    long long z = p;

    while(w)
    {
        if(w < z)
        {
            q = u; u = x; x = q;
            q = w; w = z; z = q;
        }
        q = w / z;
        u -= q * x;
        w -= q * z;
    }

    if(x < 0)
        x += p;

    return x;
}


long long b[MAX_N][MAX_K];
void gen_binom(const long long p)
{
    b[0][0] = 1LL;
    b[1][0] = 1LL;
    b[1][1] = 1LL;
    for(int i = 2; i < MAX_N; ++i)
    {
        for(int j = 0; j < MAX_K; ++j)
        {
            if(j == 0)
                b[i][j] = 1LL;
            else
                b[i][j] = (b[i-1][j-1] + b[i-1][j]) % p;
        }
    }
}


long long binom(const long long n, long long k, const long long p)
{
    k = std::min(k, n - k);
    return b[n][k];
}


long long t1[MAX_N][MAX_K];
long long t1_sums[MAX_N][MAX_K];
void gen_t1(const long long p)
{
    t1[2][1] = 1LL;
    t1[3][2] = 1LL;

    for(int j = 1; j < std::min(20, MAX_K); ++j)
    {
        t1_sums[2][j] = (t1_sums[2][j-1] + t1[2][j]) % p;
        t1_sums[3][j] = (t1_sums[3][j-1] + t1[3][j]) % p;
    }

    for(int i = 4; i < MAX_N; ++i)
    {
        for(int j = 2; j < MAX_K; ++j)
        {
            if(j == 2)
            {
                t1[i][j] = (2LL * t1[i-1][j]) % p;
                continue;
            }

            for(int len = 2; len < i; ++len)
            {
                const long long p1 = (t1[len][j] * t1_sums[i-len+1][j-1]) % p;
                const long long p2 = (t1[i-len+1][j] * t1_sums[len][j-1]) % p;
                const long long p3 = (t1[len][j-1] * t1[i-len+1][j-1]) % p;

                const long long to_add = (p1+p2+p3) % p;

                t1[i][j] = (t1[i][j] + to_add * binom(i-3, len-2, p)) % p;
            }

            if(t1[i][j] == 0LL)
                break;
        }

        for(int j = 1; j < std::min(20, MAX_K); ++j)
            t1_sums[i][j] = (t1_sums[i][j-1] + t1[i][j]) % p;
    }
}


long long t2[MAX_N][MAX_K];
long long t2_sums[MAX_N][MAX_K];
void gen_t2(const long long p)
{
    t2[1][0] = 1LL;
    t2[2][1] = 1LL;

    for(int j = 1; j < std::min(20, MAX_K); ++j)
    {
        t2_sums[1][j] = (t2_sums[2][j-1] + t2[1][j]) % p;
        t2_sums[2][j] = (t2_sums[2][j-1] + t2[2][j]) % p;
    }

    for(int i = 3; i < MAX_N; ++i)
    {
        for(int j = 1; j < MAX_K; ++j)
        {
            if(j == 1)
            {
                t2[i][j] = 1LL;
                continue;
            }

            for(int len = 2; len < i; ++len)
            {
                const long long p1 = (t1[len][j] * t2_sums[i-len+1][j-1]) % p;
                const long long p2 = (t2[i-len+1][j] * t1_sums[len][j-1]) % p;
                const long long p3 = (t1[len][j] * t2[i-len+1][j]) % p;

                const long long to_add = (p1+p2+p3) % p;

                t2[i][j] = (t2[i][j] + to_add * binom(i-3, len-2, p)) % p;
            }

            if(t2[i][j] == 0LL)
                break;
        }

        for(int j = 1; j < std::min(20, MAX_K); ++j)
            t2_sums[i][j] = (t2_sums[i][j-1] + t2[i][j]) % p;
    }
}


long long t3[MAX_N][MAX_K];
void gen_t3(const long long p)
{
    t3[1][0] = 1LL;
    t3[2][1] = 2LL;

    for(int i = 3; i < MAX_N; ++i)
    {
        for(int j = 1; j < MAX_K; ++j)
        {
            if(j == 1)
            {
                t3[i][j] = (2LL * t3[i-1][j]) % p;
                continue;
            }

            for(int len = 1; len <= i; ++len)
            {
                const long long p1 = (t2[len][j] * t2_sums[i-len+1][j-1]) % p;
                const long long p2 = (t2_sums[len][j-1] * t2[i-len+1][j]) % p;
                const long long p3 = (t2[len][j] * t2[i-len+1][j]) % p;

                const long long to_add = (p1+p2+p3) % p;

                t3[i][j] = (t3[i][j] + to_add * binom(i-3, len-2, p)) % p;
            }

            if(t3[i][j] == 0LL)
                break;
        }
    }
}


int main()
{
    long long n, k, p;
    scanf("%lld%lld%lld", &n, &k, &p);

    gen_fact(p);
    gen_binom(p);

    gen_t1(p);
    gen_t2(p);
    gen_t3(p);

    for(int i = 3; i < (MAX_N-2); ++i)
    {
        for(int j = 2; j <= MAX_K; ++j)
            t3[i][j] = t3[i+2][j];
    }

    printf("%lld\n", t3[n][k]);
}