classSolution: defwaysToBuildRooms(self, prevRoom: List[int]) -> int: MOD = 10**9 + 7 g = collections.defaultdict(list) for curr, prev inenumerate(prevRoom): g[prev].append(curr) defdfs(curr): ifnot g[curr]: return1, 1 ans, l = 1, 0 fornextin g[curr]: tmp, r = dfs(next) ans = (ans * tmp * math.comb(l + r, r)) % MOD l += r return ans, l + 1