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 Ruby (2.7.1) (judge verdict: Time Limit Exceeded)

MODC = 1000000007

n = gets.strip.to_i
o = gets.strip.split.map(&:to_i)

g=[]
r=[]
u=[]
o.each_with_index do |e,i|
  if e < 0
    u.append i
  elsif !r[i]
    if !r[e-1]
      r[i] = [i,e-1]
      r[e-1] = r[i]
      g.append r[i]
    else
      r[i]=r[e-1]
      r[e-1].append i
    end
  elsif !r[e-1]
    r[e-1]=r[i]
    r[i].append e-1
  elsif r[i] != r[e-1]
    t = r[e-1]
    t.each {|c| r[c]=r[i]}
    r[i].concat t
    g.delete t
  end
end
ans = 0
k = u.size
if k != 0
  s = Array.new(k)
  k.times do|i|
    if r[u[i]]
      s[i] = r[u[i]].size
      g.delete r[u[i]]
    else
      s[i] = 1
    end
  end
  dp = Array.new(k,0)
  s.each do |e|
    (k-1).downto(0) do |i|
      if i==0
        dp[i] += e
      else
        dp[i] += dp[i-1]*e%MODC
      end
    end
  end
  dp[0] -= s.size
  fact = 1
  k.times {|i|
    fact = fact * i % MODC if i > 0
    ans += dp[i] * fact * (n-1).pow(k-i-1,MODC) % MODC
  }
end
ans += g.size * (n-1).pow(k,MODC)
ans = n * (n-1).pow(k,MODC) - ans
ans %= MODC
p ans