#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#ifdef DEBUG
#define display(x) cerr << #x << " = " << x << endl;
#define displaya(a, st, n)\
	{cerr << #a << " = {";\
	for(int qwq = (st); qwq <= (n); ++qwq) {\
		if(qwq == (st)) cerr << a[qwq];\
		else cerr << ", " << a[qwq];\
	} cerr << "}" << endl;}
#define displayv(v) displaya(v, 0, (int)(v).size() - 1)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define display(x) ;
#define displaya(a, st, n) ;
#define displayv(v) ;
#define eprintf(...) if(0) fprintf(stderr, "...")
#endif
template<typename T> bool chmin(T &a, const T &b) { return a > b ? a = b, true : false; }
template<typename T> bool chmax(T &a, const T &b) { return a < b ? a = b, true : false; }
template<typename A, typename B>
ostream& operator << (ostream& out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}

const int maxN = 5000 + 5;
const LL P = 1000000007;
LL qpow(LL a, LL b) {
	LL r = 1;
	while(b) {
		if(b & 1) (r *= a) %= P;
		(a *= a) %= P; b >>= 1;
	}
	return r;
}
LL fac[maxN], ifac[maxN];
LL C(int n, int k) {
	if(k < 0 || k > n) return 0;
	return fac[n] * ifac[k] % P * ifac[n - k] % P;
}
int n;
int pa[maxN], sz[maxN], ig[maxN];
int findset(int x) {
	return pa[x] == x ? x : pa[x] = findset(pa[x]);
}

LL f[maxN];

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
#endif
	cin >> n;
	for(int i = 1; i <= n; ++i) pa[i] = i, sz[i] = 1, ig[i] = 0;
	for(int i = 1; i <= n; ++i) {
		int p;
		cin >> p;
		if(p >= 1) {
			int x = findset(i), y = findset(p);
			if(x != y) {
				pa[y] = x;
				ig[x] |= ig[y];
				sz[x] += sz[y];
			}
		} else {
			int x = findset(i);
			ig[x] = 1;
		}
	}
	displaya(pa, 1, n);
	displaya(ig, 1, n);
	vector<int> a;
	for(int i = 1; i <= n; ++i) if(pa[i] == i) {
		if(ig[i]) a.push_back(sz[i]);
	}
	displayv(a);
	
	LL ans = 0;
	for(int i = 1; i <= n; ++i) if(pa[i] == i) {
		if(!ig[i]) (ans += qpow(n - 1, a.size())) %= P;
	}
	display(ans);
	
	fac[0] = 1;
	for(int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % P;
	ifac[n] = qpow(fac[n], P - 2);
	for(int i = n - 1; i >= 0; --i) ifac[i] = ifac[i + 1] * (i + 1) % P;
	
	memset(f, 0, sizeof(f));
	f[0] = 1;
	for(int i = 0; i < (int)a.size(); ++i) {
		for(int j = i + 1; j > 0; --j)
			(f[j] = f[j] * (n - 1) + f[j - 1] * a[i]) %= P;
		f[0] = f[0] * (n - 1) % P;
	}
	for(int k = 2; k <= (int)a.size(); ++k) (ans += f[k] * fac[k - 1]) %= P;
	for(int i = 0; i < (int)a.size(); ++i) {
		(ans += (a[i] - 1) * qpow(n - 1, a.size() - 1)) %= P;
	}
	display(ans);
//	display(qpow(n - 1, a.size()) * n);
	display(ans);
	ans = (qpow(n - 1, a.size()) * n - ans + P) % P;
	cout << ans << endl;
	return 0;
}
