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
#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long ll;

struct Node {
    ll val;
    Node *next;
    Node(ll v) : val(v) {
        next = NULL;
    }
};

ll counterList(vector<ll> &v, ll k) {
    if(v[0] > v[v.size() - 1]) return 0;
    Node *root = NULL;
    for(int i = v.size() - 1; i >= 0; i--) {
        Node *pom = new Node(v[i]);
        pom->next = root;
        root = pom;
    }
    Node *pom = new Node(0);
    pom->next = root;
    root = pom;

    ll del = 0, c = 0;
    while(del + 1 < v.size()) {
        Node *prev = root, *current = root->next;
        ll pr = 0;
        while(current != NULL) {
            if(current->val < pr || (current->next != NULL && current->val < current->next->val)) {
                prev->next = current->next;
                pr = current->val;
                Node *de = current;
                current = current->next;
                del++;
                delete de;
            } else {
                prev = current;
                pr = current->val;
                current = current->next;
            }
        }
        c++;
    }
    return c;
}

ll counter(vector<ll> v, ll k) {
    if(v[0] > v[v.size() - 1]) return 0;
    vector<ll> v2;
    ll c = 0;
    while(v.size() > 1) {
        for(int i = 0 ; i < v.size(); i++) {
            if(i == 0 && v[i] > v[i+1]){
                v2.pb(v[i]);
            }
            else if(i == v.size()-1 && v[i] > v[i-1]) {
                v2.pb(v[i]);
            }
            else if(i-1 >= 0 &&
                i+1 < v.size() &&
                v[i] > v[i-1] &&
                v[i] > v[i+1]) {
                v2.pb(v[i]);
            }
        }
        v = v2;
        v2.clear();
        c++;
        if(c > k) return 0;
    }
    return c;
}

int main() {
    ios_base::sync_with_stdio(0);
    vector<ll> v;
    ll n, k, mod;
    cin >> n >> k >> mod;
    for (int i = 0; i < n; i++) v.pb(i+1);
    ll c = 0;
    do {
        if(counterList(v, k) == k) c = (c + 1) % mod;
    } while (next_permutation(v.begin(), v.end()));
    cout << (2*c)%mod;
}