Problem p02666 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<ll, ll>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 5010, mod = 1e9 + 7;
ll dp[N][N];
ll fac[N];
vector<int> g[N];
ll t = 0;
ll vis[N];
ll dfs(ll v) {
  vis[v] = 1;
  t++;
  for (auto u : g[v]) {
    if (vis[u])
      continue;
    dfs(u);
  }
}
ll a[N];
ll mol[N];
int32_t main() {
  fac[0] = 1;
  for (int i = 1; i < N; i++) {
    fac[i] = fac[i - 1] * i % mod;
  }
  ll power = 1;
  ll n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] != -1) {
      g[i].pb(a[i]);
      g[a[i]].pb(i);
    }
  }
  ll p2 = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] != -1)
      continue;
    t = 0;
    dfs(i);
    mol[p2++] = t;
  }
  p2--;
  for (int i = 1; i <= p2; i++) {
    // cout << mol[i] << endl;
    power *= n - 1;
    power %= mod;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      t = 0;
      dfs(i);
      ans += power;
    }
  }
  // cout << ans << " " << mol[1] << endl;
  dp[0][0] = 1;
  for (int i = 1; i <= p2; i++) {
    for (int j = 1; j <= i; j++) {
      dp[i][j] =
          dp[i - 1][j] * (n - 1) % mod + dp[i - 1][j - 1] * (mol[i]) % mod;
      dp[i][j] %= mod;
    }
    dp[i][0] = dp[i - 1][0] * (n - 1);
    dp[i][0] %= mod;
  }
  for (int i = 2; i <= p2; i++) {
    ans += dp[p2][i] * fac[i - 1] % mod;
    //  cout << i << " " << dp[p2][i]*fac[i-1]%mod << endl;
    ans %= mod;
  }
  ll powe = 1;
  for (int i = 1; i < p2; i++) {
    powe *= n - 1;
    powe %= mod;
  }
  for (int i = 1; i <= p2; i++) {
    if (mol[i] > 1) {
      ans += powe * (mol[i] - 1);
      ans %= mod;
    }
  }
  ll z = power * (n);
  z %= mod;
  z -= ans;
  z += mod;
  cout << z % mod << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<ll, ll>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 5010, mod = 1e9 + 7;
ll dp[N][N];
ll fac[N];
vector<int> g[N];
ll t = 0;
ll vis[N];
void dfs(ll v) {
  vis[v] = 1;
  t++;
  for (auto u : g[v]) {
    if (vis[u])
      continue;
    dfs(u);
  }
}
ll a[N];
ll mol[N];
int32_t main() {
  fac[0] = 1;
  for (int i = 1; i < N; i++) {
    fac[i] = fac[i - 1] * i % mod;
  }
  ll power = 1;
  ll n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] != -1) {
      g[i].pb(a[i]);
      g[a[i]].pb(i);
    }
  }
  ll p2 = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] != -1)
      continue;
    t = 0;
    dfs(i);
    mol[p2++] = t;
  }
  p2--;
  for (int i = 1; i <= p2; i++) {
    // cout << mol[i] << endl;
    power *= n - 1;
    power %= mod;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      t = 0;
      dfs(i);
      ans += power;
    }
  }
  // cout << ans << " " << mol[1] << endl;
  dp[0][0] = 1;
  for (int i = 1; i <= p2; i++) {
    for (int j = 1; j <= i; j++) {
      dp[i][j] =
          dp[i - 1][j] * (n - 1) % mod + dp[i - 1][j - 1] * (mol[i]) % mod;
      dp[i][j] %= mod;
    }
    dp[i][0] = dp[i - 1][0] * (n - 1);
    dp[i][0] %= mod;
  }
  for (int i = 2; i <= p2; i++) {
    ans += dp[p2][i] * fac[i - 1] % mod;
    //  cout << i << " " << dp[p2][i]*fac[i-1]%mod << endl;
    ans %= mod;
  }
  ll powe = 1;
  for (int i = 1; i < p2; i++) {
    powe *= n - 1;
    powe %= mod;
  }
  for (int i = 1; i <= p2; i++) {
    if (mol[i] > 1) {
      ans += powe * (mol[i] - 1);
      ans %= mod;
    }
  }
  ll z = power * (n);
  z %= mod;
  z -= ans;
  z += mod;
  cout << z % mod << endl;
}