/*
ЗАПУСКАЕМ 
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░ 
 */
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>


using namespace std;
template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;}
template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;}
#define files(FILENAME) read(FILENAME); write(FILENAME)
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define left left228
#define right right228
#define y1 y1228
#define mp make_pair
#define pb push_back
#define y2 y2228
#define rank rank228
using ll = long long;
using ld = long double;
const string FILENAME = "input";
const int MAXN = 5028;
const int Mod = 1000000007;



int sum(int a, int b) {
	return (a + b >= Mod ? a + b - Mod: a + b);
}


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


int powm(int a, int b) {
	int res = 1;
	while (b > 0) {
		if (b & 1) {
			res = mul(res, a);
		}
		b >>= 1;
		a = mul(a, a);
	}
	return res;
}


int n;
int p[MAXN];
int c[MAXN][MAXN];
vector<int> g[MAXN];
bool used[MAXN];
int cntv, cnte;


void dfs(int u) {
	cntv++;
	cnte += sz(g[u]);
	used[u] = true;
	for (auto h: g[u]) {
		if (!used[h]) {
			dfs(h);
		}
	}
}


int dp[MAXN][MAXN];
int fact[MAXN];


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
 	//read(FILENAME);
	cin >> n;
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= i; j++) {
			if (i == j || j == 0) {
				c[i][j] = 1;
			} else {
				c[i][j] = sum(c[i - 1][j], c[i - 1][j - 1]);
			}
		}
	}
	fact[0] = 1;
	for (int i = 1; i <= n; i++) {
		fact[i] = mul(fact[i - 1], i);
	}
	for (int i = 0; i < n; i++) {
		cin >> p[i];
	}
	int k = 0;
	for (int i = 0; i < n; i++) {
		if (p[i] == -1) {
			k++;
		} else {
			p[i]--;
			g[i].pb(p[i]);
			g[p[i]].pb(i);
		}
	}
	int tp = powm(n - 1, k);
	int ans = mul(tp, n);
	vector<int> st;
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			cntv = 0;
			cnte = 0;
			dfs(i);
			cnte /= 2;
			if (cnte == cntv) {
				ans = sum(ans, Mod - tp);
			} else {
				//cout << cntv << endl;
				st.pb(cntv);
			}
		}
	}
	dp[0][0] = 1;
//	cout << k << ' ' << sz(st) << endl;
	for (int i = 0; i < sz(st); i++) {
		if (st[i] > 1) {
			ans = sum(ans, Mod - mul(powm(n - 1, k - 1), st[i] - 1));
		}
		for (int j = 0; j <= i; j++) {
			dp[i + 1][j] = sum(dp[i + 1][j], mul(dp[i][j], n - 1));
			dp[i + 1][j + 1] = sum(dp[i + 1][j + 1], mul(dp[i][j], st[i]));
		} 
	}
	for (int j = 2; j <= sz(st); j++) {
		int cnt = dp[sz(st)][j];
		cnt = mul(cnt, fact[j - 1]);
		ans = sum(ans, Mod - cnt);
	}
	cout << ans << '\n';
	return 0; 	
}

Measured memory_bytes: 137552