Problem p03191 (C++)

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int P = 1e9 + 7;
inline void upd(R int &x, R int y) { (x += y) >= P ? x -= P : 0; }
inline int inc(R int x, R int y) { return x + y >= P ? x + y - P : x + y; }
inline int dec(R int x, R int y) { return x - y < 0 ? x - y + P : x - y; }
inline int mul(R int x, R int y) { return 1ll * x * y - 1ll * x * y / P * P; }
int ksm(R int x, R int y) {
  R int res = 1;
  for (; y; y >>= 1, x = mul(x, x))
    (y & 1) ? res = mul(res, x) : 0;
  return res;
}
const int N = 305;
int f[N][N][N], a[N << 1], st[N << 1], vis[N << 1], bk[N << 1], top, n, cnt, ct,
    res;
int main() {
  //	freopen("testdata.in","r",stdin);
  scanf("%d", &n);
  fp(i, 1, n << 1) scanf("%d", &a[i]);
  for (R int i = 1; i <= (n << 1); i += 2) {
    if (a[i] == a[i + 1])
      ++cnt;
    else if (a[i] > 0 && a[i + 1] > 0)
      vis[a[i]] = vis[a[i + 1]] = 1;
    else
      bk[(~a[i]) ? a[i] : a[i + 1]] = 1, ++ct;
  }
  fd(i, n << 1, 1) if (!vis[i]) st[++top] = i;
  f[0][0][0] = 1;
  fp(i, 1, top) fp(j, 0, cnt + ct) fp(k, 0, ct) if (f[i - 1][j][k]) {
    R int coef = f[i - 1][j][k];
    if (!bk[st[i]]) {
      upd(f[i][j + 1][k], coef);
      if (j)
        upd(f[i][j - 1][k], coef);
      if (k)
        upd(f[i][j][k - 1], mul(coef, k));
    } else {
      upd(f[i][j][k + 1], coef);
      if (j)
        upd(f[i][j - 1][k], coef);
    }
  }
  res = f[top][0][0];
  fp(i, 1, cnt) res = mul(res, i);
  printf("%d\n", res);
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
const int P = 1e9 + 7;
inline void upd(R int &x, R int y) { (x += y) >= P ? x -= P : 0; }
inline int inc(R int x, R int y) { return x + y >= P ? x + y - P : x + y; }
inline int dec(R int x, R int y) { return x - y < 0 ? x - y + P : x - y; }
inline int mul(R int x, R int y) { return 1ll * x * y - 1ll * x * y / P * P; }
int ksm(R int x, R int y) {
  R int res = 1;
  for (; y; y >>= 1, x = mul(x, x))
    (y & 1) ? res = mul(res, x) : 0;
  return res;
}
const int N = 305;
int f[N << 1][N][N], a[N << 1], st[N << 1], vis[N << 1], bk[N << 1], top, n,
    cnt, ct, res;
int main() {
  //	freopen("testdata.in","r",stdin);
  scanf("%d", &n);
  fp(i, 1, n << 1) scanf("%d", &a[i]);
  for (R int i = 1; i <= (n << 1); i += 2) {
    if (a[i] == a[i + 1])
      ++cnt;
    else if (a[i] > 0 && a[i + 1] > 0)
      vis[a[i]] = vis[a[i + 1]] = 1;
    else
      bk[(~a[i]) ? a[i] : a[i + 1]] = 1, ++ct;
  }
  fd(i, n << 1, 1) if (!vis[i]) st[++top] = i;
  f[0][0][0] = 1;
  fp(i, 1, top) fp(j, 0, cnt + ct) fp(k, 0, ct) if (f[i - 1][j][k]) {
    R int coef = f[i - 1][j][k];
    if (!bk[st[i]]) {
      upd(f[i][j + 1][k], coef);
      if (j)
        upd(f[i][j - 1][k], coef);
      if (k)
        upd(f[i][j][k - 1], mul(coef, k));
    } else {
      upd(f[i][j][k + 1], coef);
      if (j)
        upd(f[i][j - 1][k], coef);
    }
  }
  res = f[top][0][0];
  fp(i, 1, cnt) res = mul(res, i);
  printf("%d\n", res);
  return 0;
}