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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int prev_min_seg_size(int n) {
    switch (n) {
        case 2:
            return 1;
        case 3:
            return 2;
        case 5:
            return 3;
        case 9:
            return 5;
        case 17:
            return 9;
        case 33:
            return 17;
        case 65:
            return 33;
        case 129:
            return 65;
        case 257:
            return 129;
        case 513:
            return 257;
        default:
            return -1;
    }
}

int min_seg_size(int k) {
    switch (k) {
        case 1:
            return 2;
        case 2:
            return 3;
        case 3:
            return 5;
        case 4:
            return 9;
        case 5:
            return 17;
        case 6:
            return 33;
        case 7:
            return 65;
        case 8:
            return 129;
        case 9:
            return 257;
        case 10:
            return 513;
        default:
            return -1;
    }
}


long long int sil(int n, long long int mod) {
    long long int res = 1;
    for (int i = 2; i <= n; i++) {
        res *= (long long int) i;
        res %= mod;
    }
    return res;
}

long long int binom(int n, int k, long long int mod) {
    if (k > n) {
        return 0;
    }
    long long int res = 1;
    if (k > n - k) {
        for (int i = k + 1; i <= n; i++) {
            res *= (long long int) i;
            res %= mod;
        }
        if(sil(n-k, mod == 0)){
            return 0;
        }
        return res / sil(n - k, mod);
    }
    for (int i = n - k + 1; i <= n; i++) {
        res *= (long long int) i;
        res %= mod;
    }
    if(sil(k, mod )==0){
        return 0;
    }
    return res / sil(k, mod);
}

long long int res_for_min_seg(int n, long long int mod) {
    if (n == 2) {
        return 2;
    }
    int prev = prev_min_seg_size(n);
    //cout<<prev<<"\n";
    //return 0;
    long long int prev_binom = binom(prev - 3, (prev - 3) / 2, mod);
    //cout<<prev_binom;
    return (((((prev_binom * prev_binom) % mod) * binom(n - 3, (n - 3) / 2, mod)) % mod) * 2) % mod;
}

long long int solve(int n, int k, long long int mod, vector<long long int> pow_two) {
    int min_seg = min_seg_size(k);
    if(min_seg > n){
        return 0;
    }
    long long int res = 0;
    if (k == 1) {
        return pow_two[n - 1];
    }
    if (min_seg == n) {
        return res_for_min_seg(n, mod);
    }


    for (int seg_size = min_seg; seg_size <= n; seg_size++) {
        if(k == 2) {
           // res += 2 * (n - seg_size + 1) *binom(n-1, seg_size -1, mod) *
        }
        else{

        }
        for(int kb = k + 1; kb <= 10; kb++){
            res -= (((solve(n - seg_size, kb, mod, pow_two) * 2)%mod)*binom(n - 1, n-seg_size, mod))%mod;
        }
    }
}

int simulate(vector<int> vec, int k) {
    int counter = 0;
    vector<int> to_delete;
    while (vec.size() > 1) {
        counter++;
        if(counter > k){
            return  k+1;
        }
        for (int i = 0; i < vec.size(); i++) {
            if (i == 0) {
                if (vec[1] < vec[0]) {
                    to_delete.push_back(1);
                }
                continue;
            }
            if (i == vec.size() - 1) {
                if (vec[i] > vec[i - 1]) {
                    to_delete.push_back(i - 1);
                }
                continue;
            }
            if (vec[i] > vec[i - 1]) {
                to_delete.push_back(i - 1);
            }
            if (vec[i] > vec[i + 1]) {
                to_delete.push_back(i + 1);
            }
        }
        sort(to_delete.begin(), to_delete.end());
        auto it = unique(to_delete.begin(), to_delete.end());
        to_delete.resize(distance(to_delete.begin(), it));
        for (int i = 0; i < to_delete.size(); i++) {
            //cout<<"    "<<to_delete[i]<<"\n";
            vec.erase(vec.begin() + to_delete[i] - i);
        }
        to_delete.clear();

    }
    return counter;
}


int main() {
    ios_base::sync_with_stdio(0);
    int n, k;
    long long int res = 0;
    long long int mod;
    vector<long long int> pow_two;
    cin >> n >> k >> mod;
    pow_two.push_back(0);
    pow_two.push_back(1);
    for (int i = 2; i <= n; i++) {
        pow_two.push_back((pow_two[i - 1] * 2) % mod);
    }
    if (k > 10) {
        cout << 0;
        return 0;
    }
    if (n > 8) {
    int min_seg = min_seg_size(k);
    if (min_seg > n) {
        cout << 0;
        return 0;
    }
    if (k == 1) {
        cout << pow_two[n];
        return 0;
    }

    if (min_seg == n) {
      //  cout<<"sdsddsd";
        cout << res_for_min_seg(n, mod);
        return 0;
    }
}

    // cout << solve(n,k,mod,pow_two);
     vector<int> vec(n);
     //long long int res[20];
     // for(int i2=1;i2<12;i2++) {
     // vec.resize(i2);
     //for(int i=0;i<20;i++){
     //    res[i]=0;
     // }
     for (int i = 0; i < n; i++) {
         vec[i] = i + 1;
     }
     do {
         if(simulate(vec, k) == k){
             res++;
         }
         //res[simulate(vec)]++;
         /*if (simulate(vec) == k) {
             for (int i = 0; i < vec.size(); i++) {
                 cout << vec[i] << " ";
             }
             cout << "\n";
         }*/

     } while (next_permutation(vec.begin(), vec.end()));
     //cout<<i2<<":\n";
     //  for(int i=0;i<i2;i++){
     //    cout<<"\t"<<i<<": "<<res[i]<<"\n";
     //}
     // }
    cout<<res%mod;
    return 0;
}
/*
1:
	0: 1
2:
	0: 0
	1: 2
3:
	0: 0
	1: 4
	2: 2
4:
	0: 0
	1: 8
	2: 16
	3: 0
5:
	0: 0
	1: 16
	2: 100
	3: 4
	4: 0
6:
	0: 0
	1: 32
	2: 616
	3: 72
	4: 0
	5: 0
7:
	0: 0
	1: 64
	2: 4024
	3: 952
	4: 0
	5: 0
	6: 0
8:
	0: 0
	1: 128
	2: 28512
	3: 11680
	4: 0
	5: 0
	6: 0
	7: 0
9:
	0: 0
	1: 256
	2: 219664
	3: 142800
	4: 160
	5: 0
	6: 0
	7: 0
	8: 0
10:
	0: 0
	1: 512
	2: 1831712
	3: 1788896
	4: 7680
	5: 0
	6: 0
	7: 0
	8: 0
	9: 0
11:
	0: 0
	1: 1024
	2: 16429152
	3: 23252832
	4: 233792
	5: 0
	6: 0
	7: 0
	8: 0
	9: 0
	10: 0
 */
/*
4 1
1 2 3 4
1 2 4 3
1 3 4 2
1 4 3 2
2 3 4 1
2 4 3 1
3 4 2 1
4 3 2 1
 */