#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define Debug(v) for (auto i:v) printf("%lld ",i);puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
typedef vector<int> poly;
inline ll read(){
    ll x=0,f=1;char c=getchar();
    while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
    if (c=='-') f=-1,c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

const int N = 5010, mod = 1e9+7;
int n,cnt,cir,a[N],f[N][N];

int fa[N],siz[N],flag[N];
inline int Find(int x){
	return fa[x]==x?x:fa[x]=Find(fa[x]);
}

inline int power(int a,int b){
	int ret=1;
	for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) ret=1ll*ret*a%mod;
	return ret;
}

int main(){
	n=read();
	For(i,1,n) fa[i]=i;
	For(i,1,n){
		a[i]=read(),cnt+=(a[i]==-1);
		if (a[i]==-1) continue;
		int x=Find(i),y=Find(a[i]);
		if (x==y) ++cir,flag[x]=1;
			else fa[x]=y,flag[y]|=flag[x];
	}
	For(i,1,n) siz[Find(i)]++;
	vector<int>v;
	For(i,1,n) if (Find(i)==i&&!flag[i]) v.pb(siz[i]);
	f[0][0]=1;
	For(i,1,siz(v)){
		f[i][0]=1ll*f[i-1][0]*(n-1)%mod;
		For(j,1,i) f[i][j]=(1ll*f[i-1][j]*(n-1)+1ll*f[i-1][j-1]*(j==1?1:j-1)%mod*v[i-1])%mod; 
	}
	int ans=1ll*n*power(n-1,cnt)%mod;
	For(i,2,siz(v)) ans=(ans+mod-f[siz(v)][i])%mod;
	for (auto i:v) ans=(ans+mod-1ll*(i-1)*power(n-1,siz(v)-1)%mod)%mod;
	ans=(ans+mod-1ll*cir*power(n-1,cnt)%mod)%mod;
	printf("%d\n",ans);
}
