#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define SZ(x) ((int)x.size()-1)
#define ms(a,b) memset(a,b,sizeof a)
#define F(i,a,b) for (int i=(a);i<=(b);++i)
#define DF(i,a,b) for (int i=(a);i>=(b);--i)
using namespace std;
inline int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=1e5+10,mod=1e9+7;
#define int LL
LL fast(LL x,LL p){
	LL ret=1;
	for (;p;p>>=1,x=x*x%mod) if (p&1) ret=ret*x%mod;
	return ret;
}
int f[M],s[M],si[M],n;
bool vis[M];
LL Fd(int x){
	if (f[x]==x) return x;
	return f[x]=Fd(f[x]);
}
int dp[M],a[M],ct,cs;
signed main(){
	n=read();
	F(i,1,n) f[i]=i; 
	F(i,1,n){
		a[i]=read();
		if (a[i]!=-1) {
			int u=Fd(i),v=Fd(a[i]);
			if (u!=v) f[u]=v; 
		}
	}
	F(i,1,n){
		if (!s[Fd(i)]) ct++; 
		s[Fd(i)]++;
		if (a[i]==-1) si[Fd(i)]++,cs++;
	}
	int block=ct-cs;
	LL tot=fast(n-1,cs),ans=1ll*tot*n%mod,tbl=1ll*block*tot%mod,sv=0; 
	dp[0]=1;
	F(i,1,n){
		if (vis[Fd(i)] || !si[Fd(i)]) continue;
		int x=Fd(i); vis[x]=1; sv=(sv+(s[x]-1)*fast(n-1,cs-1))%mod;
		DF(i,n,1){
			dp[i]=(dp[i]+1ll*dp[i-1]*s[x])%mod;
		}
	}
	LL fac=1;
	F(i,2,cs){
		fac=1ll*fac*(i-1)%mod;
		sv=(sv+1ll*dp[i]*fac%mod*fast(n-1,cs-i))%mod; 
	}
//	cout<<tot<<" "<<sv<<" "<<tbl<<"   sb250\n";
	cout<<(ans-tbl-sv+mod)%mod<<"\n";
	return 0;
}


