Problem p03191 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define re register
const int mod = 1e9 + 7;
struct par {
  int x, tp;
} c[1010];
int a[610], b[610], qq[610], f[310][610], g[310][610];
inline bool cmp(const par &A, const par &B) { return A.x < B.x; }
int main() {
  re int n, nm1 = 0, tt = 0;
  scanf("%d", &n);
  for (re int i = 1; i <= 2 * n; i++) {
    scanf("%d", &a[i]);
    a[i] = a[i] != -1 ? 2 * n + 1 - a[i] : -1;
    if (a[i] != -1)
      b[a[i]] = 1;
  }
  for (re int i = 1; i <= 2 * n; i++)
    if (!b[i])
      c[++tt] = (par){i, 1};
  for (re int i = 1; i <= n; i++)
    if (a[i * 2 - 1] == -1 && a[i * 2] == -1)
      nm1++;
    else if (a[i * 2 - 1] == -1 || a[i * 2] == -1)
      c[++tt] = (par){max(a[i * 2 - 1], a[i * 2]), 0};
  sort(c + 1, c + tt + 1, cmp);
  f[0][0] = 1;
  re int a1 = 0, b1 = 0;
  for (re int i = 1; i <= tt; i++) {
    memcpy(g, f, sizeof(f));
    memset(f, 0, sizeof(f));
    // printf("**%d %d\n",c[i].tp,c[i].x);
    if (c[i].tp) {
      a1++;
      for (re int j = 0; j <= a1; j++)
        for (re int k = 0; k <= b1; k++)
          f[j][k] = ((long long)g[j][k + 1] * (k + 1) + (j ? g[j - 1][k] : 0) +
                     g[j + 1][k]) %
                    mod; //,printf("*l*%d %d %d\n",j,k,f[j][k]);
    } else {
      b1++;
      for (re int j = 0; j <= a1; j++)
        for (re int k = 0; k <= b1; k++)
          f[j][k] = ((k ? g[j][k - 1] : 0) + g[j + 1][k]) %
                    mod; //,printf("*l*%d %d %d\n",j,k,f[j][k]);
    }
  }
  for (re int i = 1; i <= nm1; i++)
    f[0][0] = (long long)f[0][0] * i % mod;
  printf("%d\n", f[0][0]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define re register
const int mod = 1e9 + 7;
struct par {
  int x, tp;
} c[1010];
int a[610], b[610], qq[610], f[610][310], g[610][310];
inline bool cmp(const par &A, const par &B) { return A.x < B.x; }
int main() {
  re int n, nm1 = 0, tt = 0;
  scanf("%d", &n);
  for (re int i = 1; i <= 2 * n; i++) {
    scanf("%d", &a[i]);
    a[i] = a[i] != -1 ? 2 * n + 1 - a[i] : -1;
    if (a[i] != -1)
      b[a[i]] = 1;
  }
  for (re int i = 1; i <= 2 * n; i++)
    if (!b[i])
      c[++tt] = (par){i, 1};
  for (re int i = 1; i <= n; i++)
    if (a[i * 2 - 1] == -1 && a[i * 2] == -1)
      nm1++;
    else if (a[i * 2 - 1] == -1 || a[i * 2] == -1)
      c[++tt] = (par){max(a[i * 2 - 1], a[i * 2]), 0};
  sort(c + 1, c + tt + 1, cmp);
  f[0][0] = 1;
  re int a1 = 0, b1 = 0;
  for (re int i = 1; i <= tt; i++) {
    memcpy(g, f, sizeof(f));
    memset(f, 0, sizeof(f));
    // printf("**%d %d\n",c[i].tp,c[i].x);
    if (c[i].tp) {
      a1++;
      for (re int j = 0; j <= a1; j++)
        for (re int k = 0; k <= b1; k++)
          f[j][k] = ((long long)g[j][k + 1] * (k + 1) + (j ? g[j - 1][k] : 0) +
                     g[j + 1][k]) %
                    mod; //,printf("*l*%d %d %d\n",j,k,f[j][k]);
    } else {
      b1++;
      for (re int j = 0; j <= a1; j++)
        for (re int k = 0; k <= b1; k++)
          f[j][k] = ((k ? g[j][k - 1] : 0) + g[j + 1][k]) %
                    mod; //,printf("*l*%d %d %d\n",j,k,f[j][k]);
    }
  }
  for (re int i = 1; i <= nm1; i++)
    f[0][0] = (long long)f[0][0] * i % mod;
  printf("%d\n", f[0][0]);
}