#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

class DisjointSet{
public:
  vector<int> rank,p;
  vector<int> sz;
 
  DisjointSet(){}
  DisjointSet(int size){
    rank.resize(size,0);
    p.resize(size,0);
    sz.resize(size,0);
    for(int i=0;i<size;i++) makeSet(i);
  }
 
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
    sz[x]=1;
  }
 
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
 
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
 
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      sz[x]+=sz[y];
    }else{
      p[x]=y;
      sz[y]+=sz[x];
      if(rank[x]==rank[y]){
        rank[y]++;
      }
    }
  }
 
  int findSet(int x){
    if(x!=p[x]){
      p[x]=findSet(p[x]);
    }
    return p[x];
  }
 
  int findSize(int x){
    return sz[findSet(x)];
  }
};

#define MAX_N 1000010
ll inv[MAX_N+10],fac[MAX_N+10],ifac[MAX_N+10];

void setComb(){
  inv[0]=1;inv[1]=1;fac[1]=1;ifac[1]=1;fac[0]=1;ifac[0]=1;
  for(int i=2;i<MAX_N;i++){
    inv[i]=(-MOD/i)*inv[MOD%i]%MOD;
    fac[i]=fac[i-1]*i%MOD;
    ifac[i]=ifac[i-1]*inv[i]%MOD;

    inv[i]=(inv[i]+MOD)%MOD;
    fac[i]=(fac[i]+MOD)%MOD;
    ifac[i]=(ifac[i]+MOD)%MOD;
  }
  return;
}

ll comb(ll n,ll k){
  if(n<k||n<0||k<0) return 0;
  else return ((fac[n]*ifac[k]%MOD*ifac[n-k]%MOD+MOD)%MOD);
}

ll hcomb(ll n,ll r){// this size is really ok??
  if(n==0&&r==0) return 1;
  else if(n<0||r<0) return 0;
  else return comb(n+r-1,r);
}


ll mod_pow(ll x,ll n){
  x%=MOD;
  ll res=1;
  while(n>0){
    if(n&1) res=res*x%MOD;
    x=x*x%MOD;
    n>>=1;
  }
  return res;
}

void add(ll &a,ll b){
  a=(a+b)%MOD;
}

void mul(ll &a,ll b){
  a%=MOD;b%=MOD;
  a=a*b%MOD;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  setComb();

  ll N;
  cin>>N;
  vector<int> P(N);
  rep(i,N) cin>>P[i];

  DisjointSet us(N);
  int K=0;
  rep(i,N) if(P[i]==-1) K++;

  rep(i,N){
    if(P[i]==-1) continue;
    P[i]--;
    if(us.same(i,P[i])==false) us.unite(i,P[i]);
  }

  vector<ll> A;
  rep(i,N){
    if(P[i]==-1) A.push_back(us.findSize(i));
  }
  int M=A.size();
  vector<vector<ll>> dp(M+1,vector<ll> (M+1,0));
  dp[0][0]=1;
  for(int i=0;i<M;i++) for(int j=0;j<=M;j++){
    if(dp[i][j]==0) continue;
    add(dp[i+1][j],dp[i][j]);
    if(j+1<=M) add(dp[i+1][j+1],dp[i][j]*A[i]%MOD);
  }

  ll nans=0;
  for(int i=0;i<M;i++){
    add(nans,(A[i]-1)*mod_pow(N-1,K-1)%MOD);
  }
  vector<int> yet(N,0);
  rep(i,N) if(P[i]==-1) yet[us.findSet(i)]=1;
  for(int i=0;i<N;i++){
    if(yet[us.findSet(i)]) continue;
    if(i!=us.findSet(i)) continue;
    int sz=us.findSize(i);
    add(nans,mod_pow(N-1,K)%MOD);
  }

  for(int i=2;i<=M;i++){
    ll res=mod_pow(N-1,K-i);
    mul(res,fac[i-1]);
    mul(res,dp[M][i]);
    add(nans,res);
  }

  ll ans=N;
  mul(ans,mod_pow(N-1,K));
  add(ans,MOD-nans);
  cout<<ans<<endl;

  return 0;
}


Measured memory_bytes: 222304