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 Rust (1.42.0) (judge verdict: Wrong Answer)

// -*- coding:utf-8-unix -*-

use proconio::input;

static MODC:u64 = 1000000007;
fn powm(xx:u64, yy:u32) -> u64 {
    let mut x = xx;
    let mut y = yy;
    let mut result = 1;
    while y > 0 {
        if y & 1 != 0 {
            result *= x;
            result %= MODC;
        }
        x *= x;
        x %= MODC;
        y >>= 1;
    }
    result
}

fn main() {
    input! {
        n: usize,
        o: [i32; n],
    }

    let n1:u64 = (n-1)as u64;
    let mut cy: u64 = 0;
    let mut g: Vec<Vec<usize>> = Vec::new();
    let mut r = vec![-1;n];
    let mut u: Vec<usize> = Vec::new();
    for i in 0..n {
        if o[i] < 0 {
            u.push(i);
        } else {
            let e = (o[i]-1) as usize;
            if r[i] < 0 {
                if r[e] < 0 {
                    r[i] = g.len() as i32;
                    r[e] = g.len() as i32;
                    g.push(vec![i,e]);
                } else {
                    r[i] = r[e];
                    g[r[e] as usize].push(i);
                }
            } else if r[e] < 0 {
                r[e] = r[i];
                g[r[i] as usize].push(e);
            } else if r[i] != r[e] {
                let t = if r[i] < r[e] {r[i]} else {r[e]};
                let a = if r[i] < r[e] {r[e]} else {r[i]};
                for c in 0..n {
                    if r[c] == a {
                        r[c] = t;
                    } else if r[c] > a {
                        r[c] -= 1;
                    }
                }
                let mut v = g.remove(a as usize);
                g[t as usize].append(&mut v);
            } else {
                cy += 1;
            }
        }
    }
    //p [g,r,u]
    let mut ans:u64 = 0;
    let k = u.len();
    if k != 0 {
        let mut s : Vec<usize> = vec![0; k];
        for i in 0..k {
            if r[u[i]] < 0 {
                s[i] = 1;
            }
            else {
                s[i] = g[r[u[i]] as usize].len();
                //g.remove_item(r[u[i]]);
            }
        }
        let mut dp : Vec<u64> = vec![0; k];
        for e in s.iter() {
            let mut i = k;
            while i > 0 {
                i -= 1;
                if i == 0 {
                    dp[i] += *e as u64;
                }
                else {
                    dp[i] += dp[i-1] * (*e as u64) % MODC;
                }
            }
        }
        dp[0] -= s.len() as u64;
        let mut fact:u64 = 1;
        for i in 0..k {
            if i > 0 { fact = fact * (i as u64) % MODC; }
            ans += dp[i] * fact * powm(n1,(k-i-1) as u32) % MODC;
        }
    }
    ans += cy * powm(n1, k as u32);
    ans %= MODC;
    ans = n as u64 * powm(n1, k as u32) + MODC - ans;
    ans %= MODC;
    println!("{}", ans);
}