#include<bits/stdc++.h>
using namespace std;
const int N = 5e3+100;
const int P = 1e9+7;
#define fr(i,a,b) for(int i = a;i <= b; ++i)
#define nfr(i,a,b) for(int i = a;i >= b; --i)

int n,m;
int p[N],f[N];

inline void add(int &x,int y){
   x += y; if(x >= P)x -= P;
}

inline int pow_mod(int x,int k){
   int res = 1,base = x;
   while(k){
      if(k&1)res = 1LL*res*base%P;
      k >>= 1; base = 1LL*base*base%P;
   }
   return res;
}

int dp[2][N];
inline int find(int x){
   return (x == f[x]) ? x : f[x] = find(f[x]);
}
int ct[N],num[N];
int a[N];

int cur = 1;

int main(){
   scanf("%d",&n);
   fr(i,1,n)scanf("%d",p+i);
   fr(i,1,n)f[i] = i;

   fr(i,1,n){
      if(p[i] == -1){
         cur = 1LL*cur*(n-1)%P;
         continue ;
      }
      int z1 = find(i),z2 = find(p[i]);
      if(z1 == z2)++ct[z1];
      else {
         ct[z1] += ct[z2]+1;
         f[z2] = z1;
      }
   }

   fr(i,1,n){
      ++num[find(i)];
   }

   int tot = 0,ans = 1LL*n*cur%P;
   fr(i,1,n){
      if(f[i] == i){
         if(ct[i] == num[i]){
            add(ans,P-cur);
         } else a[++tot] = num[i];
      }
   }

   dp[0][0] = 1;
   fr(i,1,tot){
      int id = i&1;
      memset(dp[id],0,sizeof dp[id]);
      fr(j,0,i-1){
         add(dp[id][j+1],1LL*dp[id^1][j]*a[i]%P);
         add(dp[id][j],1LL*dp[id^1][j]*(n-1)%P);
      }
   }

   fr(i,1,tot){
      add(ans,P-1LL*(a[i]-1)*pow_mod(n-1,tot-1)%P);
   }

   cur = 1;
   fr(i,2,tot){
      add(ans,P-1LL*dp[tot&1][i]%P*cur%P);
      cur = 1LL*cur*i%P;
   }
   printf("%d\n",ans); return 0;
}
