/* cerberus97 - Hanit Banga */

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

#define pb push_back
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int N = 5e3 + 10, mod = 1e9 + 7;

int add(int a, int b) {
	a += b;
	if (a >= mod) a -= mod;
	return a;
}

int sub(int a, int b) {
	a -= b;
	if (a < 0) a += mod;
	return a;
}

int mul(ll a, int b) {
	return (a * b) % mod;
}

int pwr(int x, int e) {
	int res = 1;
	while (e) {
		if (e & 1) {
			res = mul(res, x);
		}
		x = mul(x, x);
		e /= 2;
	}
	return res;
}

int par[N], sz[N];

int get_root(int u);
bool merge(int u, int v);

int main() {
	fast_cin();
	int n; cin >> n;
	vector<int> p(n);
	int e_cycles = 0;
	for (int i = 0; i < n; ++i) {
		sz[i] = 1;
		par[i] = i;
	}
	for (int i = 0; i < n; ++i) {
		cin >> p[i];
		if (p[i] != -1) {
			--p[i];
			e_cycles += !merge(i, p[i]);
		}
	}
	vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
	dp[0][0] = 1;
	int k = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j <= k; ++j) {
			dp[i + 1][j] = add(dp[i + 1][j], dp[i][j]);
			if (p[i] == -1) {
				dp[i + 1][j + 1] = add(dp[i + 1][j + 1], mul(sz[get_root(i)], dp[i][j]));
			}
		}
		k += (p[i] == -1);
	}
	vector<int> fact(n + 1, 1);
	for (int i = 2; i <= n; ++i) {
		fact[i] = mul(i, fact[i - 1]);
	}
	int tot = pwr(n - 1, k), cycles = mul(e_cycles, tot);
	cycles = add(cycles, mul(sub(dp[n][1], k), pwr(n - 1, k - 1)));
	for (int x = 2; x <= k; ++x) {
		cycles = add(cycles, mul(mul(fact[x - 1], dp[n][x]), pwr(n - 1, k - x)));
	}
	int ans = sub(mul(tot, n - 1), sub(cycles, tot));
	cout << ans << endl;
}

int get_root(int u) {
	if (u != par[u]) {
		par[u] = get_root(par[u]);
	}
	return par[u];
}

bool merge(int u, int v) {
	u = get_root(u);
	v = get_root(v);
	if (u == v) {
		return false;
	} else {
		sz[u] += sz[v];
		par[v] = u;
		return true;
	}
}