The King is on a quest to conquer the entire world.
There are ‘N’ kingdoms in the world numbered from 0 to N-1. Each kingdom can only be reached from exactly one of the other kingdoms, except the ‘0th’ kingdom where the King lives. Also, it is possible to reach all other kingdoms from the 0th kingdom.
That is, the world is made up of a network of kingdoms in form of a tree rooted at zero. You are given an array ‘prevKingdom’ denoting that the ith kingdom can only be reached from the ‘prevKingdom[i]th’ kingdom, and prevKingdom[0] = -1.
The King initially lives in the 0th kingdom, but he wants to conquer all other kingdoms; he can only conquer a kingdom if all other kingdoms lying on the shortest path to the 0th kingdom have already been conquered.
Find the number of ways in which the King can conquer the entire world. As this number can be very big, return its modulo 109+7.
Note :Two ways of conquering the world are considered different if the order in which the king conquered the kingdoms from 1 to N-1 are different. Refer to the example below for a better understanding.
For Example :
If N = 4, prevKingdom = { -1, 0, 0, 1 } , then the network of kingdoms is denoted as:

Then the King has 3 ways to conquer the world:
0 -> 1 -> 2 -> 3
0 -> 1 -> 3 -> 2
0 -> 2 -> 1 -> 3
All these 3 ways satisfy the constraint that before conquering any kingdom all the kingdoms on the shortest path to the 0th kingdom have been conquered, in other words: before conquering kingdom-3 the King must conquer kingdom-1.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’ denoting the number of kingdoms.
The next line contains N integers, ‘prevKingdom[i]’ denoting that the ith kingdom can only be reached from the prevKingdom[i]^th kingdom.
Output Format :
For each test case, print a single integer denoting the number of ways the King can conquer the world.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
0 <= N <= 10000
0 <= prevKingdom[i] < N, and prevKingdom[0] = -1
Time limit: 1 sec
2
4
-1 0 0 1
4
-1 0 1 2
3
1
For test case 1 :
We will return 3, because:
( 0 -> 1 -> 2 -> 3 )
( 0 -> 1 -> 3 -> 2 )
( 0 -> 2 -> 1 -> 3 )
are the only possible ways to conquer the world.
For test case 2 :
We will return 1, because:
( 0 -> 1 -> 2 -> 3 ) is the only possible way to conquer the world.
2
8
-1 0 0 1 2 3 3 4
12
-1 0 0 1 2 3 3 4 5 5 6 6
70
13200
Consider a node with 2 children, if we already calculated the solution for subtrees rooted at both the child nodes, then how can we combine the result to get the answer for the current node?
This problem simply wants us to find the number of topological orderings of a directed tree rooted at 0.
Consider a case :
Where we have already precalculated answers for the subtree rooted at nodes 1 and 2.
After understanding this logic the question can simply be tackled by the dfs-algorithm.
Do note that some languages might require you to do inverse modulo calculations. Using Fermat's little theorem we can directly calculate the value of ( (a*b-1) % p ) as:
( a * ( b (p - 2) % p ) ) % p )
The steps are as follows :
O( N ), where N denotes the number of kingdoms
We precalculate the factorial and then use depth-first search to visit each kingdom exactly once. Do note that each time we are calculating the inverse modulo expression using binary exponentiation, this involves a complexity of ~log(mod), so the exact time complexity will be O(N*log(1e9)), but ignore the log for a while as mod is explicitly being calculated to fit the answer in integer range.
Hence the time complexity is O( N ).
O( N ), where N denotes the number of kingdoms
Extra space is required to store the tree in form of an adjacency list.
Hence the space complexity is O( N ).