Problem p03234 (C++)

Buggy submission (status: Time Limit Exceeded):
/********************************************************************************

        Code by a weak man who named CYJian, and he hopes the code can get more
 scores.

        Algorithm:

 ********************************************************************************/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

//{{{ FAST IO
const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge                                                                     \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),               \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)

template <typename T> inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = ge))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48), ch = ge;
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline ll read_ll() {
  ll x;
  return read(x), x;
}

template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; }
template <typename T> inline void chkmax(T &a, T b) { a = a > b ? a : b; }
//}}}

const int mod = 1e9 + 7;

inline int Mod(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) { x += y, x -= x >= mod ? mod : 0; }
inline int fsp(int x, int k = mod - 2) {
  int s = 1;
  while (k) {
    if (k & 1)
      s = 1LL * s * x % mod;
    x = 1LL * x * x % mod, k >>= 1;
  }
  return s;
}

int F[610];
int pw[610];
int fac[610];
int ifac[610];

int to[610];
int cnt[610];
int f[610][610];

int main() {
#ifndef ONLINE_JUDGE
  FILE("count");
#endif

  fac[0] = pw[0] = 1;
  for (int i = 1; i <= 600; i++)
    fac[i] = 1LL * fac[i - 1] * i % mod;
  ifac[600] = fsp(fac[600]);
  for (int i = 600; i >= 1; i--)
    ifac[i - 1] = 1LL * ifac[i] * i % mod;
  int iv = fsp(2);
  for (int i = 1; i <= 600; i++)
    pw[i] = 1LL * pw[i - 1] * iv % mod;
  for (int i = 0; i <= 600; i += 2)
    F[i] = 1LL * fac[i] * ifac[i >> 1] % mod * pw[i >> 1] % mod;

  int Case = 1;
  while (Case--) {
    int n = ri, m = ri;
    for (int i = 1; i <= m; i++) {
      int u = ri, v = ri;
      to[u] = v, to[v] = u;
    }
    n <<= 1;

    for (int i = 1; i <= n; i++)
      cnt[i] = cnt[i - 1] + (!to[i]);

    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j += 2) {
        int flag = 0;
        for (int k = i; k <= j && !flag; k++)
          flag |= to[k] && (to[k] < i || to[k] > j);
        if (flag)
          continue;
        int &G = f[i][j] = F[cnt[j] - cnt[i - 1]];
        for (int k = i + 1; k < j; k++)
          G = (G + 1LL * (mod - f[i][k]) * F[cnt[j] - cnt[k]]) % mod;
      }

    int res = 0;
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j += 2)
        res = (res + 1LL * F[cnt[n] - cnt[j] + cnt[i - 1]] * f[i][j]) % mod;
    cout << res << endl;

    for (int i = 1; i <= n; i++)
      to[i] = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        f[i][j] = 0;
  }
  return 0;
}

Fixed submission:
/********************************************************************************

        Code by a weak man who named CYJian, and he hopes the code can get more
 scores.

        Algorithm:

 ********************************************************************************/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

//{{{ FAST IO
const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge                                                                     \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),               \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)

template <typename T> inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = ge))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48), ch = ge;
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline ll read_ll() {
  ll x;
  return read(x), x;
}

template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; }
template <typename T> inline void chkmax(T &a, T b) { a = a > b ? a : b; }
//}}}

const int mod = 1e9 + 7;

inline int Mod(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) { x += y, x -= x >= mod ? mod : 0; }
inline int fsp(int x, int k = mod - 2) {
  int s = 1;
  while (k) {
    if (k & 1)
      s = 1LL * s * x % mod;
    x = 1LL * x * x % mod, k >>= 1;
  }
  return s;
}

int F[610];
int pw[610];
int fac[610];
int ifac[610];

int to[610];
int cnt[610];
int f[610][610];

int main() {
  fac[0] = pw[0] = 1;
  for (int i = 1; i <= 600; i++)
    fac[i] = 1LL * fac[i - 1] * i % mod;
  ifac[600] = fsp(fac[600]);
  for (int i = 600; i >= 1; i--)
    ifac[i - 1] = 1LL * ifac[i] * i % mod;
  int iv = fsp(2);
  for (int i = 1; i <= 600; i++)
    pw[i] = 1LL * pw[i - 1] * iv % mod;
  for (int i = 0; i <= 600; i += 2)
    F[i] = 1LL * fac[i] * ifac[i >> 1] % mod * pw[i >> 1] % mod;

  int Case = 1;
  while (Case--) {
    int n = ri, m = ri;
    for (int i = 1; i <= m; i++) {
      int u = ri, v = ri;
      to[u] = v, to[v] = u;
    }
    n <<= 1;

    for (int i = 1; i <= n; i++)
      cnt[i] = cnt[i - 1] + (!to[i]);

    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j += 2) {
        int flag = 0;
        for (int k = i; k <= j && !flag; k++)
          flag |= to[k] && (to[k] < i || to[k] > j);
        if (flag)
          continue;
        int &G = f[i][j] = F[cnt[j] - cnt[i - 1]];
        for (int k = i + 1; k < j; k++)
          G = (G + 1LL * (mod - f[i][k]) * F[cnt[j] - cnt[k]]) % mod;
      }

    int res = 0;
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j += 2)
        res = (res + 1LL * F[cnt[n] - cnt[j] + cnt[i - 1]] * f[i][j]) % mod;
    cout << res << endl;

    for (int i = 1; i <= n; i++)
      to[i] = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        f[i][j] = 0;
  }
  return 0;
}