Problem:
Score : 700 points

Problem Statement

There are N towns numbered 1, 2, \cdots, N.

Some roads are planned to be built so that each of them connects two distinct towns bidirectionally. Currently, there are no roads connecting towns.

In the planning of construction, each town chooses one town different from itself and requests the following: roads are built so that the chosen town is reachable from itself using one or more roads.

These requests from the towns are represented by an array P_1, P_2, \cdots, P_N. If P_i = -1, it means that Town i has not chosen the request; if 1 \leq P_i \leq N, it means that Town i has chosen Town P_i.

Let K be the number of towns i such that P_i = -1. There are (N-1)^K ways in which the towns can make the requests. For each way to make requests, find the minimum number of roads needed to meet all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).

Constraints

2 \leq N \leq 5000

P_i = -1 or 1 \leq P_i \leq N.

P_i \neq i

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \cdots P_N

Output

For each way to make requests, find the minimum number of roads needed to meet all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).

Sample Input 1

4
2 1 -1 3

Sample Output 1

8

There are three ways to make requests, as follows:

Choose P_1 = 2, P_2 = 1, P_3 = 1, P_4 = 3. In this case, at least three roads - for example, (1,2),(1,3),(3,4) - are needed to meet the requests.

Choose P_1 = 2, P_2 = 1, P_3 = 2, P_4 = 3. In this case, at least three roads - for example, (1,2),(1,3),(3,4) - are needed to meet the requests.

Choose P_1 = 2, P_2 = 1, P_3 = 4, P_4 = 3. In this case, at least two roads - for example, (1,2),(3,4) - are needed to meet the requests.

Note that it is not mandatory to connect Town i and Town P_i directly.

The sum of the above numbers is 8.

Sample Input 2

2
2 1

Sample Output 2

1

There may be just one fixed way to make requests.

Sample Input 3

10
2 6 9 -1 6 9 -1 -1 -1 -1

Sample Output 3

527841

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include "bits/stdc++.h"
using namespace std;
const int mod = 1000000000 + 7;
const int maxn = 5e3 + 10;
int p[maxn];
int par[maxn], sub[maxn];
long long fac[maxn];
long long dp[maxn][maxn];
int cmp;
int root(int x) {
	if(x == par[x]) return x;
	return par[x] = root(par[x]);
}
void join(int x, int y) {
	x = root(x);
	y = root(y);
	if(x != y) {
		par[x] = y;
		sub[y] += sub[x];
		cmp--;
	}
}
long long modpow(int b, int e) {
	if(e == 0) return 1;
	if(e & 1) return (modpow(b, e - 1) * b) % mod;
	long long m = modpow(b, e >> 1);
	return (m * m) % mod;
}
int main() {
	int n;
	cin >> n;
	fac[0] = 1;
	for(int i = 1; i <= n; i++) {
		cin >> p[i];
		par[i] = i;
		sub[i] = 1;
		fac[i] = (fac[i - 1] * i) % mod; 
	}
	cmp = n;
	for(int i = 1; i <= n; i++) {
		if(p[i] != -1) join(i, p[i]);
	}
	vector <int> v;
	for(int i = 1; i <= n; i++) {
		if(p[i] == -1) {
			v.push_back(sub[root(i)]);
		}
	}
	dp[0][0] = 1;
	for(int i = 1; i <= v.size(); i++) {
		dp[i][0] = 1;
		for(int j = 1; j <= i; j++) {
			dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] * v[i - 1];
			dp[i][j] %= mod;
		}
	}
	long long ans = (cmp - v.size()) * modpow(n - 1, v.size());
	for(int i = 2; i <= v.size(); i++) {
		long long ways = (dp[v.size()][i] * fac[i - 1]) % mod; 
		ans += ways * modpow(n - 1, v.size() - i);
		ans %= mod;
	}
	for(auto i : v) {
		ans += (i - 1) * modpow(n - 1, v.size() - 1);
		ans %= mod;
	}
	ans = n * modpow(n - 1, v.size()) - ans;
	ans %= mod;
	ans += mod;
	cout << ans % mod << endl;
 	return 0;
}