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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> p2;
ll p[5005], tr[5005], fac[5005], par[5005], dp[5005][5005],
    hell = pow(10, 9) + 7;
ll find(ll i) {
  if (par[i] == i)
    return i;
  return par[i] = find(par[i]);
}
void merge(ll a, ll b) {
  a = find(a), b = find(b);
  if (a == b)
    return;
  par[b] = a;
}
void solve() {
  ll n, res = 0, k = 0;
  cin >> n;
  tr[0] = 1;
  for (ll i = 1; i <= n; i++) {
    par[i] = i;
    tr[i] = ((n - 1) * tr[i - 1]) % hell;
  }
  for (ll i = 1; i <= n; i++) {
    cin >> p[i];
    if (p[i] == (-1)) {
      k++;
      continue;
    }
    merge(i, p[i]);
  }
  vector<vector<ll>> g(5005);
  for (ll i = 1; i <= n; i++)
    g[find(i)].push_back(i);
  vector<ll> v;
  for (ll i = 1; i <= n; i++)
    if (!g[i].empty()) {
      ll f = 0;
      for (ll j = 0; j < ll(g[i].size()); j++)
        if (p[g[i][j]] == (-1))
          f = 1;
      if (!f)
        res = (res - tr[k]) % hell;
      else
        v.push_back(ll(g[i].size()));
    }
  ll m = ll(v.size());
  for (ll i = 0; i <= m; i++)
    dp[i][0] = 1;
  for (ll i = 1; i <= m; i++)
    for (ll j = 1; j <= k; j++)
      dp[i][j] = (dp[i - 1][j] + v[i - 1] * dp[i - 1][j - 1]) % hell;
  for (ll i = 2; i <= k; i++) {
    ll val = (dp[m][i] * fac[i - 1]) % hell;
    val *= tr[k - i];
    val %= hell;
    res = (res - val) % hell;
  }
  for (ll i = 0; i < m; i++)
    res = (res - (((v[i] - 1) * tr[k - 1]) % hell)) % hell;
  res += (n * tr[k]) % hell;
  res %= hell;
  if (res < 0)
    res += hell;
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  fac[0] = 1;
  for (ll i = 1; i < 5005; i++)
    fac[i] = (i * fac[i - 1]) % hell;
  ll qc = 1;
  for (ll i = 1; i <= qc; i++)
    solve();
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> p2;
ll p[5005], tr[5005], fac[5005], par[5005], dp[5005][5005],
    hell = pow(10, 9) + 7;
ll find(ll i) {
  if (par[i] == i)
    return i;
  return par[i] = find(par[i]);
}
void merge(ll a, ll b) {
  a = par[a], b = par[b];
  if (a == b)
    return;
  par[b] = a;
}
void solve() {
  ll n, res = 0, k = 0;
  cin >> n;
  tr[0] = 1;
  for (ll i = 1; i <= n; i++) {
    par[i] = i;
    tr[i] = ((n - 1) * tr[i - 1]) % hell;
  }
  for (ll i = 1; i <= n; i++) {
    cin >> p[i];
    if (p[i] == (-1)) {
      k++;
      continue;
    }
    merge(i, p[i]);
  }
  vector<vector<ll>> g(5005);
  for (ll i = 1; i <= n; i++)
    g[find(i)].push_back(i);
  vector<ll> v;
  for (ll i = 1; i <= n; i++)
    if (!g[i].empty()) {
      ll f = 0;
      for (ll j = 0; j < ll(g[i].size()); j++)
        if (p[g[i][j]] == (-1))
          f = 1;
      if (!f)
        res = (res - tr[k]) % hell;
      else
        v.push_back(ll(g[i].size()));
    }
  ll m = ll(v.size());
  for (ll i = 0; i <= m; i++)
    dp[i][0] = 1;
  for (ll i = 1; i <= m; i++)
    for (ll j = 1; j <= k; j++)
      dp[i][j] = (dp[i - 1][j] + v[i - 1] * dp[i - 1][j - 1]) % hell;
  for (ll i = 2; i <= k; i++) {
    ll val = (dp[m][i] * fac[i - 1]) % hell;
    val *= tr[k - i];
    val %= hell;
    res = (res - val) % hell;
  }
  for (ll i = 0; i < m; i++)
    res = (res - (((v[i] - 1) * tr[k - 1]) % hell)) % hell;
  res += (n * tr[k]) % hell;
  res %= hell;
  if (res < 0)
    res += hell;
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  fac[0] = 1;
  for (ll i = 1; i < 5005; i++)
    fac[i] = (i * fac[i - 1]) % hell;
  ll qc = 1;
  for (ll i = 1; i <= qc; i++)
    solve();
}