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

using namespace std;
using ll = long long;
vector<int> v;
vector<int> perm;
vector<int> tmp;
const int INF = 1e3 + 5;

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    ll n, k, p;
    cin >> n >> k >> p;

    for (int i = 1; i <= n; i++) {
        v.emplace_back(i);
    }

    ll ctr = 0;

    do {
        int j = 0;

        perm = v;

        while (perm.size() > 1) {
            j++;
            for (int i = 0; i < perm.size(); i++) {
                if (i == 0) {
                    if (perm[i] < perm[i + 1]) {
                    } else {
                        tmp.emplace_back(perm[i]);
                    }
                } else if (i == perm.size() - 1) {
                    if (perm[i] < perm[i - 1]) {
                    } else {
                        tmp.emplace_back(perm[i]);
                    }
                } else {
                    if (perm[i] < perm[i - 1] or perm[i] < perm[i + 1]) {
                    } else {
                        tmp.emplace_back(perm[i]);
                    }
                }
            }

            swap(perm, tmp);
            tmp.clear();
        }
        if (j == k) {
            ctr = (ctr + 1) % p;
        }

    } while(next_permutation(v.begin(), v.end()));

    cout << ctr % p << "\n";

}