Language: C++

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll>P;
template< typename T >
ll mod_pow(ll x, ll n, const ll &p) {
  ll ret = 1;
  while(n > 0) {
    if(n & 1) (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}
const int MAX = 510000;
const int MOD = 1000000007;
//この設定だとn<10^7が限界。maxの０を一個増やせば100msかかるが一桁増やせる。
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++) {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main(){
  COMinit();
  ll N;
  cin>>N;
  vector<int>P(N);rep(i,N){P[i]=-1;}
  ll cnt=0;
  queue<int>qu;
  vector<vector<int>> ed(N);
  ll road=0;
  rep(i,N){
  	ll p;
    cin>>p;
    p--;
    P[i]=p;
    if(p==-2){
      cnt++;
      qu.push(i);
    }
    else{
      if(P[p]!=i){
      	ed[i].push_back(p);
      	ed[p].push_back(i);
        road++;
      }
    }
  }
  vector<int>Fr;
  while(!qu.empty()){
    Fr.push_back(qu.front());
    qu.pop();
  }
  ll ans=0;
  if(cnt==0){cout<<road;}
  if(cnt==1){
  	ans+=(((N-1ll)*road)%MOD);
    ans+=(N-1-ed[Fr[0]].size());
    ans%=MOD;
    cout<<ans;
  }
  else{
    ans=ans+road*mod_pow(N-1,cnt,(int64_t)(1e9 + 7));
    ans%=MOD;
    rep(i,cnt){
      ll q=Fr[i];
      ll f=N-1ll-ed[q].size();
      ans=ans+(f)*mod_pow(N-1ll,cnt-1ll,(int64_t)(1e9 + 7));
      ans%=MOD;
    }
    ll z=(cnt*(cnt-1ll))/2ll;
    z%=MOD;
    z=(z*mod_pow(N-1ll,cnt-2ll,(int64_t)(1e9 + 7)))%MOD;
  }
  ans+=MOD;
  ans-=z;
  ans%=MOD;
  cout<<ans;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:79:25: error: no matching function for call to 'mod_pow(ll, ll&, int64_t)'
   79 |     ans=ans+road*mod_pow(N-1,cnt,(int64_t)(1e9 + 7));
      |                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:7:4: note: candidate: 'template<class T> ll mod_pow(ll, ll, const ll&)'
    7 | ll mod_pow(ll x, ll n, const ll &p) {
      |    ^~~~~~~
a.cc:7:4: note:   template argument deduction/substitution failed:
a.cc:79:25: note:   couldn't deduce template parameter 'T'
   79 |     ans=ans+road*mod_pow(N-1,cnt,(int64_t)(1e9 + 7));
      |                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:84:26: error: no matching function for call to 'mod_pow(ll, ll, int64_t)'
   84 |       ans=ans+(f)*mod_pow(N-1ll,cnt-1ll,(int64_t)(1e9 + 7));
      |                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:7:4: note: candidate: 'template<class T> ll mod_pow(ll, ll, const ll&)'
    7 | ll mod_pow(ll x, ll n, const ll &p) {
      |    ^~~~~~~
a.cc:7:4: note:   template argument deduction/substitution failed:
a.cc:84:26: note:   couldn't deduce template parameter 'T'
   84 |       ans=ans+(f)*mod_pow(N-1ll,cnt-1ll,(int64_t)(1e9 + 7));
      |                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:89:17: error: no matching function for call to 'mod_pow(ll, ll, int64_t)'
   89 |     z=(z*mod_pow(N-1ll,cnt-2ll,(int64_t)(1e9 + 7)))%MOD;
      |          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:7:4: note: candidate: 'template<class T> ll mod_pow(ll, ll, const ll&)'
    7 | ll mod_pow(ll x, ll n, const ll &p) {
      |    ^~~~~~~
a.cc:7:4: note:   template argument deduction/substitution failed:
a.cc:89:17: note:   couldn't deduce template parameter 'T'
   89 |     z=(z*mod_pow(N-1ll,cnt-2ll,(int64_t)(1e9 + 7)))%MOD;
      |          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:92:8: error: 'z' was not declared in this scope
   92 |   ans-=z;
      |        ^