Problem p03191 (C++)

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

#define LL long long
#define pii pair<int, int>
using namespace std;
const int inf = 2147483647;
const int mod = 1e9 + 7;
const int N = 301;

int _max(int x, int y) { return x > y ? x : y; }
int _min(int x, int y) { return x < y ? x : y; }
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
void put(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    put(x / 10);
  putchar(x % 10 + '0');
}

int f[2][N][N], p[N * 2];

int add(int x, int y) {
  x += y;
  return x >= mod ? x - mod : x;
}

int main() {
  int n = read(), s = 0;
  for (int i = 1; i <= n; i++) {
    int x = read(), y = read();
    if (x != -1 && y != -1) {
      p[x] = p[y] = -1;
      continue;
    }
    if (x != -1)
      p[x] = 1;
    else if (y != -1)
      p[y] = 1;
    else
      s++;
  }
  int now = 0, s1 = 0, s2 = 0;
  f[0][0][0] = 1;
  for (int i = 2 * n; i >= 1; i--) {
    if (p[i] == -1)
      continue;
    now ^= 1;
    memset(f[now], 0, sizeof(f[now]));
    for (int j = 0; j <= s1; j++)
      for (int k = 0; k <= s2; k++)
        if (f[now ^ 1][j][k]) {
          int c = f[now ^ 1][j][k];
          if (p[i]) {
            if (j)
              f[now][j - 1][k] = add(f[now][j - 1][k], c);
            f[now][j][k + 1] = add(f[now][j][k + 1], c);
          } else {
            f[now][j + 1][k] = add(f[now][j + 1][k], c);
            if (j)
              f[now][j - 1][k] = add(f[now][j - 1][k], c);
            if (k)
              f[now][j][k - 1] = add(f[now][j][k - 1], (LL)c * k % mod);
          }
        }
    s1 += p[i] == 0, s2 += p[i] == 1;
  }
  int ans = f[now][0][0];
  for (int i = 1; i <= s; i++)
    ans = (LL)ans * i % mod;
  put(ans), puts("");
  return 0;
}


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

#define LL long long
#define pii pair<int, int>
using namespace std;
const int inf = 2147483647;
const int mod = 1e9 + 7;
const int N = 301;

int _max(int x, int y) { return x > y ? x : y; }
int _min(int x, int y) { return x < y ? x : y; }
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
void put(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    put(x / 10);
  putchar(x % 10 + '0');
}

int f[2][N][N], p[N * 2];

int add(int x, int y) {
  x += y;
  return x >= mod ? x - mod : x;
}

int main() {
  int n = read(), s = 0;
  for (int i = 1; i <= n; i++) {
    int x = read(), y = read();
    if (x != -1 && y != -1) {
      p[x] = p[y] = -1;
      continue;
    }
    if (x != -1)
      p[x] = 1;
    else if (y != -1)
      p[y] = 1;
    else
      s++;
  }
  int now = 0, s1 = 0, s2 = 0;
  f[0][0][0] = 1;
  for (int i = 2 * n; i >= 1; i--) {
    if (p[i] == -1)
      continue;
    now ^= 1;
    memset(f[now], 0, sizeof(f[now]));
    for (int j = 0; j <= _min(s1, n); j++)
      for (int k = 0; k <= _min(s2, n); k++)
        if (f[now ^ 1][j][k]) {
          int c = f[now ^ 1][j][k];
          if (p[i]) {
            if (j)
              f[now][j - 1][k] = add(f[now][j - 1][k], c);
            f[now][j][k + 1] = add(f[now][j][k + 1], c);
          } else {
            f[now][j + 1][k] = add(f[now][j + 1][k], c);
            if (j)
              f[now][j - 1][k] = add(f[now][j - 1][k], c);
            if (k)
              f[now][j][k - 1] = add(f[now][j][k - 1], (LL)c * k % mod);
          }
        }
    s1 += p[i] == 0, s2 += p[i] == 1;
  }
  int ans = f[now][0][0];
  for (int i = 1; i <= s; i++)
    ans = (LL)ans * i % mod;
  put(ans), puts("");
  return 0;
}