Problem p02666 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 5003, M = 1000000007;

int n;
int p[N];
int qmm;
vector<int> g[N];

int ast[N], f[N];

bool c[N];
int q;
void dfs(int x) {
  c[x] = true;
  ++q;
  for (int i = 0; i < g[x].size(); ++i) {
    int h = g[x][i];
    dfs(h);
  }
  if (p[x] != -1 && !c[p[x]])
    dfs(p[x]);
}

int ans;

int dp[N][N];

void solv() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &p[i]);
    if (p[i] != -1)
      g[p[i]].push_back(i);
    else
      ++qmm;
  }
  ast[0] = 1;
  for (int i = 1; i <= n; ++i)
    ast[i] = (ast[i - 1] * 1LL * (n - 1)) % M;
  f[0] = 1;
  for (int i = 1; i <= n; ++i)
    f[i] = (f[i - 1] * 1LL * i) % M;
  int ans = 0;
  vector<int> v;
  for (int i = 1; i <= n; ++i) {
    if (p[i] == -1) {
      q = 0;
      dfs(i);
      v.push_back(q);
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (!c[i]) {
      dfs(i);
      ans = (ans + ast[qmm]) % M;
    }
  }
  dp[0][0] = 1;
  for (int i = 0; i < v.size(); ++i) {
    for (int j = 0; j < v.size(); ++j) {
      dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % M;
      dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * 1LL * v[i]) % M;
    }
  }
  for (int j = 2; j <= v.size(); ++j) {
    ans =
        (ans + (dp[v.size()][j] * 1LL * f[j - 1]) % M * 1LL * ast[qmm - j]) % M;
  }
  for (int i = 0; i < v.size(); ++i) {
    ans = (ans + (v[i] - 1) * 1LL * ast[qmm - 1]) % M;
  }
  ans = ((n * 1LL * ast[qmm]) % M - ans + M) % M;
  printf("%d\n", ans);
}

int main() {
#ifdef SOMETHING
  freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  solv();
  return 0;
}

// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 5003, M = 1000000007;

int n;
int p[N];
int qmm;
vector<int> g[N];

int ast[N], f[N];

bool c[N];
int q;
void dfs(int x) {
  c[x] = true;
  ++q;
  for (int i = 0; i < g[x].size(); ++i) {
    int h = g[x][i];
    if (!c[h])
      dfs(h);
  }
  if (p[x] != -1 && !c[p[x]])
    dfs(p[x]);
}

int ans;

int dp[N][N];

void solv() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &p[i]);
    if (p[i] != -1)
      g[p[i]].push_back(i);
    else
      ++qmm;
  }
  ast[0] = 1;
  for (int i = 1; i <= n; ++i)
    ast[i] = (ast[i - 1] * 1LL * (n - 1)) % M;
  f[0] = 1;
  for (int i = 1; i <= n; ++i)
    f[i] = (f[i - 1] * 1LL * i) % M;
  int ans = 0;
  vector<int> v;
  for (int i = 1; i <= n; ++i) {
    if (p[i] == -1) {
      q = 0;
      dfs(i);
      v.push_back(q);
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (!c[i]) {
      dfs(i);
      ans = (ans + ast[qmm]) % M;
    }
  }
  dp[0][0] = 1;
  for (int i = 0; i < v.size(); ++i) {
    for (int j = 0; j < v.size(); ++j) {
      dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % M;
      dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * 1LL * v[i]) % M;
    }
  }
  for (int j = 2; j <= v.size(); ++j) {
    ans =
        (ans + (dp[v.size()][j] * 1LL * f[j - 1]) % M * 1LL * ast[qmm - j]) % M;
  }
  for (int i = 0; i < v.size(); ++i) {
    ans = (ans + (v[i] - 1) * 1LL * ast[qmm - 1]) % M;
  }
  ans = ((n * 1LL * ast[qmm]) % M - ans + M) % M;
  printf("%d\n", ans);
}

int main() {
#ifdef SOMETHING
  freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  solv();
  return 0;
}

// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}