Problem p03234 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define reg register
#define pr std::pair<int, int>
#define fi first
#define se second
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
#define Rep(i, n) rep(i, 1, n)
#define Lep(i, n) lep(i, 1, n)
#define IRep(i, n) irep(i, n, 1)
#define ILep(i, n) ilep(i, n, 1)
typedef long long ll;
typedef long double ld;

namespace modular {
const int MOD = 1000000007;
inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }
inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }
inline int mul(int x, int y) { return 1LL * x * y % MOD; }
inline int qpow(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = mul(x, x))
    if (y & 1)
      ans = mul(ans, x);
  return ans;
}
}; // namespace modular
using namespace modular;

namespace Base {
template <typename Tp> inline Tp input() {
  Tp x = 0, y = 1;
  char c = getchar();
  while ((c < '0' || '9' < c) && c != EOF) {
    if (c == '-')
      y = -1;
    c = getchar();
  }
  if (c == EOF)
    return 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x *= y;
}
template <typename Tp> inline void read(Tp &x) { x = input<Tp>(); }
template <typename Tp> inline void chmax(Tp &x, Tp y) { x < y ? x = y : 0; }
template <typename Tp> inline void chmin(Tp &x, Tp y) { x > y ? x = y : 0; }
}; // namespace Base
using namespace Base;
/*----------------------------------------------------------------------------*/

#define MAX_N 607

int N, M;
int mc[MAX_N], g[MAX_N], f[MAX_N][MAX_N], su[MAX_N][MAX_N];

inline bool check(int l, int r) {
  if ((r ^ l ^ 1) & 1)
    return false;
  rep(i, l, r) if (mc[i] && (mc[i] < l || mc[i] > r)) return false;
  return true;
}

void solve() {
  read(N), read(M);
  N <<= 1;
  Rep(i, N) mc[i] = 0;
  int x, y;
  Rep(i, M) {
    read(x), read(y);
    mc[mc[y] = x] = y;
  }
  g[0] = 1;
  for (int i = 2; i <= N; i += 2)
    g[i] = mul(g[i - 2], i - 1);
  Rep(i, N) { rep(j, i, N) su[i][j] = su[i][j - 1] + (mc[j] ? 0 : 1); }
  int res = 0;
  Rep(i, N) {
    rep(j, i, N) if (check(i, j)) {
      int &fx = f[i][j];
      fx = g[su[i][j]];
      lep(k, i + 1, j) { inc(fx, MOD - mul(f[i][k], g[su[k + 1][j]])); }
      inc(res, mul(fx, g[su[1][i - 1] + su[j + 1][N]]));
    }
  }
  Rep(i, N) rep(j, i, N) f[i][j] = 0;
  printf("%d\n", res);
}

int main() {
  int T = input<int>();
  while (T--)
    solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define reg register
#define pr std::pair<int, int>
#define fi first
#define se second
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
#define Rep(i, n) rep(i, 1, n)
#define Lep(i, n) lep(i, 1, n)
#define IRep(i, n) irep(i, n, 1)
#define ILep(i, n) ilep(i, n, 1)
typedef long long ll;
typedef long double ld;

namespace modular {
const int MOD = 1000000007;
inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }
inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }
inline int mul(int x, int y) { return 1LL * x * y % MOD; }
inline int qpow(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = mul(x, x))
    if (y & 1)
      ans = mul(ans, x);
  return ans;
}
}; // namespace modular
using namespace modular;

namespace Base {
template <typename Tp> inline Tp input() {
  Tp x = 0, y = 1;
  char c = getchar();
  while ((c < '0' || '9' < c) && c != EOF) {
    if (c == '-')
      y = -1;
    c = getchar();
  }
  if (c == EOF)
    return 0;
  while ('0' <= c && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x *= y;
}
template <typename Tp> inline void read(Tp &x) { x = input<Tp>(); }
template <typename Tp> inline void chmax(Tp &x, Tp y) { x < y ? x = y : 0; }
template <typename Tp> inline void chmin(Tp &x, Tp y) { x > y ? x = y : 0; }
}; // namespace Base
using namespace Base;
/*----------------------------------------------------------------------------*/

#define MAX_N 607

int N, M;
int mc[MAX_N], g[MAX_N], f[MAX_N][MAX_N], su[MAX_N][MAX_N];

inline bool check(int l, int r) {
  if ((r ^ l ^ 1) & 1)
    return false;
  rep(i, l, r) if (mc[i] && (mc[i] < l || mc[i] > r)) return false;
  return true;
}

void solve() {
  read(N), read(M);
  N <<= 1;
  Rep(i, N) mc[i] = 0;
  int x, y;
  Rep(i, M) {
    read(x), read(y);
    mc[mc[y] = x] = y;
  }
  g[0] = 1;
  for (int i = 2; i <= N; i += 2)
    g[i] = mul(g[i - 2], i - 1);
  Rep(i, N) { rep(j, i, N) su[i][j] = su[i][j - 1] + (mc[j] ? 0 : 1); }
  int res = 0;
  Rep(i, N) {
    rep(j, i, N) if (check(i, j)) {
      int &fx = f[i][j];
      fx = g[su[i][j]];
      lep(k, i + 1, j) { inc(fx, MOD - mul(f[i][k], g[su[k + 1][j]])); }
      inc(res, mul(fx, g[su[1][i - 1] + su[j + 1][N]]));
    }
  }
  Rep(i, N) rep(j, i, N) f[i][j] = 0;
  printf("%d\n", res);
}

int main() {
  solve();
  return 0;
}