#define DEBUG(...) cerr << __VA_ARGS__ << endl;

#ifndef CDEBUG
#undef DEBUG
#define DEBUG(...) ((void)0);
#define NDEBUG
#endif

#define ran(i, a, b) for (auto i = (a); i < (b); i++)

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
const int mod = 1e9 + 7;
#ifndef M_PI
const double M_PI = acos(-1.0);
#endif

ll mod_pow(ll x, int n){
  ll r = 1;
  for(int i=1; i<=n; i <<= 1){
    if(i & n){
      r *= x;
      r %= mod;
    }
    x *= x;
    x %= mod;
  }
  return r;
}

ll mod_inv(ll x){
  return mod_pow(x, mod-2);
}

const int nmax = 5005;

int par[nmax];
int sze[nmax];
ll dp[nmax];
ll dps[nmax];

int anc(int i){
  if(par[i]){
    par[i] = anc(par[i]);
    return par[i];
  }
  return i;
}

int main() {
  int n;
  cin>>n;
  vector<int> unknown;
  int cycle_cnt = 0;
  for(int i=1; i<=n; ++i){
    int p;
    cin>>p;
    if(p==-1){
      unknown.push_back(i);
    } else {
      //i is root;
      int v = anc(p);
      if( v != i){
        par[i] = v;
      } else {
        ++cycle_cnt;
      }
    }
  }
  for(int i=1; i<=n; ++i){
    ++sze[anc(i)];
  }
  vector<int> sizes;
  for(int i : unknown){
    sizes.push_back(sze[i]);
  }
  dps[0] = 1;
  for(int m : sizes){
    dps[0] *= n-1;
    dps[0] %= mod;
  }
  ll mult = mod_inv(n-1);
  ll res = dps[0]*(n-cycle_cnt)%mod;
  for(int m : sizes){
    dp[1] = m*dps[0]%mod*mult%mod;
    res += dps[0]*mult%mod;
    res %= mod;
    for(int j=1; j<sizes.size(); ++j){
      dp[j+1] = m*j*dps[j]%mod*mult%mod;
    }
    for(int j=0; j<sizes.size(); ++j){
      dps[j+1] += dp[j+1];
      dps[j+1] %= mod;
    }
  }
  for(int j=0; j<sizes.size(); ++j){
    res -= dps[j+1];
  }
  res %= mod;
  res += mod;
  res %= mod;
  cout<<res<<endl;
  return 0;
}
