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
#include <bits/stdc++.h>
using namespace std;
int n, k, mod, wynik;
vector<int> v;

inline bool ok()
{
    set<pair<int,int>> skw;
    int id = 1;
    for(auto i: v)
    {
        skw.insert({id,i});
        id++;
    }
    int ile = 0;
    while(skw.size() != 1)
    {
        if(ile >= k) return false;
        vector<set<pair<int,int>>::iterator> del;
        set<pair<int,int>>::iterator pop;
        for(set<pair<int,int>>::iterator i = skw.begin(); i != skw.end(); ++i)
        {
            bool deleteme = false;
            set<pair<int,int>>::iterator i2 = i;
            if(i != skw.begin() && (*pop).second > (*i).second) deleteme = true;
            i2 = i; ++i2;
            if(i2 != skw.end() && (*i2).second > (*i).second)  deleteme = true;
            if(deleteme) del.push_back(i);
            pop = i;
        }
        for(auto i: del)
        {
            skw.erase(i);
        }
        ile++;
    }
    if(ile == k) return true;
    else return false;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k>>mod;
    for(int i = 1; i <= n; i++)
    {
        v.push_back(i);
    }
    do
    {
        if(ok()) wynik++;
        wynik %= mod;
    }
    while(next_permutation(v.begin(), v.end()));
    cout<<wynik<<endl;
}