You are to implement a solution for a road-building plan among towns. The towns are numbered from 1 to N, and currently, there are no roads connecting them. Each town can request that a road be built to connect it to another town, making it reachable through one or more roads.
Town Requests
Each town's request is represented by an array P where:
P[i] (for town i) can take the following values:
-1 if Town i has not made a request.
An integer j (where 1 ≤ j ≤ N) indicating that Town i requests a road to Town j.
Key Definitions
Let K be the number of towns i such that P[i] = -1.
There are (N-1)^K different combinations of requests that can be made based on the towns that have not made a choice.
Objective
For each combination of requests, calculate the minimum number of roads needed to satisfy all requests, and return the total sum of those minimum numbers across all combinations, modulo (10^9+7).
Input Format
An integer N (1 ≤ N ≤ 100,000) - The number of towns.
An array P of size N where:
P[i] is in the range [-1, N] for 1 ≤ i ≤ N.
Output Format
A single integer which is the sum of the minimum number of roads needed across all possible request combinations, modulo (10^9 + 7).
Example
Input:
text
N = 3
P = [-1, 2, -1]
Output:
text
3
Explanation
Here, we have 3 towns, and Town 1 and Town 3 have not made requests while Town 2 requests a road to Town 2.
The possible combinations are:
Town 1 could request Town 2 or Town 3.
Calculate the number of roads needed for each combination.
The total minimum roads required across these combinations, in this case, results in the output 3.
Implementation Requirements
Please place your implementation in the /app/ directory.