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 fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 2e3 + 5, mod = 1e9 + 7;
int N, cs, cf, f[maxn][maxn];
void add(int & x, int y)
{
  x += y;
  if(x >= mod) x -= mod;
}
signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  cin >> N >> cs >> cf;
  if(cs > cf) swap(cs, cf);
  f[1][0] = 1;
  for(int i = 1; i <= N; ++i){
    for(int j = 0; j <= i; ++j){
      if(i == cs || i == cf){
        add(f[i + 1][j], f[i][j]);
        if(j > 0) add(f[i + 1][j - 1], 1ll * f[i][j] * j % mod);
      }
      else{
        if(i > cs && j > 0){
          add(f[i + 1][j - 1], 1ll * f[i][j] * j % mod);
        }
        if(i > cf && j > 0){
          add(f[i + 1][j - 1], 1ll * f[i][j] * j % mod);
        }
        add(f[i + 1][j + 1], f[i][j]);
        if(j > 1) add(f[i + 1][j - 1], 1ll * j * (j - 1) % mod * f[i][j] % mod);
      }
    }
  }
  cout << f[N][0];
}
| # | 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... |