There are N towns numbered 1, 2, \cdots, N.

Some roads are planned to be built so that each of them connects two distinct towns bidirectionally. Currently, there are no roads connecting towns.

In the planning of construction, each town chooses one town different from itself and requests the following: roads are built so that the chosen town is reachable from itself using one or more roads.

These requests from the towns are represented by an array P_1, P_2, \cdots, P_N. If P_i = -1, it means that Town i has not chosen the request; if 1 \leq P_i \leq N, it means that Town i has chosen Town P_i.

Let K be the number of towns i such that P_i = -1. There are (N-1)^K ways in which the towns can make the requests. For each way to make requests, find the minimum number of roads needed to meet all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).

Constraints

* 2 \leq N \leq 5000
* P_i = -1 or 1 \leq P_i \leq N.
* P_i \neq i
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
P_1 P_2 \cdots P_N


Output

For each way to make requests, find the minimum number of roads needed to meet all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).

Examples

Input

4
2 1 -1 3


Output

8


Input

2
2 1


Output

1


Input

10
2 6 9 -1 6 9 -1 -1 -1 -1


Output

527841
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair < int , int > pii;
#define mpr make_pair
#define FS first
#define SC second
#define PB push_back
template < typename T > T MAX(T a,T b){return (a>b)?a:b;}
template < typename T > T MIN(T a,T b){return (a<b)?a:b;}
template < typename T > void UMAX(T &a,T b){a=(a>b?a:b);}
template < typename T > void UMIN(T &a,T b){a=(a<b?a:b);}
LL readint(){
	char c=getchar();
	LL ret=0ll;
	while(!(c>='0' && c<='9')) c=getchar();
	while(c>='0' && c<='9'){
		ret=ret*10ll+(LL)(c-'0');
		c=getchar();
	}
	return ret;
}
void putint(LL v){
	if(!v){
		putchar('0');
		return;
	}
	if(v>=10ll) putint(v/10ll);
	putchar('0'+(v%10ll));
}
const int MOD=1000000007;
int n,f[5005],CNT;
namespace DSU{
	int fa[5005],sz[5005];
	void init(){
		int i;
		for(i=0;i<n;++i) fa[i]=i,sz[i]=1;
	}
	int anc(int x){
		return fa[x]==x?x:fa[x]=anc(fa[x]);
	}
	void unite(int x,int y){
		x=anc(x);
		y=anc(y);
		if(x==y){
			++CNT;
			return;
		}
		fa[x]=y;
		sz[y]+=sz[x];
	}
}
int dp[5005];
int powM(int V,int T=MOD-2){
	int R=1;
	while(T){
		if(T&1) R=(LL)R*V%MOD;
		T>>=1;
		V=(LL)V*V%MOD;
	}
	return R;
}
int main(){
	int i,j,k,sch=1;
	n=readint();
	DSU::init();
	for(i=0;i<n;++i){
		scanf("%d",f+i);
		if(~f[i]) DSU::unite(--f[i],i);
	}
	vector < int > vs;
	for(i=0;i<n;++i){
		if(!~f[i]){
			vs.push_back(DSU::sz[DSU::anc(i)]);
			sch=(LL)sch*(n-1)%MOD;
		}
	}
	int res=(LL)CNT*sch%MOD,fac=1,invopt=powM(n-1);
	dp[0]=1;
	for(i=0;i<(int)vs.size();++i){
		res+=(LL)sch*invopt%MOD*(vs[i]-1)%MOD;
		res%=MOD;
		for(j=i+1;j>0;--j){
			dp[j]=((LL)dp[j-1]*vs[i]+(LL)dp[j]*(n-1))%MOD;
		}
		dp[0]=(LL)dp[0]*(n-1)%MOD;
	}
	for(i=2;i<=(int)vs.size();++i){
		res=(res+(LL)fac*dp[i])%MOD;
		fac=(LL)fac*i%MOD;
	}
//	printf("%d %d %d\n",res,sch,n);
	printf("%lld\n",((LL)n*sch+MOD-res)%MOD);
	return 0;
}
/*
 * 代码框架
 * 0. 计算(n-1)^k
 * 1. 实现DSU，不断加入已有的边，若碰到冗余的加边则记录环的出现。
 * 2. 对每个-1的点求得其所在连通分量的大小，丢进vector
 * 3. dp[i][j]：前i个元素中，选j个乘起来的乘积之和
 * 4. 计算阶乘，枚举环长，sigma(圆排列个数*dp)
*/