You are an ant tasked with adding n new rooms numbered 0 to n-1 to your colony. You are given the expansion plan as a 0-indexed integer array of length n, prevRoom, where prevRoom[i] indicates that you must build room
prevRoom[i] before building room i, and these two rooms must be connected
directly. Room 0 is already built, so prevRoom[0] = -1. The expansion plan is given such that once all the rooms are built, every room will be reachable from room 0.
You can only build one room at a time, and you can travel freely between rooms you have already built only if they are connected. You can choose to build any room as long as its previous room is already built.
Return thenumber of different orders you can build all the rooms in.
Since the answer may be large, return it modulo10^9 + 7.

Input: prevRoom =[-1,0,1]Output: 1Explanation: There is only one way to build the additional rooms:0->1->2
The problem reduces to counting the number of valid topological orderings of a rooted tree, where each room can only be built after its parent. The number of ways to build all rooms is the product of the ways to build each subtree, multiplied by the multinomial coefficient for merging the subtrees.
classSolution {
publicintwaysToBuildRooms(int[] prevRoom) {
int MOD = 1_000_000_007, n = prevRoom.length;
List<Integer>[] g =new List[n];
for (int i = 0; i < n; i++) g[i]=new ArrayList<>();
for (int i = 1; i < n; i++) g[prevRoom[i]].add(i);
long[] fact =newlong[n+1], invFact =newlong[n+1];
fact[0]= 1;
for (int i = 1; i <= n; i++) fact[i]= fact[i-1]* i % MOD;
for (int i = 0; i <= n; i++) invFact[i]= modinv(fact[i], MOD);
return (int)dfs(0, g, fact, invFact, MOD)[1];
}
privatelong[]dfs(int u, List<Integer>[] g, long[] fact, long[] invFact, int MOD) {
int sz = 1;
long ways = 1;
for (int v : g[u]) {
long[] res = dfs(v, g, fact, invFact, MOD);
int csz = (int)res[0];
long cways = res[1];
ways = ways * cways % MOD * invFact[csz]% MOD;
sz += csz;
}
ways = ways * fact[sz-1]% MOD;
returnnewlong[]{sz, ways};
}
privatelongmodinv(long x, int MOD) {
long res = 1, p = MOD-2;
while (p > 0) {
if ((p&1) == 1) res = res * x % MOD;
x = x * x % MOD;
p >>= 1;
}
return res;
}
}
classSolution {
funwaysToBuildRooms(prevRoom: IntArray): Int {
val MOD = 1_000_000_007
val n = prevRoom.size
val g = Array(n) { mutableListOf<Int>() }
for (i in1 until n) g[prevRoom[i]].add(i)
val fact = LongArray(n+1) { 1L }
val invFact = LongArray(n+1) { 1L }
for (i in1..n) fact[i] = fact[i-1] * i % MOD
funmodinv(x: Long): Long {
var res = 1L; var p = MOD-2L; var b = x
while (p > 0) {
if (p and 1L==1L) res = res * b % MOD
b = b * b % MOD
p = p shr 1 }
return res
}
for (i in1..n) invFact[i] = modinv(fact[i])
fundfs(u: Int): Pair<Int, Long> {
var sz = 1var ways = 1Lfor (v in g[u]) {
val(csz, cways) = dfs(v)
ways = ways * cways % MOD * invFact[csz] % MOD
sz += csz
}
ways = ways * fact[sz-1] % MOD
return sz to ways
}
return dfs(0).second.toInt()
}
}
classSolution:
defwaysToBuildRooms(self, prevRoom: list[int]) -> int:
MOD =10**9+7 n = len(prevRoom)
g = [[] for _ in range(n)]
for i in range(1, n):
g[prevRoom[i]].append(i)
fact = [1] * (n+1)
invFact = [1] * (n+1)
for i in range(1, n+1):
fact[i] = fact[i-1] * i % MOD
defmodinv(x):
res, p =1, MOD-2while p:
if p&1: res = res*x%MOD
x = x*x%MOD
p //=2return res
for i in range(1, n+1):
invFact[i] = modinv(fact[i])
defdfs(u):
sz, ways =1, 1for v in g[u]:
csz, cways = dfs(v)
ways = ways * cways % MOD * invFact[csz] % MOD
sz += csz
ways = ways * fact[sz-1] % MOD
return sz, ways
return dfs(0)[1]