Problem p03191 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define P 1000000007

using namespace std;

typedef long long ll;

int n, A[777], B[777];

ll F[2][333][333], Fac[777], Rev[777], bra[777];

void add(ll &x, ll y) { x = (x + y) % P; }

ll powmod(ll x, ll y = P - 2, ll p = P) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans = ans * x % p;
    x = x * x % p;
    y >>= 1;
  }
  return ans;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * 2; i++)
    scanf("%d", &A[i]);
  for (int i = 0; i <= n * 2; i++)
    Fac[i] = !i ? 1 : Fac[i - 1] * i % P, Rev[i] = powmod(Fac[i]);
  for (int i = 0; i <= n; i++)
    bra[i] =
        Fac[i * 2] * Rev[i] % P * Rev[i] % P * powmod(i + 1) % P * Fac[i] % P;
  int a = 0, b = 1, cnt = 0;
  for (int i = 1; i <= n * 2; i += 2) {
    if (A[i] != -1 && A[i + 1] != -1) {
      B[A[i]] = (B[A[i + 1]] = 2);
    } else if (A[i] != -1)
      B[A[i]] = 1;
    else if (A[i + 1] != -1)
      B[A[i + 1]] = 1;
    else
      cnt++;
  }
  memset(F, 0, sizeof F);
  F[a][0][0] = 1;
  for (int i = 1; i <= n * 2; i++)
    if (B[i] != 2) {
      memset(F[b], 0, sizeof F[b]);
      for (int j = 0; j <= i; j++)
        for (int k = 0; k <= i; k++)
          if (F[a][j][k]) {
            ll v = F[a][j][k];
            if (B[i] == 0) {
              if (k)
                add(F[b][j][k - 1], v);
              add(F[b][j][k + 1], v);
              add(F[b][j + 1][k], v);
            } else {
              if (j)
                add(F[b][j - 1][k], v * j);
              add(F[b][j][k + 1], v);
            }
          }
      swap(a, b);
    }
  ll ans = 0;
  // for(int i = 0; i <= n; i++) ans = (ans + bra[i] * F[a][i * 2][0]) % P;
  printf("%lld\n", (F[a][0][0] * Fac[cnt] % P + P) % P);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define P 1000000007

using namespace std;

typedef long long ll;

int n, A[777], B[777];

ll F[2][333][333], Fac[777], Rev[777], bra[777];

void add(ll &x, ll y) { x = (x + y) % P; }

ll powmod(ll x, ll y = P - 2, ll p = P) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans = ans * x % p;
    x = x * x % p;
    y >>= 1;
  }
  return ans;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * 2; i++)
    scanf("%d", &A[i]);
  for (int i = 0; i <= n * 2; i++)
    Fac[i] = !i ? 1 : Fac[i - 1] * i % P, Rev[i] = powmod(Fac[i]);
  for (int i = 0; i <= n; i++)
    bra[i] =
        Fac[i * 2] * Rev[i] % P * Rev[i] % P * powmod(i + 1) % P * Fac[i] % P;
  int a = 0, b = 1, cnt = 0;
  for (int i = 1; i <= n * 2; i += 2) {
    if (A[i] != -1 && A[i + 1] != -1) {
      B[A[i]] = (B[A[i + 1]] = 2);
    } else if (A[i] != -1)
      B[A[i]] = 1;
    else if (A[i + 1] != -1)
      B[A[i + 1]] = 1;
    else
      cnt++;
  }
  memset(F, 0, sizeof F);
  F[a][0][0] = 1;
  for (int i = 1; i <= n * 2; i++)
    if (B[i] != 2) {
      memset(F[b], 0, sizeof F[b]);
      for (int j = 0; j <= i && j <= n; j++)
        for (int k = 0; k <= i && k <= n; k++)
          if (F[a][j][k]) {
            ll v = F[a][j][k];
            if (B[i] == 0) {
              if (k)
                add(F[b][j][k - 1], v);
              add(F[b][j][k + 1], v);
              add(F[b][j + 1][k], v);
            } else {
              if (j)
                add(F[b][j - 1][k], v * j);
              add(F[b][j][k + 1], v);
            }
          }
      swap(a, b);
    }
  ll ans = 0;
  // for(int i = 0; i <= n; i++) ans = (ans + bra[i] * F[a][i * 2][0]) % P;
  printf("%lld\n", (F[a][0][0] * Fac[cnt] % P + P) % P);
  return 0;
}