Problem p03191 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <typename T> inline T read() {
  T f = 0, x = 0;
  char c = getchar();
  while (!isdigit(c))
    f = c == '-', c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
  return f ? -x : x;
}
namespace run {
const int N = 309, mod = 1e9 + 7;
inline int add(int x, int y) { return x + y >= mod ? x - mod + y : x + y; }
inline int sub(int x, int y) { return x >= y ? x - y : x + mod - y; }
inline int qpow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1)
      ret = 1LL * x * ret % mod;
    x = 1LL * x * x % mod, y >>= 1;
  }
  return ret;
}

int vis[N], n, a[N], m, f[N][N][N];
int main() {
  n = read<int>() * 2;
  for (int i = 1; i <= n; i++) {
    a[i] = read<int>();
    if (~a[i])
      vis[a[i]] = 1;
    if (~i & 1 && ~a[i] && ~a[i - 1])
      vis[a[i]] = 2, vis[a[i - 1]] = 2;
  }

  for (int i = 1; i <= n; i++)
    if (vis[i] != 2)
      vis[++m] = vis[i];
  f[m + 1][0][0] = 1;
  for (int i = m; i >= 1; i--) {
    for (int j = 0; j <= m - i + 1; j++)
      for (int k = 0; k <= m - i + 1 - j; k++) {
        if (!vis[i]) {
          f[i][j][k] = add(f[i + 1][j + 1][k], f[i][j][k]);
          f[i][j][k] = (1LL * f[i + 1][j][k + 1] * (k + 1) + f[i][j][k]) % mod;
          f[i][j][k] = add(f[i + 1][j - 1][k], f[i][j][k]);
        } else {
          f[i][j][k] = add(f[i + 1][j + 1][k], f[i][j][k]);
          f[i][j][k] = add(f[i + 1][j][k - 1], f[i][j][k]);
        }
      }
  }
  int ans = f[1][0][0], sum = 0;
  for (int i = 1; i <= m; i++)
    if (vis[i])
      sum++;
  for (int i = 1; i <= m / 2 - sum; i++)
    ans = 1LL * i * ans % mod;
  printf("%d\n", ans);
  return 0;
}
} // namespace run
int main() {
#ifdef my
  freopen("minimum.in", "r", stdin);
  freopen("minimum.out", "w", stdout);
#endif
  return run::main();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <typename T> inline T read() {
  T f = 0, x = 0;
  char c = getchar();
  while (!isdigit(c))
    f = c == '-', c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
  return f ? -x : x;
}
namespace run {
const int N = 309, mod = 1e9 + 7;
inline int add(int x, int y) { return x + y >= mod ? x - mod + y : x + y; }
inline int sub(int x, int y) { return x >= y ? x - y : x + mod - y; }
inline int qpow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1)
      ret = 1LL * x * ret % mod;
    x = 1LL * x * x % mod, y >>= 1;
  }
  return ret;
}

int vis[N * 2], n, a[N * 2], m, f[N * 2][N * 2][N * 2];
int main() {
  n = read<int>() * 2;
  for (int i = 1; i <= n; i++) {
    a[i] = read<int>();
    if (~a[i])
      vis[a[i]] = 1;
    if (~i & 1 && ~a[i] && ~a[i - 1])
      vis[a[i]] = 2, vis[a[i - 1]] = 2;
  }

  for (int i = 1; i <= n; i++)
    if (vis[i] != 2)
      vis[++m] = vis[i];
  f[m + 1][0][0] = 1;
  for (int i = m; i >= 1; i--) {
    for (int j = 0; j <= m - i + 1; j++)
      for (int k = 0; k <= m - i + 1 - j; k++) {
        if (!vis[i]) {
          f[i][j][k] = add(f[i + 1][j + 1][k], f[i][j][k]);
          f[i][j][k] = (1LL * f[i + 1][j][k + 1] * (k + 1) + f[i][j][k]) % mod;
          f[i][j][k] = add(f[i + 1][j - 1][k], f[i][j][k]);
        } else {
          f[i][j][k] = add(f[i + 1][j + 1][k], f[i][j][k]);
          f[i][j][k] = add(f[i + 1][j][k - 1], f[i][j][k]);
        }
      }
  }
  int ans = f[1][0][0], sum = 0;
  for (int i = 1; i <= m; i++)
    if (vis[i])
      sum++;
  for (int i = 1; i <= m / 2 - sum; i++)
    ans = 1LL * i * ans % mod;
  printf("%d\n", ans);
  return 0;
}
} // namespace run
int main() {
#ifdef my
  freopen("minimum.in", "r", stdin);
  freopen("minimum.out", "w", stdout);
#endif
  return run::main();
}