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<cstdio>
#include<vector>
int p[5005];
std::vector<int>g[5005];
int vis[5005];
long long f[5005], invf[5005];
long long size[5005];
long long dp[5005][5005];
const long long mod = 1e9+7;
const long long sign = mod-1;
long long cycle = 0;

long long power(long long a,long long b){
    long long res = 1;
    while(b){
        if(b&1) res = res*a%mod;
        a = a*a%mod;
        b /= 2;
    }
    return res;
}

long long inv(long long u){ return power(u,mod-2); }

void preWork(int n){
    f[0] = invf[0] = 1;
    for(int i = 1; i <= n; i++){
        f[i] = f[i-1]*(long long)i%mod;
        invf[i] = inv(f[i]);
    }
}

long long getC(int n,int m){
    if(m>n || m<0) return 0;
    else{
        long long res = f[n];
        res = res*invf[m]%mod;
        res = res*invf[n-m]%mod;
        return res;
    }
}

void dfs(int u,int t){
    vis[u] = t;
    for(int i = 0; i < (int)g[u].size(); i++){
        int v = g[u][i];
        if(!vis[v]) dfs(v,t);
    }
}

int getAmount(int n){
    int tag = 0, total = 0;
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            dfs(i,++tag);
            int find = -1, curSize = 0;
            for(int j = 1; j <= n; j++){
                if(vis[j]==tag){
                    curSize++;
                    if(p[j]==-1) find = 1;
                }
            }
            if(find==-1) cycle++;
            else size[++total] = curSize;
        }
    }
    return total;
}

int main(){
    long long n;
    scanf("%lld",&n);
    preWork(n);
    long long k = 0;
    for(int i = 1; i <= n; i++){
        int u;
        scanf("%d",&u);
        p[i] = u;
        if(u!=-1){
            g[u].push_back(i);
            g[i].push_back(u);
        }
        else k++;
    }
    int total = getAmount(n);
    //printf("total = %d, cycle = %lld\n",total,cycle);
    long long ans = power(n-1,k)*(n-cycle)%mod;
    //printf("ans = %lld\n",ans);
    dp[0][0] = 1;
    for(int i = 1; i <= total; i++){
        for(int j = 0; j <= total; j++){
            dp[i][j] = dp[i-1][j];
            if(j!=0) dp[i][j] = (dp[i][j]+dp[i-1][j-1]*size[i]%mod)%mod;
            //printf("dp[%d][%d] = %lld\n",i,j,dp[i][j]);
        }
    }
    dp[total][1] = 0;
    for(int i = 1; i <= total; i++) dp[total][1] = (dp[total][1]+(size[i]-1))%mod;
    for(int i = 1; i <= total; i++){
        //printf("i = %d\n",i);
        long long minus = power(n-1,k-i)*f[i-1]%mod;
        minus = minus*dp[total][i]%mod;
        minus = minus*sign%mod;
        //printf("minus = %lld\n",minus);
        ans = (ans+minus)%mod;
    }
    printf("%lld\n",ans);
    return 0;
}