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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double ld;

int distro_is_ok(vector<int> perm, int k) {
    for(int j=0; j<k; j++) {
        vector<int> tura;
        int n = perm.size();
        if(perm[0] > perm[1]) tura.push_back(perm[0]);
        for(int i=1; i < perm.size()-1; i++) {
            if(perm[i-1] < perm[i] && perm[i+1] < perm[i]) {
                tura.push_back(perm[i]);
            }
        }
        if(perm[n-1] > perm[n-2]) tura.push_back(perm[n-1]);
        if(tura.size() == 1 && j < k-1) return 0; 
        if(tura.size() == 1 && j == k-1) return 1;
        perm.clear();
        for(auto el : tura) {
            perm.push_back(el);
        }
    }
    return 0;
}

int main() {
    std::ios::sync_with_stdio(false);
    int n, m, t, k, p;
    cin >> n >> k >> p;
    LL result = 0;
    vector<int> perm(n);
    for(int i=1; i<=n; i++) {
        perm[i-1] = i;
    }
    do {
        if(distro_is_ok(perm, k) == 1) {
            result = (result + 1) % p;
        }
    } while(std::next_permutation(perm.begin(), perm.end()));
    cout << result << endl;  
    return 0;
}