//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#pragma warning(disable:4786)
#pragma warning(disable:4996)
#include <ctime>
#include<list>
#include <numeric>
#include<bitset>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>
#include<cmath>
#include<stack>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include <array>
using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) )
#define S(X) ( (X) * (X) )
#define SZ(V) (int )V.size()
#define FORN(i, n) for(int i = 0; i < n; i++)
#define FORAB(i, a, b) for(int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C)  ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))

typedef long long int LL;
//typedef __int128 LLL;
typedef long long LLL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VP;
typedef vector<double> VD;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef long double ld;

#define MAXN 1000006
//#define MAXN 1006

const LL MOD = 1000000007;
//const LL MOD = 998244353;
const LL INF = 2000000000000000001LL; //2e18 + 1

int n;
VI V[5003];
int vis[5003];

PII dfs(int at) {
	int vv = 1, e = SZ(V[at]);
	vis[at] = 1;
	for (int v : V[at]) {
		if (vis[v]) continue;
		PII a = dfs(v);
		vv += a.first;
		e += a.second;
	}
	return { vv, e };
}

void solve(int ks) {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		int v;
		scanf("%d", &v);
		if (v != -1) {
			v--;
			V[i].push_back(v);
			V[v].push_back(i);
		}
	}

	VI Full, Less;
	for (int i = 0; i < n; i++) {
		if (vis[i]) continue;
		PII a = dfs(i);
		if (a.second == 2 * a.first) Full.push_back(a.first);
		else Less.push_back(a.first);
	}

	VL Mul(SZ(Less) + 1, 0);
	Mul[0] = 1;
	for (int l : Less) {
		VL nMul(SZ(Less) + 1, 0);
		for (int i = 0; i < SZ(Mul); i++) {
			nMul[i] = (nMul[i] + Mul[i]) % MOD;
			if (i + 1 < SZ(nMul)) nMul[i + 1] = (nMul[i + 1] + Mul[i] * l) % MOD;
		}
		Mul.swap(nMul);
	}

	VL fact(SZ(Less) + 1, 0);
	VL nMinus1(SZ(Less) + 1, 0);
	fact[0] = nMinus1[0] = 1;
	for (int i = 1; i < SZ(fact); i++) {
		fact[i] = (i * fact[i - 1]) % MOD;
		nMinus1[i] = (nMinus1[i - 1] * (n - 1)) % MOD;
	}

	LL ret = 0;
	for (int i = 0; i < SZ(Mul); i++) {
		if (i == 0) {
			ret = (ret + SZ(Full) * nMinus1[SZ(Less)]) % MOD;
		}
		else if (i == 1) {
			LL sum = 0;
			for (LL l : Less) sum += l - 1;
			ret = (ret + sum * nMinus1[SZ(Less) - 1]) % MOD;
		}
		else {
			ret = (ret + ((Mul[i] * fact[i - 1]) % MOD) * nMinus1[SZ(Less) - i]) % MOD;
		}
	}

	ret = (nMinus1[SZ(Less)] * n + MOD - ret) % MOD;
	printf("%lld\n", ret);

}

void gen() {
}

int main()
{
	double start_time = clock();
#ifdef LOCAL
	freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
	// freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
	//	freopen("out.out", "w", stdout);
#endif

	gen();

	if (0) {
		int T;
		scanf("%d", &T);
		//AIN(T, 1, 5000);
		for (int ks = 1; ks <= T; ks++) {
			solve(ks);
			if (ks % 1 == 0) fprintf(stderr, "%d done\n", ks);
		}
	}
	else {
		solve(0);
	}

	double end_time = clock();
	fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);

	return 0;
}


Measured memory_bytes: 4168