Problem p03234 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
template <typename T> inline bool chkmin(T &x, T y) {
  return (y < x) ? (x = y, 1) : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return (y > x) ? (x = y, 1) : 0;
}
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 300 + 10, mod = 1e9 + 7;
int n, m, g[maxn], dp[maxn][maxn], a[maxn], b[maxn], sum[maxn], ans;
inline bool check(int l, int r) {
  REP(i, 1, m)
  if ((l <= a[i] && a[i] <= r) ^ (l <= b[i] && b[i] <= r)) return 0;
  return 1;
}
int main() {
  n = read() * 2, m = read();
  REP(i, 1, n) sum[i] = 1;
  REP(i, 1, m) {
    a[i] = read(), b[i] = read();
    if (a[i] > b[i])
      swap(a[i], b[i]);
    sum[a[i]] = sum[b[i]] = 0;
  }
  REP(i, 1, n) sum[i] += sum[i - 1];
  g[0] = 1;
  REP(i, 2, n) g[i] = (ll)g[i - 2] * (i - 1) % mod;
  for (int l = 2; l <= n; l += 2)
    REP(i, 1, n - l + 1) {
      int j = i + l - 1;
      if (!check(i, j))
        continue;
      dp[i][j] = g[sum[j] - sum[i - 1]];
      for (int k = i - 1; k < j; k += 2)
        dp[i][j] = (dp[i][j] - (ll)dp[i][k] * g[sum[j] - sum[k]]) % mod;
      ans = (ans + (ll)dp[i][j] * g[sum[n] - sum[j] + sum[i - 1]]) % mod;
    }
  ans = (ans + mod) % mod;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
template <typename T> inline bool chkmin(T &x, T y) {
  return (y < x) ? (x = y, 1) : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return (y > x) ? (x = y, 1) : 0;
}
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 600 + 10, mod = 1e9 + 7;
int n, m, g[maxn], dp[maxn][maxn], a[maxn], b[maxn], sum[maxn], ans;
inline bool check(int l, int r) {
  REP(i, 1, m)
  if ((l <= a[i] && a[i] <= r) ^ (l <= b[i] && b[i] <= r)) return 0;
  return 1;
}
int main() {
  n = read() * 2, m = read();
  REP(i, 1, n) sum[i] = 1;
  REP(i, 1, m) {
    a[i] = read(), b[i] = read();
    if (a[i] > b[i])
      swap(a[i], b[i]);
    sum[a[i]] = sum[b[i]] = 0;
  }
  REP(i, 1, n) sum[i] += sum[i - 1];
  g[0] = 1;
  REP(i, 2, n) g[i] = (ll)g[i - 2] * (i - 1) % mod;
  for (int l = 2; l <= n; l += 2)
    REP(i, 1, n - l + 1) {
      int j = i + l - 1;
      if (!check(i, j))
        continue;
      dp[i][j] = g[sum[j] - sum[i - 1]];
      for (int k = i - 1; k < j; k += 2)
        dp[i][j] = (dp[i][j] - (ll)dp[i][k] * g[sum[j] - sum[k]]) % mod;
      ans = (ans + (ll)dp[i][j] * g[sum[n] - sum[j] + sum[i - 1]]) % mod;
    }
  ans = (ans + mod) % mod;
  printf("%d\n", ans);
  return 0;
}