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 <stdio.h>
#include <stdlib.h>

#define Mod 1000000007

typedef struct List {
	struct List *next;
	int v;
} list;

int main()
{
	int i, N, P[5001];
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d", &(P[i]));
	
	list **adj = (list**)malloc(sizeof(list*) * (N + 1)), *d = (list*)malloc(sizeof(list) * N * 2);
	for (i = 0; i < N; i++) {
		if (P[i+1] == -1) continue;
		d[i*2].v = P[i+1];
		d[i*2+1].v = i + 1;
		d[i*2].next = adj[i+1];
		d[i*2+1].next = adj[P[i+1]];
		adj[i+1] = &(d[i*2]);
		adj[P[i+1]] = &(d[i*2+1]);
	}
	
	list *p;
	int j, k, K, comp[5001] = {}, q[5001], head, tail, size[5001] = {};
	for (i = 1, k = 1; i <= N; i++) {
		if (P[i] != -1) continue;
		comp[i] = k;
		q[0] = i;
		for (head = 0, tail = 1; head < tail; head++) {
			for (p = adj[q[head]]; p != NULL; p = p->next) {
				if (comp[p->v] == 0) {
					comp[p->v] = k;
					q[tail++] = p->v;
				}
			}
		}
		size[k++] = tail;
	}
	for (i = 1, K = k - 1; i <= N; i++) {
		if (comp[i] != 0) continue;
		comp[i] = k;
		q[0] = i;
		for (head = 0, tail = 1; head < tail; head++) {
			for (p = adj[q[head]]; p != NULL; p = p->next) {
				if (comp[p->v] == 0) {
					comp[p->v] = k;
					q[tail++] = p->v;
				}
			}
		}
		k++;
	}
	
	int l;
	long long ans = N - k + 1 + K, dp[2][5001] = {{1}}, N_pow[5001] = {1}, fact;
	for (i = 1; i <= K; i++) {
		l = i % 2;
		N_pow[i] = N_pow[i-1] * (N - 1) % Mod;
		dp[l][0] = dp[1-l][0] * size[i] % Mod;
		for (j = 1; j <= i; j++) dp[l][j] = (dp[1-l][j-1] + dp[1-l][j] * size[i]) % Mod;
	}
	ans = (ans * N_pow[K] - (dp[K%2][K-1] - K) * N_pow[K-1] % Mod) % Mod;
	for (i = 2, fact = 1; i <= K; i++, fact = fact * (i - 1) % Mod) {
		ans = (ans + Mod - ((dp[K%2][K-i] * fact % Mod) * N_pow[K-i] % Mod)) % Mod;
	}
	
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}