Problem p03191 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int maxn = 333, mod = 1000000007;
#define MP make_pair
#define PB push_back
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
inline ll read() {
  char ch = getchar();
  ll x = 0, f = 0;
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
int n, f[maxn * 2][maxn][maxn], cnt1, cnt2, ans;
bool vis[maxn], ind[maxn];
int main() {
  n = read();
  FOR(i, 1, n) {
    int x = read(), y = read();
    if (~x && ~y)
      vis[x] = vis[y] = true;
    else if (~x)
      ind[x] = true, cnt2++;
    else if (~y)
      ind[y] = true, cnt2++;
    else
      cnt1++;
  }
  f[2 * n + 1][0][0] = 1;
  ROF(i, 2 * n, 1) FOR(j, 0, cnt1 + cnt2) FOR(k, 0, cnt2) {
    if (vis[i])
      f[i][j][k] = f[i + 1][j][k];
    else if (ind[i]) {
      if (k)
        f[i][j][k] = (f[i][j][k] + f[i + 1][j][k - 1]) % mod;
      if (j != cnt1 + cnt2)
        f[i][j][k] = (f[i][j][k] + f[i + 1][j + 1][k]) % mod;
    } else {
      if (j)
        f[i][j][k] = (f[i][j][k] + f[i + 1][j - 1][k]) % mod;
      if (j != cnt1 + cnt2)
        f[i][j][k] = (f[i][j][k] + f[i + 1][j + 1][k]) % mod;
      if (k != cnt2)
        f[i][j][k] = (f[i][j][k] + 1ll * (k + 1) * f[i + 1][j][k + 1]) % mod;
    }
  }
  ans = f[1][0][0];
  FOR(i, 1, cnt1) ans = 1ll * ans * i % mod;
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int maxn = 333, mod = 1000000007;
#define MP make_pair
#define PB push_back
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
inline ll read() {
  char ch = getchar();
  ll x = 0, f = 0;
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
int n, f[maxn * 2][maxn][maxn], cnt1, cnt2, ans;
bool vis[maxn * 2], ind[maxn * 2];
int main() {
  n = read();
  FOR(i, 1, n) {
    int x = read(), y = read();
    if (~x && ~y)
      vis[x] = vis[y] = true;
    else if (~x)
      ind[x] = true, cnt2++;
    else if (~y)
      ind[y] = true, cnt2++;
    else
      cnt1++;
  }
  f[2 * n + 1][0][0] = 1;
  ROF(i, 2 * n, 1) FOR(j, 0, cnt1 + cnt2) FOR(k, 0, cnt2) {
    if (vis[i])
      f[i][j][k] = f[i + 1][j][k];
    else if (ind[i]) {
      if (k)
        f[i][j][k] = (f[i][j][k] + f[i + 1][j][k - 1]) % mod;
      if (j != cnt1 + cnt2)
        f[i][j][k] = (f[i][j][k] + f[i + 1][j + 1][k]) % mod;
    } else {
      if (j)
        f[i][j][k] = (f[i][j][k] + f[i + 1][j - 1][k]) % mod;
      if (j != cnt1 + cnt2)
        f[i][j][k] = (f[i][j][k] + f[i + 1][j + 1][k]) % mod;
      if (k != cnt2)
        f[i][j][k] = (f[i][j][k] + 1ll * (k + 1) * f[i + 1][j][k + 1]) % mod;
    }
  }
  ans = f[1][0][0];
  FOR(i, 1, cnt1) ans = 1ll * ans * i % mod;
  printf("%d\n", ans);
}