p489.ans2
======================
1

=================
p489.in2
======================
3 3

=================
p489.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, mod;

void read() { cin >> n >> mod; }

void solve() {
    // dp [pos] [dir][value] = dp[pos - 1][!dir][x]      for x in [0, value)
    //                                                   if dir == 0
    //                                                   for x in [value, pos]
    //                                                   if dir == 1

    if(n == 1) {
        cout << 1 % mod << '\n';
        return;
    }

    vector<vector<int>> last_dp(2), dp(2);
    last_dp[0] = {1 % mod};
    last_dp[1] = {1 % mod};

    for(int pos = 1; pos < n; pos++) {
        // dir = 1
        dp[1].resize(pos + 1);
        dp[1][pos] = 0; 
        for(int value = pos - 1; value >= 0; value--) {
            dp[1][value] = last_dp[0][value] + dp[1][value + 1];
            if(dp[1][value] >= mod) {
                dp[1][value] -= mod;
            }
        }
        // dir = 0
        dp[0].resize(pos + 1);
        dp[0][0] = 0;
        for(int value = 1; value <= pos; value++) {
            dp[0][value] = last_dp[1][value - 1] + dp[0][value - 1];
            if(dp[0][value] >= mod) {
                dp[0][value] -= mod;
            }
        }
        swap(last_dp, dp);
    }

    int ans = 0;
    for(int value = 0; value <= n - 1; value++) {
        ans = (ans + last_dp[0][value]) % mod;
        ans = (ans + last_dp[1][value]) % mod;
    }

    cout << ans << '\n'; 
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p489.ans1
======================
4

=================
p489.in1
======================
3 10

=================
statement.txt
======================
489. Extremal Permutations
Time limit per test: 0.5 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



A member ai of the sequence a1, a2, ·s, an is called a  if either ai > ai-1 and ai > ai+1 (local maximum) or ai < ai-1 and ai < ai+1 (local minimum). A sequence p1, p2, ·s, pn is called a  of the integers from 1 to n if each of the integers appears in the sequence exactly once. A permutation is called  if each member (except the first and the last) is a local extreme.

Compute the total number of extremal permutations of the integers from 1 to n and output the result modulo m.

Input
The first and only line of the input file contains the integers n () and m (1 ≤ m ≤ 109).

Output
The output file should contain a single integer, the remainder from division of the total number of extremal permutations of integers from 1 to n by the given integer m.

Example(s)
sample input
sample output
3 10
4

sample input
sample output
3 3
1



Note. The extremal permutations of 1·s3 are (1, 3, 2), (2, 1, 3), (2, 3, 1) and (3, 1, 2).

=================
