#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll p=1e9+7;
ll ans,n,fa[5010],a[5010],b[5010],v[5010],f[2][5010],jc[5010],k;
ll find(ll x){
	if(x==fa[x])return x;
	return fa[x]=find(fa[x]);
}
ll ksm(ll x,ll y){
	ll xlh=1;
	while(y){
		if(y&1)xlh=xlh*x%p;
		x=x*x%p;
		y/=2;
	}
	return xlh;
}
int main(){
	ll i,x,y,j,now=0,la,hh=0;
	scanf("%lld",&n);jc[0]=1;
	for(i=1;i<=n;i++){
		jc[i]=jc[i-1]*i%p;
		scanf("%lld",&a[i]);fa[i]=i;
		if(a[i]==-1)k++;
	}
	for(i=1;i<=n;i++)if(a[i]!=-1){
		x=find(i);y=find(a[i]);
		if(x==y)v[x]=1,ans++;
		 else fa[x]=y;
	}
	for(i=1;i<=n;i++){
		x=find(i);
		if(!v[x])b[x]++;
	}
	//sej=ksm(n-1,k);
	ans=(n-ans)*ksm(n-1,k)%p;
	//printf("%lld\n",ans);
	f[0][0]=1;
	for(i=1;i<=n;i++)if(find(i)==i&&!v[i]){
		hh++;
		la=now;now^=1;
		memset(f[now],0,sizeof(f[now]));
		for(j=0;j<=n;j++)if(f[la][j]){
			f[now][j]=(f[now][j]+f[la][j])%p;
			f[now][j+1]=(f[now][j+1]+f[la][j]*b[i]%p)%p;
		}
	}
	f[now][1]=(f[now][1]-hh+p)%p;
	for(i=1;i<=k;i++)ans=(ans-f[now][i]*ksm(n-1,k-i)%p*jc[i-1]%p+p)%p;
	printf("%lld",ans);
}