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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define PB push_back
#define mp make_pair
#define MP make_pair
#define pbp(a, b) pb(mp(a, b))
#define PBP(a, b) PB(MP(a, b))
#define fst first
#define snd second
#define int4 int32_t
typedef long long LL;
#define int LL
#define FOR(i,a,b) for(int(i)=(a)-(int)((a)>(b));(i)!=(b)-(int)((a)>(b));(i)+=((a)<(b))?1:-1)
#define REP(b) FOR(i, 0, b)
#define REPI(i, b) FOR(i, 0, b)
#define REP1(b) FOR(i, 1, (b)+1)
#define REPI1(i, b) FOR(i, 1, (b)+1)
#define REV(b) FOR(i, b, 0)
#define REV1(b) FOR(i, (b)+1, 1)
#define ITER(j, b) for(auto (j) : (b))
#define ALL(x) (x).begin(), (x).end()
#define BOTH(x) (x).fst, (x).snd
#define sz(x) ((int)(x).size())
#define SZ(x) ((int)(x).size())
#define SKP(...) (__VA_ARGS__)
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef long double LD;

template<class C> void minu(C& a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxu(C& a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h)
{
    while(*sdbg==' ') sdbg++;
    if(*sdbg!='\"' && *sdbg!='\'' && (*sdbg!='S' || *(sdbg+1)!='K' || *(sdbg+2)!='P'))cerr<<" "<<sdbg<<"="<<h<<"\n";
    else cerr<<" "<<h<<"\n";
}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a)
{
    
    while(*sdbg==' ') sdbg++;
    if(*sdbg!='\"' && *sdbg!='\'' && (*sdbg!='S' || *(sdbg+1)!='K' || *(sdbg+2)!='P'))
    {
        cerr<<" ";
        while(*sdbg!=',')cerr<<*sdbg++;
        cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
    }
    else
    {
        while(*sdbg!=',') sdbg++;
        cerr<<" "<<h<<","; _dbg(sdbg+1, a...);
    }
}

template<class T> ostream &operator<<(ostream &os, vector<T> V)
{
    os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]";
} 

template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P)
{
    return os << "(" << P.fst << "," << P.snd << ")";
}

int XXXX_XXXX_XXXX;

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define deb(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define scanf(...) (XXXX_XXXX_XXXX=scanf(__VA_ARGS__))
#define prerr(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...) {}
#define cerr if(0)cout
#endif

int mod;

const int maxn = 1005;

int oba[maxn][20];
int obapre[maxn][20];
int jed[maxn][20];
int jedpre[maxn][20];
int bin[maxn][maxn];

void dod(int &a, int b)
{
    a=(a+b)%mod;
}

int pot(int pod, int wyk)
{
    int ret=1;
    while(wyk>0)
    {
        if(wyk&1) ret=(ret*pod)%mod;
        pod=(pod*pod)%mod;
        wyk>>=1;
    }
    return ret;
}

int odw(int pod)
{
    return pot(pod, mod-2);
}

int czas(vector <int> a)
{
    int ret=0;
    while(!a.empty())
    {
        vector <int> tmp;
        if(sz(a)>=2 && a[0]>a[1]) tmp.push_back(a[0]);
        for(int i=1; i<sz(a)-1; i++)
        {
            if(a[i-1]<a[i] && a[i+1]<a[i]) tmp.push_back(a[i]);
        }
        ret++;
        a=tmp;
    }
    return ret;
}

int32_t main()
{
    int n, k;
    mod = 1000000007;
    scanf("%lld%lld%lld", &n, &k, &mod);
    for(int i=0; i<=n; i++)
    {
        bin[i][0]=1;
        for(int j=1; j<=i; j++)
        {
            bin[i][j]=(((bin[i][j-1]*(i+1-j))%mod)*odw(j))%mod;
        }
    }
    if(k>11)
    {
        printf("0\n");
        exit(0);
    }
    oba[0][0]=1;
    obapre[0][0]=1;
    for(int j=1; j<=k; j++)
    {
        for(int i=1; i<=n; i++)
        {
            for(int poz=0; poz<i; poz++)
            {
                int dl1=poz;
                int dl2=i-1-poz;
                int sum=0;
                dod(sum, oba[dl1][j-1]    * oba[dl2][j-1]);
                dod(sum, obapre[dl1][j-1] * oba[dl2][j]);
                dod(sum, oba[dl1][j]      * obapre[dl2][j-1]);
                dod(oba[i][j], sum*bin[i-1][dl1]);
            }
            dod(obapre[i][j], obapre[i][j-1]+oba[i][j]);
        }
        obapre[0][j]=obapre[0][j-1]+oba[0][j];
    }
    
    jed[0][0]=1;
    jedpre[0][0]=1;
    for(int j=1; j<=k; j++)
    {
        for(int i=1; i<=n; i++)
        {
            for(int poz=0; poz<i; poz++)
            {
                int dl1=poz;
                int dl2=i-1-poz;
                int sum=0;
                dod(sum, jedpre[dl1][j-1] * oba[dl2][j-1]);
                dod(sum, jed[dl1][j]      * obapre[dl2][j-1]);
                dod(jed[i][j], sum*bin[i-1][dl1]);
            }
            dod(jedpre[i][j], jedpre[i][j-1]+jed[i][j]);
        }
        jedpre[0][j]=jedpre[0][j-1]+jed[0][j];
    }
    
    int wyn=0;
    for(int poz=0; poz<n; poz++)
    {
        int dl1=poz;
        int dl2=n-1-poz;
        int sum=0;
        dod(sum, jed[dl1][k] * jedpre[dl2][k-1]);
        dod(sum, jed[dl1][k] * jed[dl2][k]);
        dod(sum, jedpre[dl1][k-1] * jed[dl2][k]);
        dod(wyn, sum*bin[n-1][dl1]);
    }
    
    printf("%lld\n", wyn);
    
    /*
    prerr("%2d: ", -1);
    for(int j=0; j<k; j++)
    {
        prerr("%9lld ", j);
    }
    prerr("\n");
    for(int i=0; i<=n; i++)
    {
        prerr("%2lld: ", i);
        for(int j=0; j<=k; j++)
        {
            prerr("%9lld ", jed[i][j]);
            
            int wyn=0;
            vector <int> per;
            for(int ii=0; ii<i; ii++)
            {
                per.push_back(ii);
            }
            do
            {
                if(czas(per)==j) wyn++;
            }
            while(next_permutation(ALL(per)));
            prerr("%4lld ", wyn);
        }
        prerr("\n");
    }//*/
    
    /*
    prerr("%2d: ", -1);
    for(int j=0; j<k; j++)
    {
        prerr("%4lld ", j);
    }
    prerr("\n");
    for(int i=0; i<=n; i++)
    {
        prerr("%2lld: ", i);
        for(int j=0; j<=k; j++)
        {
            int wyn=0;
            vector <int> per;
            for(int ii=0; ii<i; ii++)
            {
                per.push_back(ii);
            }
            do
            {
                if(czas(per)==j) wyn++;
            }
            while(next_permutation(ALL(per)));
            prerr("%4lld ", wyn);
        }
        prerr("\n");
    }
    //*/
    
}