Problem p03234 (C++)

Buggy submission (status: Runtime Error):
// problem:agc028_d
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fst first
#define scd second

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline ll readll() {
  ll f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int MAXN = 305, MOD = 1e9 + 7;
inline int mod(int x) { return x < MOD ? (x < 0 ? x + MOD : x) : x - MOD; }
int n, K, p[MAXN], cnt[MAXN][MAXN], dp[MAXN][MAXN], g[MAXN];
int DP(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if (l == r)
    return dp[l][r] = 0;
  if (l + 1 == r)
    return dp[l][r] = (p[l] == r || (!p[l] && !p[r])) ? 1 : 0;

  if (cnt[l][r] == -1 || cnt[l][r] & 1)
    return dp[l][r] = 0;
  else
    dp[l][r] = g[cnt[l][r]];
  for (int i = l + 1; i < r; ++i) {
    if (cnt[i + 1][r] != -1)
      dp[l][r] = mod(dp[l][r] - (ll)DP(l, i) * g[cnt[i + 1][r]] % MOD);
  }
  return dp[l][r];
}
int main() {
  n = read() * 2;
  K = read();
  g[0] = 1;
  for (int i = 2; i <= n; i += 2)
    g[i] = (ll)g[i - 2] * (i - 1) % MOD;
  for (int i = 1, a, b; i <= K; ++i)
    a = read(), b = read(), p[a] = b, p[b] = a;
  for (int l = 1; l <= n; ++l) {
    for (int r = l; r <= n; ++r) {
      cnt[l][r] = r - l + 1;
      for (int i = l; i <= r; ++i) {
        if (!p[i])
          continue;
        if (p[i] < l || p[i] > r) {
          cnt[l][r] = -1;
          break;
        }
        cnt[l][r]--;
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  // int l,r;while(cin>>l>>r)cout<<DP(l,r)<<endl;
  int ans = 0;
  for (int l = 1; l <= n; ++l) {
    for (int r = l; r <= n; ++r) {
      int c = n - (r - l + 1), ok = 1;
      for (int i = 1; i < l; ++i) {
        if (!p[i])
          continue;
        if (p[i] >= l && p[i] <= r) {
          ok = 0;
          break;
        }
        c--;
      }
      if (!ok)
        continue;
      for (int i = r + 1; i <= n; ++i) {
        if (!p[i])
          continue;
        if (p[i] >= l && p[i] <= r) {
          ok = 0;
          break;
        }
        c--;
      }
      if (!ok)
        continue;
      ans = mod(ans + (ll)DP(l, r) * g[c] % MOD);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
// problem:agc028_d
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fst first
#define scd second

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline ll readll() {
  ll f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int MAXN = 605, MOD = 1e9 + 7;
inline int mod(int x) { return x < MOD ? (x < 0 ? x + MOD : x) : x - MOD; }
int n, K, p[MAXN], cnt[MAXN][MAXN], dp[MAXN][MAXN], g[MAXN];
int DP(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if (l == r)
    return dp[l][r] = 0;
  if (l + 1 == r)
    return dp[l][r] = (p[l] == r || (!p[l] && !p[r])) ? 1 : 0;

  if (cnt[l][r] == -1 || cnt[l][r] & 1)
    return dp[l][r] = 0;
  else
    dp[l][r] = g[cnt[l][r]];
  for (int i = l + 1; i < r; ++i) {
    if (cnt[i + 1][r] != -1)
      dp[l][r] = mod(dp[l][r] - (ll)DP(l, i) * g[cnt[i + 1][r]] % MOD);
  }
  return dp[l][r];
}
int main() {
  n = read() * 2;
  K = read();
  g[0] = 1;
  for (int i = 2; i <= n; i += 2)
    g[i] = (ll)g[i - 2] * (i - 1) % MOD;
  for (int i = 1, a, b; i <= K; ++i)
    a = read(), b = read(), p[a] = b, p[b] = a;
  for (int l = 1; l <= n; ++l) {
    for (int r = l; r <= n; ++r) {
      cnt[l][r] = r - l + 1;
      for (int i = l; i <= r; ++i) {
        if (!p[i])
          continue;
        if (p[i] < l || p[i] > r) {
          cnt[l][r] = -1;
          break;
        }
        cnt[l][r]--;
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  // int l,r;while(cin>>l>>r)cout<<DP(l,r)<<endl;
  int ans = 0;
  for (int l = 1; l <= n; ++l) {
    for (int r = l; r <= n; ++r) {
      int c = n - (r - l + 1), ok = 1;
      for (int i = 1; i < l; ++i) {
        if (!p[i])
          continue;
        if (p[i] >= l && p[i] <= r) {
          ok = 0;
          break;
        }
        c--;
      }
      if (!ok)
        continue;
      for (int i = r + 1; i <= n; ++i) {
        if (!p[i])
          continue;
        if (p[i] >= l && p[i] <= r) {
          ok = 0;
          break;
        }
        c--;
      }
      if (!ok)
        continue;
      ans = mod(ans + (ll)DP(l, r) * g[c] % MOD);
    }
  }
  cout << ans << endl;
  return 0;
}