Problem p03234 (C++)

Buggy submission (status: Runtime Error):
// This amazing code is by Eric Sunli Chen.
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-' && t != EOF; t = getchar())
    ;
  if (t == '-')
    neg = true, t = getchar();
  if (t == EOF)
    return false;
  for (; t <= '9' && t >= '0'; t = getchar())
    x = x * 10 + t - '0';
  if (neg)
    x = -x;
  return true;
}
template <typename T> void print_int(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0)
    a[sz++] = x % 10, x /= 10;
  if (sz == 0)
    putchar('0');
  for (int i = sz - 1; i >= 0; i--)
    putchar('0' + a[i]);
}
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a, b) (get1(a) && get1(b))
#define get3(a, b, c) (get1(a) && get2(b, c))
#define printendl(a) print_int(a), puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int inf = 0x3f3f3f3f;
const LL Linf = 1ll << 61;
const double pi = acos(-1.0);

const int maxn = 311;
const int mod = 1e9 + 7;
int n, k, a[maxn], b[maxn], match[maxn], ff[maxn];

bool ok[maxn][maxn];
int dp[maxn][maxn], sum[maxn][maxn];

int main() {
  ff[0] = 1;
  for (int i = 2; i < maxn; i += 2)
    ff[i] = (LL)ff[i - 2] * (i - 1) % mod;

  get2(n, k);
  for (int i = 1; i <= k; i++) {
    get2(a[i], b[i]);
    match[a[i]] = b[i];
    match[b[i]] = a[i];
  }
  n *= 2;
  for (int l = 1; l <= n; l++)
    for (int r = l; r <= n; r++) {
      ok[l][r] = 1;
      for (int i = l; i <= r; i++)
        if (match[i] && (match[i] < l || match[i] > r))
          ok[l][r] = 0;
        else if (!match[i])
          sum[l][r]++;
      if (sum[l][r] % 2)
        ok[l][r] = 0;
    }
  int ans = 0;
  for (int l = n; l >= 1; l--)
    for (int r = l; r <= n; r++)
      if (ok[l][r]) {
        dp[l][r] = ff[sum[l][r]];
        for (int i = l; i < r; i++)
          dp[l][r] =
              (dp[l][r] + (LL)(mod - dp[l][i]) * ff[sum[i + 1][r]]) % mod;
        ans = (ans + (LL)dp[l][r] * ff[n - k - k - sum[l][r]]) % mod;
      }
  printendl(ans);
  return 0;
}

Fixed submission:
// This amazing code is by Eric Sunli Chen.
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-' && t != EOF; t = getchar())
    ;
  if (t == '-')
    neg = true, t = getchar();
  if (t == EOF)
    return false;
  for (; t <= '9' && t >= '0'; t = getchar())
    x = x * 10 + t - '0';
  if (neg)
    x = -x;
  return true;
}
template <typename T> void print_int(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0)
    a[sz++] = x % 10, x /= 10;
  if (sz == 0)
    putchar('0');
  for (int i = sz - 1; i >= 0; i--)
    putchar('0' + a[i]);
}
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a, b) (get1(a) && get1(b))
#define get3(a, b, c) (get1(a) && get2(b, c))
#define printendl(a) print_int(a), puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int inf = 0x3f3f3f3f;
const LL Linf = 1ll << 61;
const double pi = acos(-1.0);

const int maxn = 611;
const int mod = 1e9 + 7;
int n, k, a[maxn], b[maxn], match[maxn], ff[maxn];

bool ok[maxn][maxn];
int dp[maxn][maxn], sum[maxn][maxn];

int main() {
  ff[0] = 1;
  for (int i = 2; i < maxn; i += 2)
    ff[i] = (LL)ff[i - 2] * (i - 1) % mod;

  get2(n, k);
  for (int i = 1; i <= k; i++) {
    get2(a[i], b[i]);
    match[a[i]] = b[i];
    match[b[i]] = a[i];
  }
  n *= 2;
  for (int l = 1; l <= n; l++)
    for (int r = l; r <= n; r++) {
      ok[l][r] = 1;
      for (int i = l; i <= r; i++)
        if (match[i] && (match[i] < l || match[i] > r))
          ok[l][r] = 0;
        else if (!match[i])
          sum[l][r]++;
      if (sum[l][r] % 2)
        ok[l][r] = 0;
    }
  int ans = 0;
  for (int l = n; l >= 1; l--)
    for (int r = l; r <= n; r++)
      if (ok[l][r]) {
        dp[l][r] = ff[sum[l][r]];
        for (int i = l; i < r; i++)
          dp[l][r] =
              (dp[l][r] + (LL)(mod - dp[l][i]) * ff[sum[i + 1][r]]) % mod;
        ans = (ans + (LL)dp[l][r] * ff[n - k - k - sum[l][r]]) % mod;
      }
  printendl(ans);
  return 0;
}