This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define int long long
#define bug(x) cerr<<#x<<" = "<<x<<'\n'
using namespace std;
const int mod = 1e9 + 7;
const int N = 2001;
void add(int& a, int b) {
    a += b;
    while (a >= b) a -= mod;
    while (a < 0)  a += mod;
}
void mul(int& a, int b) {
    a = 1ll * a * b % mod;
}
int st, en, n;
int dp[N][N][4];
/// 0 - 00; 1 - 10, 2 - 01, 3 - 11;
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("kangaroo.in", "r", stdin);
//    freopen("kangaroo.out", "w", stdout);
    cin>>n>>st>>en;
    dp[0][2][0] = 1;
    int cur = 0;
    if (st > en) swap(st, en);
    for (int i = 0; i < n - 2; ++i) {
        ++cur;
        if (cur == st) ++cur;
        if (cur == en) ++cur;
        for (int j = 1; j <= i + 2; ++j) {
            for (int k = 0; k < 4; ++k) {
                if (dp[i][j][k] == 0) continue;
                if (i == n - 3 && j == 2 && k == 3)
                    add(dp[n - 2][1][k], dp[i][j][k]);
                for (int t = 0; t < 2; ++t) if ((k >> t & 1) == 0){
                    int m = (t == 0 ? st : en), d = __builtin_popcount(k);
                    if (i < n - 3) {
                        if (cur > m && j > 2)
                            add(dp[i + 1][j - 1][k | (1 << t)], 1ll * dp[i][j][k] * (j - 2) % mod);
                        else if (cur < m)
                            add(dp[i + 1][j][k | (1 << t)], dp[i][j][k]);
                    } else if (j == 2 && cur > m && d == 1) {
                            add(dp[i + 1][1][3], dp[i][j][k]);
                    }
                } else if (i < n - 3) {
                    add(dp[i + 1][j - 1][k], 1ll * dp[i][j][k] * (j - 2) % mod);
                }
                add(dp[i + 1][j + 1][k], dp[i][j][k]);
                if (j > 3) add(dp[i + 1][j - 1][k], 1ll * (dp[i][j][k] * (j - 2)) % mod * (j - 3) % mod);
            }
        }
    }
    cout << dp[n - 2][1][3];
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |