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: Wrong Answer)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <ctime>
#include <set>

#ifndef ONLINE_JUDGE
#include "LocalDebug.h"
#else 
#define dbg(...)
#endif

const int N = 5005;
const int P = 1000000007;
int fac[N], inv[N], fnv[N], pw[N];

int comb (int x, int y) {
	if (x == 0) return 0;
	if (x < y) return 0;
	return 1LL * fac[x] * fnv[y] % P * fnv[x-y] % P;
}
int power (int a, int n) {
	int ans = 1;
	while (n > 0) {
		if (n % 2) ans = 1LL * ans * a % P;
		n /= 2;
		a = 1LL * a * a % P;
	}
	return ans;
}

void go () {
	fac[0] = inv[0] = fnv[0] = 1;
	fac[1] = inv[1] = fnv[1] = 1;
	pw[0] = 1; pw[1] = 2;
	for (int i = 2; i < N; ++i) {
		fac[i] = 1LL * fac[i-1] * i % P;
		inv[i] = 1LL * (P - P/i) * inv[P%i] % P;
		fnv[i] = 1LL * fnv[i-1] * inv[i] % P;
		pw[i] = 1LL * pw[i-1] * 2 % P;
	}
	int n; std::cin >> n;
	std::vector <int> a(n), d(n);
	int k = 0;
	for (int i = 0; i < n; ++i) {
		std::cin >> a[i];
		if (a[i] != -1) a[i]--, d[a[i]]++;
		else ++k;
	}
	int use = 0, left = k-1;
	long long ans = 0;
	std::set <std::pair<int,int>> base;
	for (int i = 0; i < n; ++i) {
		if (a[i] != -1) {
			base.insert (std::make_pair(i,a[i]));
			base.insert (std::make_pair(a[i],i));
			continue;
		}
		// link -1 before
		dbg (i, use, left);
		for (int j = 0; j <= use; ++j) { // there are j '-1' choose self
			int c = 1LL * comb(use,j) * power(n-2,use-j) % P;
			dbg (i, j, c);
			ans = (ans + 1LL * (use-j) * c % P * power(n-1,left)) % P;
		}
		dbg (i, ans);
		// link not -1
		ans = (ans + 1LL * (n - d[i] - k) * power(n-1, k-1) % P); 
		use++;
		left--;
	}
	ans = ans + (1LL * (int)(base.size())/2 * power(n-1, k)) % P;
	std::cout << ans << "\n";
}

int main () {
	int T = 1;
	time_t tic = clock();
	while (T--) go();
	time_t toc = clock();
	std::cerr << __FILE__ << " execution time : " << 1000.0 * (toc - tic) / CLOCKS_PER_SEC << "ms\n";
	return 0;
}