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

typedef long long LL;

const int N = 5005;
const int MOD = 1000000007;

int n, po[N], f[N], g[N], h[N], dp[N][N], a[N], m;

int find(int x)
{
	return f[x] == x ? x : f[x] = find(f[x]);
}

int main()
{
	scanf("%d", &n);
	po[0] = 1;
	for (int i = 1; i <= n; i++) f[i] = i, g[i] = 1, po[i] = (LL)po[i - 1] * (n - 1) % MOD;
	int tot = 0;
	for (int i = 1; i <= n; i++)
	{
		int x; scanf("%d", &x);
		if (x == -1) {tot++; continue;}
		if (find(x) == find(i)) h[find(x)] = 1;
		else h[find(x)] ^= h[find(i)], g[find(x)] += g[find(i)], f[find(i)] = find(x);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) if (f[i] == i)
	{
		if (h[i]) (ans += po[tot]) %= MOD;
		else a[++m] = g[i], (ans += (LL)(g[i] - 1) * po[tot - 1] % MOD) %= MOD;
	}
	for (int i = 1; i <= m; i++)
	{
		dp[i][1] = (dp[i - 1][1] + a[i]) % MOD;
		for (int j = 2; j <= i; j++)
			dp[i][j] = (dp[i - 1][j] + (LL)dp[i - 1][j - 1] * (j - 1) % MOD * a[i] % MOD) % MOD;
	}
	for (int i = 2; i <= m; i++) (ans += (LL)dp[m][i] * po[tot - i] % MOD) %= MOD;
	printf("%d\n", ((LL)n * po[tot] % MOD + MOD - ans) % MOD);
	return 0;
}