#include <bits/stdc++.h>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
		freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 5005;
int n, p[N], fac[N];
void add(int &x, int y) {
	x += y; if (x >= mod) x -= mod;
}
void rem(int &x, int y) {
	x -= y;
	if (x < 0) x += mod;
}
int mul(int x, int y) {
	return 1ll * x * y % mod;
}
int fpow(int a, int p) {
	if (!p) return 1;
	int tmp = fpow(a, p >> 1);
	tmp = mul(tmp, tmp);
	if (p & 1) tmp = mul(tmp, a);
	return tmp;
}
struct DSU {
	int p[N], sz[N];
	void init(int n) {
		fw (i, 0, n) {
			p[i] = i;
			sz[i] = 1;
		}
	}
	int getp(int u) {
		return u == p[u] ? u : p[u] = getp(p[u]);
	}
	bool joint(int u, int v) {
		u = getp(u), v = getp(v);
		if (u == v) return false;
		if (sz[u] < sz[v]) swap(u, v);
		p[v] = u;
		sz[u] += sz[v];
		return true;
	}
	int getSize(int u) {
		return sz[getp(u)];
	}
} dsu;
int dp[N][N];
signed main() {
	#ifdef BLU
	in("blu.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	fac[0] = 1;
	fw (i, 1, N) fac[i] = mul(fac[i - 1], i);
	cin >> n;
	dsu.init(n);
	int cntCycles = 0, cntUndecided = 0;
	fw (i, 0, n) {
		cin >> p[i];
		if (p[i] != -1) p[i]--;
		if (p[i] >= 0) {
			bool success = dsu.joint(i, p[i]);
			if (!success) cntCycles++;
		} else cntUndecided++;
	}
	vector<int> trees;
	fw (i, 0, n) if (p[i] == -1) {
		trees.pb(dsu.getSize(i));
	}
	int ans = mul(n - cntCycles, fpow(n - 1, cntUndecided));
	dp[0][0] = 1;
	fw (i, 0, trees.size()) fw (j, 0, i + 1) if (dp[i][j]) {
		add(dp[i + 1][j], dp[i][j]);
		add(dp[i + 1][j + 1], mul(dp[i][j], trees[i]));
	}
	fw (j, 1, cntUndecided + 1) {
		int ways = mul(dp[cntUndecided][j] - (j == 1) * cntUndecided, fac[j - 1]);
		int other = cntUndecided - j;
//		cout << "ways = " << ways << " other = " << other << "\n";
		rem(ans, mul(ways, fpow(n - 1, other)));
	}
	cout << ans;
	return 0;
}
