#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)x.size())
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
typedef long long LL;
typedef pair<int,int> pa;
typedef vector<int> vec;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
const int MO=1e9+7;
int fa[500010],siz[500010],fy,n,m,ltk,p[500010],a[500010];
LL ans,f[5010][5010],fac[2000010],inv[2000010],P[2000010];
LL pw(LL x,LL y){
	LL t=1;
	for (;y;y>>=1){
		if (y&1) t=t*x%MO;
		x=x*x%MO;
	}
	return t;
}
LL C(int x,int y){
	return fac[x]*inv[y]%MO*inv[x-y]%MO;
}
int getf(int x){
	return fa[x]==x?x:fa[x]=getf(fa[x]);
}
int main(){
	fac[0]=1;
	FOR(i,1,2000000) fac[i]=fac[i-1]*i%MO;
	inv[2000000]=pw(fac[2000000],MO-2);
	FORD(i,1999999,0) inv[i]=inv[i+1]*(i+1)%MO;
	cin>>n;
	P[0]=1;
	FOR(i,1,n) P[i]=P[i-1]*(n-1)%MO;
	FOR(i,1,n) getint(p[i]);
	FOR(i,1,n) fa[i]=i;
	ltk=n;
	FOR(i,1,n){
		if (p[i]==-1){++fy;continue;}
		int x=i,y=p[i];
		int X=getf(x),Y=getf(y);
		if (X==Y) continue;
		--ltk;
		fa[X]=Y;
	}
	FOR(i,1,n) siz[getf(i)]++;
	FOR(i,1,n) if (p[i]==-1){
		a[++m]=siz[getf(i)];
	}
	f[0][0]=1;
	FOR(i,0,m-1) FOR(j,0,i) if (f[i][j]){
		(f[i+1][j]+=f[i][j])%=MO;
		(f[i+1][j+1]+=f[i][j]*a[i+1])%=MO;
	}
	ans=(n-ltk+fy)*P[fy]%MO;
	FOR(j,2,m) if (f[m][j]){
		(ans-=f[m][j]*fac[j-1]%MO*P[fy-j])%=MO;
	}
	FOR(i,1,m) (ans-=(a[i]-1)*P[fy-1])%=MO;
	ans=(ans%MO+MO)%MO;
	cout<<ans<<endl;
	return 0;
}