#include<bits/stdc++.h>
#define ll long long
#define mo 1000000007
#define N 5010
using namespace std;
int fa[N],a[N],n,sum,s2,si[N],b[N],nb;
ll ans,dp[N][N],jie[N];
int que(int x){if (fa[x]!=x)fa[x]=que(fa[x]);return fa[x];}
ll po(ll x,ll y){ll z=1;while (y){if (y%2)z=x*z%mo;x=x*x%mo;y/=2;}return z;}
void jb(int x,int y){
	int nx=que(x),ny=que(y);
	if (nx!=ny)fa[nx]=ny,si[ny]+=si[nx];else sum++;
}
int main(){
	cin>>n;
	for (int i=1;i<=n;i++)fa[i]=i,si[i]=1;
	jie[0]=1;
	for (int i=1;i<=n;i++)jie[i]=jie[i-1]*i%mo;
	for (int i=1;i<=n;i++){
		cin>>a[i];
		if (a[i]!=-1)jb(i,a[i]);
	}
	for (int i=1;i<=n;i++)if (a[i]==-1){s2++;b[++nb]=si[que(i)];}
	ans=(n-sum)*po(n-1,s2)%mo;
	for (int i=1;i<=nb;i++){
		if (b[i]>1)ans=(ans+(b[i]-1)*(mo-po(n-1,s2-1)))%mo;
	}
	dp[0][0]=1;
	for (int i=1;i<=nb;i++){
		for (int j=0;j<i;j++){
			dp[i][j]=(dp[i][j]+dp[i-1][j])%mo;
			dp[i][j+1]=(dp[i][j+1]+dp[i-1][j]*b[i])%mo;
		}
	}
	for (int i=2;i<=nb;i++)ans=(ans+(dp[nb][i]*(mo-po(n-1,s2-i))%mo)*jie[i-1])%mo;
	cout<<ans<<endl;
	return 0;
}