#include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reversed_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
using namespace std;

inline int read()
{
	int x = 0,f = 1; char ch = getchar();
	for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
	for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
	return x*f;
}

const int mxN = 5000;
const int P = 1e9+7;
llong fact[mxN+3],facti[mxN+3],inv[mxN+3];

void updsum(llong &x,llong y) {x+=y-P,x+=(x>>31)&P;}
llong quickpow(llong x,llong y)
{
	llong cur = x,ret = 1ll;
	for(int i=0; y; i++)
	{
		if(y&(1ll<<i)) {y-=(1ll<<i); ret = ret*cur%P;}
		cur = cur*cur%P;
	}
	return ret;
}
llong mulinv(llong x) {return quickpow(x,P-2);}
llong comb(llong x,llong y) {return x<0||y<0||x<y?0ll:fact[x]*facti[y]%P*facti[x-y]%P;}
void initfact(int n)
{
	fact[0] = 1ll; for(int i=1; i<=n; i++) fact[i] = fact[i-1]*i%P;
	facti[n] = quickpow(fact[n],P-2); for(int i=n-1; i>=0; i--) facti[i] = facti[i+1]*(i+1ll)%P;
	for(int i=1; i<=n; i++) inv[i] = facti[i]*fact[i-1]%P;
}

int n,en,tot;
int a[mxN+3];
struct Edge
{
	int v,nxt;
} e[mxN*2+3]; int fe[mxN+3];
int cnt[mxN+3];
int uf[mxN+3],sz[mxN+3],sz2[mxN+3];
llong f[2][mxN+3];

int findfa(int u) {return u==uf[u]?u:uf[u]=findfa(uf[u]);}
void unionfa(int u,int v)
{
	int x = findfa(u),y = findfa(v);
	if(x!=y) {uf[x] = y; sz[y] += sz[x]; sz2[y] += sz2[x];} sz[y]--;
}

int main()
{
	initfact(mxN);
	n = read();
	for(int i=1; i<=n; i++) uf[i] = i,sz[i] = 1,sz2[i] = 1;
	for(int i=1; i<=n; i++) {a[i] = read(); if(a[i]!=-1) {unionfa(i,a[i]);} else {tot++;}}
	llong ans = 0ll; int cur = 0; f[0][0] = 1ll;
	for(int i=1; i<=n; i++)
	{
		if(findfa(i)==i)
		{
			memset(cnt,0,sizeof(int)*(n+2));
			if(sz[i]==0)
			{
				updsum(ans,quickpow(n-1,tot));
			}
			else
			{
				cur ^= 1; memset(f[cur],0,sizeof(f[cur]));
				for(int j=0; j<=tot; j++) if(f[cur^1][j])
				{
					updsum(f[cur][j],f[cur^1][j]);
					updsum(f[cur][j+1],f[cur^1][j]*sz2[i]%P);
				}
			}
		}
	}
	for(int i=1; i<=n; i++)
	{
		llong tmp = quickpow(n-1,tot-i)*f[cur][i]%P*fact[i-1]%P;
		updsum(ans,tmp);
	}
	updsum(ans,P-tot*quickpow(n-1,tot-1)%P);
	llong ans2 = 0ll;
	for(int i=3; i<=tot; i++) {updsum(ans2,comb(tot,i)*fact[i-1]%P);}
//	ans = (ans-ans2*mulinv(2ll)%P+P)%P;
	ans = (quickpow(n-1,tot)*n%P-ans+P)%P;
	printf("%lld\n",ans);
	return 0;
}