Problem p03521 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = 500000004;
const double EPS = 1e-6;
const double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
vector<int> g[2100];
int sz[2100];
long long dp[3][2100][2100];
long long dp2[4][2100][4100];
long long fact[3100];
void solve(int a) {
  //		printf("%d\\n",a);fflush(stdout);
  int n = g[a].size();
  dp2[0][0][0] = 1;
  for (int i = 0; i < g[a].size(); i++) {
    solve(g[a][i]);
  }
  for (int i = 0; i < g[a].size(); i++) {
    int T = sz[a];
    sz[a] += sz[g[a][i]];
    int to = g[a][i];
    for (int j = 0; j <= sz[a] + 10; j++) {
      dp2[0][i + 1][j] = dp2[1][i + 1][j] = dp2[2][i + 1][j] =
          dp2[3][i + 1][j] = 0;
    }
    for (int j = 0; j <= T; j++) {
      //	if(a==1&&j<2)printf("%d %d: %lld %lld
      //%lld\\n",i,j,dp2[0][i][j],dp2[1][i][j],dp2[2][i][j]);

      for (int k = 0; k <= sz[g[a][i]] + 1; k++) {
        dp2[0][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[0][i][j] * dp[0][to][k]) % mod;
        dp2[1][i + 1][j + k] =
            (dp2[1][i + 1][j + k] + dp2[0][i][j] * dp[1][to][k]) % mod;
        dp2[1][i + 1][j + k] =
            (dp2[1][i + 1][j + k] + dp2[1][i][j] * dp[0][to][k]) % mod;
        dp2[2][i + 1][j + k] =
            (dp2[2][i + 1][j + k] + dp2[2][i][j] * dp[0][to][k]) % mod;
        dp2[2][i + 1][j + k] =
            (dp2[2][i + 1][j + k] + dp2[0][i][j] * dp[2][to][k]) % mod;
        dp2[3][i + 1][j + k] =
            (dp2[3][i + 1][j + k] + dp2[1][i][j] * dp[2][to][k]) % mod;
        dp2[3][i + 1][j + k] =
            (dp2[3][i + 1][j + k] + dp2[2][i][j] * dp[1][to][k]) % mod;
        dp2[3][i + 1][j + k] =
            (dp2[3][i + 1][j + k] + dp2[3][i][j] * dp[0][to][k]) % mod;
      }
    }
  }

  // if(a==1)printf("%d %d: %lld %lld
  // %lld\\n",4,0,dp2[0][4][0],dp2[1][4][0],dp2[2][4][0]); if(a==1)printf("%d %d:
  // %lld %lld %lld\\n",4,1,dp2[0][4][1],dp2[1][4][1],dp2[2][4][1]);

  //	printf("AA\\n");fflush(stdout);
  for (int i = 0; i <= sz[a]; i++) {
    dp[0][a][i] =
        (dp2[0][n][i] + dp2[1][n][i] + dp2[2][n][i] + dp2[3][n][i]) % mod;
    dp[1][a][i + 1] = (dp2[0][n][i] + dp2[1][n][i]) % mod;
    dp[2][a][i + 1] = (dp2[0][n][i] + dp2[2][n][i]) % mod;
    //		if(i<=1)printf("%d %d: %lld
    //%lld\\n",a,i,dp[0][a][i],dp[1][a][i]);
  }
  //	if(sz[a]+1==1)printf("%d %d: %lld
  //%lld\\n",a,sz[a]+1,dp[0][a][sz[a]+1],dp[1][a][sz[a]+1]);
  sz[a]++;
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a - 1; i++) {
    int p;
    scanf("%d", &p);
    p--;
    //	printf("%d %d\\n",i+1,p);fflush(stdout);
    g[p].push_back(i + 1);
  }
  solve(0);
  long long ret = 0;
  fact[0] = 1;
  for (int i = 1; i < 3100; i++)
    fact[i] = fact[i - 1] * i % mod;

  for (int i = 0; i <= a; i++) {
    //	printf("%d: %lld\\n",i,dp[0][0][i]);
    if (i % 2 == 0)
      ret = (ret + dp[0][0][i] * fact[a - i] % mod) % mod;
    else
      ret = (ret + mod - dp[0][0][i] * fact[a - i] % mod) % mod;
  }
  printf("%lld\\n", ret);
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = 500000004;
const double EPS = 1e-6;
const double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
vector<int> g[2100];
int sz[2100];
long long dp[2][2100];
long long dp2[3][2100][2100];
long long fact[3100];
void solve(int a) {
  int n = g[a].size();
  dp2[0][0][0] = 1;
  for (int i = 0; i < g[a].size(); i++) {
    solve(g[a][i]);
    int T = sz[a];
    sz[a] += sz[g[a][i]];
    for (int j = 0; j <= sz[a]; j++) {
      dp2[0][i + 1][j] = dp2[1][i + 1][j] = dp2[2][i + 1][j] = 0;
    }
    for (int j = 0; j <= T; j++) {
      for (int k = 0; k <= sz[g[a][i]]; k++) {
        dp2[0][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[0][i][j] * dp[0][k]) % mod;
        dp2[1][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[0][i][j] * dp[1][k]) % mod;
        dp2[1][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[1][i][j] * dp[0][k]) % mod;
        dp2[2][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[1][i][j] * dp[1][k]) % mod;
        dp2[2][i + 1][j + k] =
            (dp2[0][i + 1][j + k] + dp2[2][i][j] * dp[0][k]) % mod;
      }
    }
  }
  for (int i = 0; i <= sz[g[a][i]]; i++) {
    dp[0][i] = (dp2[0][n][i] + dp2[1][n][i] * 2 + dp2[2][n][i]) % mod;
    dp[1][i + 1] = dp2[1][n][i] % mod;
  }
  sz[a]++;
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a - 1; i++) {
    int p;
    scanf("%d", &p);
    p--;
    g[p].push_back(i + 1);
  }
  solve(0);
  long long ret = 0;
  fact[0] = 1;
  for (int i = 1; i < 3100; i++)
    fact[i] = fact[i - 1] * i % mod;

  for (int i = 0; i <= a; i++) {
    if (i % 2 == 0)
      ret = (ret + dp[0][i] * fact[a - i] % mod) % mod;
    else
      ret = (ret + mod - dp[0][i] * fact[a - i] % mod) % mod;
  }
  printf("%lld\\n", ret);
}