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

const int N = 5500;
const int MOD = 1e9 + 7;

vector<int> con[N];
int a[N], vis[N], pw[N];

int dfs(int u) {
	int sz = 1;
	vis[u] = 1;
	for (int v : con[u]) if (!vis[v]) sz += dfs(v);
	return sz;
}

int dp[N];

int main() {
	
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n, k = 0; cin >> n;
	
	pw[0] = 1;
	for (int i = 1; i <= n; i++) pw[i] = (long long)pw[i - 1] * (n - 1) % MOD; 
	
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (a[i] != -1) con[i].push_back(a[i]), con[a[i]].push_back(i);
		else k++;
	}
	vector<int> V;
	V.push_back(0);
	for (int i = 1; i <= n; i++) if (!vis[i] && a[i] == -1) {
		V.push_back(dfs(i));
	}
	int Vn = V.size() - 1;
	int ans = (long long)n * pw[k] % MOD;
	int xxx = 0;
	for (int i = 1; i <= n; i++) if (!vis[i]) {
		xxx++;
		dfs(i);
	}
	ans -= (long long)xxx * pw[k] % MOD;
	if (ans < 0) ans += MOD;
	dp[0] = 1;
	for (int i = 1; i <= Vn; i++) {
		for (int j = Vn; j >= 1; j--) {
			dp[j] += (long long)dp[j - 1] * max(j - 1, 1) % MOD * V[i] % MOD;
			if (dp[j] >= MOD) dp[j] -= MOD;
		}
	}
	
	for (int i = 1; i <= Vn; i++) {
		int xx = dp[i] - (i == 1) * Vn;
		if (xx < 0) xx += MOD;
		ans -= (long long)xx * pw[Vn - i] % MOD;
		if (ans < 0) ans += MOD;
	}
	
	cout << ans << "\n";
	
	return 0;
}
