#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<int(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

template<class T> void read(T &x){
	int f=0; x=0; char ch=getchar();
	for(;!isdigit(ch);ch=getchar()) f|=(ch=='-');
	for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
	if(f) x=-x;
}

const int N=5005,mod=1e9+7;
int f[N][N],pa[N],mark[N],sz[N],s[N];
int n,c,p,con,ans;

int power(int x,int p){
	int res=1;
	for(;p;p>>=1,x=(ll)x*x%mod)
		if(p&1) res=(ll)res*x%mod;
	return res;
}
int mul(int x,int y){
	return (ll)x*y%mod;
}
void add(int &x,int y){
	x=(x+y>=mod?x+y-mod:x+y);
}
int gp(int x){
	return pa[x]==x?x:pa[x]=gp(pa[x]);
}

int main(){
	read(n);
	rep(i,1,n){
		pa[i]=i;
		sz[i]=1;
	}
	rep(i,1,n){
		read(p);
		if(p==-1) continue;
		if(gp(i)==gp(p)){
			mark[gp(i)]=1;
		}
		else{
			sz[gp(i)]+=sz[gp(p)];
			mark[gp(i)]|=mark[gp(p)];
			pa[gp(p)]=gp(i);
		}
	}
	rep(i,1,n){
		if(pa[i]!=i) continue;
		if(!mark[i]) s[++c]=sz[i];
		con++;
	}
	ans=mul(con-c,power(n-1,c));
	f[0][0]=1;
	rep(i,1,c){
		rep(j,0,i-1){
			add(f[i][j],f[i-1][j]);
			add(f[i][j+1],mul(f[i-1][j],s[i]));
		}
	}
	rep(i,1,c){
		int res=s[i]-1;
		res=mul(res,power(n-1,c-1));
		add(ans,res);
	}
	int coef=1;
	rep(i,2,c){
		int res=f[c][i];
		res=mul(res,power(n-1,c-i));
		res=mul(res,coef);
		add(ans,res);
		coef=mul(coef,i);
	}
	ans=(mul(n,power(n-1,c))+mod-ans)%mod;
	cout<<ans<<endl;
	return 0;
}
