#include <bits/stdc++.h>
using namespace std;

const int MAXN = 5005;
const int MOD = 1000000007;

long long modpow(long long r, long long p){
	if(p == 0)return 1;
	if(p % 2 == 1)return r * modpow(r, p - 1) % MOD;
	
	long long x = modpow(r, p / 2);
	return x * x % MOD;
}

int n, p[MAXN];
vector <int> g[MAXN];
bool vis[MAXN];

long long dp[MAXN][2 * MAXN], fact[MAXN];

int sz, col;
void dfs(int u){
	sz++;
	col += (p[u] == -1);
	vis[u] = true;
	
	for(int v : g[u]){
		if(vis[v])continue;
		dfs(v);
	}
}

int main(){
	scanf("%d", &n);
	
	int cnt = 0;
	for(int i = 0; i < n; i++){
		scanf("%d", &p[i]);
		
		if(p[i] != -1){
			g[i].push_back(p[i] - 1);
			g[p[i] - 1].push_back(i);
		}else{
			cnt++;
		}
	}
	
	long long res = 0;
	
	vector <int> z, w;
	
	for(int i = 0; i < n; i++){
		if(vis[i])continue;
		
		sz = 0;
		col = 0;
		dfs(i);
		
		if(col == 0){
			w.push_back(sz);
		}else{
			z.push_back(sz);
		}
	}
	int a = w.size(), b = z.size();
	
	for(int i = 0; i <= n; i++){
		if(i == 0)fact[i] = 1;
		else fact[i] = i * fact[i - 1] % MOD;
	}
	
	for(int i = b; i >= 0; i--)
	for(int cnt = 0; cnt <= n; cnt++){
		if(i == b){
			if(cnt >= 2){
				dp[i][cnt] = fact[cnt - 1];
			}
		}else{
			dp[i][cnt] = ((n - 1) * dp[i + 1][cnt] + z[i] * dp[i + 1][cnt + 1]) % MOD;
		}
	}
	res = (res + dp[0][0]) % MOD;

	for(int i = 0; i < b; i++)
		res = (res + (z[i] - 1) * modpow(n - 1, b - 1)) % MOD;
	
	res = ((n - a) * modpow(n - 1, cnt) - res + MOD) % MOD;
	printf("%lld\n", res);
	
	return 0;
}
