Problem p02728 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10**6)
stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n_ = 2 * 10**5 + 20
mod = 10**9 + 7
fun = [1] * (n_ + 1)
for i in range(1, n_ + 1):
    fun[i] = fun[i - 1] * i % mod
rev = [1] * (n_ + 1)
rev[n_] = pow(fun[n_], mod - 2, mod)
for i in range(n_ - 1, 0, -1):
    rev[i] = rev[i + 1] * (i + 1) % mod

def modinv(x, mod):
    x %= mod
    a, b = x, mod
    u, v = 1, 0
    while b:
        t = a // b
        a -= t * b; a, b = b, a
        u -= t * v; u, v = v, u
    return u % mod

def nCr(n, r):
    if r > n:
        return 0
    return fun[n] * rev[r] % mod * rev[n - r] % mod

n = ni()
g = [list() for _ in range(n)]
for _ in range(n-1):
    a,b = nm()
    a -= 1; b -= 1
    g[a].append(b)
    g[b].append(a)

sl = [1]*n
pm = [1]*n
def dfs(v, p):
    for x in g[v]:
        if x == p:
            continue
        dfs(x, v)
        sl[v] += sl[x]
        pm[v] = pm[v] * pm[x] % mod
    sv = sl[v] - 1
    for x in g[v]:
        if x == p:
            continue
        pm[v] = pm[v] * nCr(sv, sl[x]) % mod
        sv -= sl[x]
    return

def dfs2(v, p):
    pp = pm[p] *  modinv(nCr(n-1, sl[v]) * pm[v], mod) % mod
    sp = n - sl[v]
    pm[v] = pm[v] * pp % mod * nCr(n-1, sp) % mod
    for x in g[v]:
        if x == p:
            continue
        dfs2(x, v)

dfs(0, -1)
for x in g[0]:
    dfs2(x, 0)

print(*pm, sep = '\n')

Buggy submission (verdict: Runtime Error):
def reroot(nodeCount, edges, identity, merge, addNode):
    adjacents = [list() for _ in range(nodeCount)]
    indexForAdjacents = [list() for _ in range(nodeCount)]
    for u, v in edges:
        indexForAdjacents[u].append(len(adjacents[v]))
        indexForAdjacents[v].append(len(adjacents[u]))
        adjacents[u].append(v)
        adjacents[v].append(u)
    childSubtreeRes = [[identity]*len(adjacents[i]) for i in range(nodeCount)]
    nodeRes = [identity]*nodeCount
    parents = [-1]*nodeCount
    order = [0]*nodeCount
    index = 0
    stack = [0]
    while stack:
        node = stack.pop()
        order[index] = node
        index += 1
        for adjacent in adjacents[node]:
            if adjacent == parents[node]:
                continue
            stack.append(adjacent)
            parents[adjacent] = node
    for node in reversed(order):
        parent = parents[node]
        result = identity
        parentIndex = -1
        for j in range(len(adjacents[node])):
            if adjacents[node][j] == parent:
                parentIndex = j
                continue
            result = merge(result, childSubtreeRes[node][j])
        childSubtreeRes[parent][indexForAdjacents[node][parentIndex]] = addNode(result, node)
    for node in order:
        accumsFromTail = [identity]*len(adjacents[node])
        for j in range(len(adjacents[node])-1, 0, -1):
            accumsFromTail[j - 1] = merge(childSubtreeRes[node][j], accumsFromTail[j])
        accum = identity
        for j in range(len(adjacents[node])):
            result = addNode(merge(accum, accumsFromTail[j]), node)
            childSubtreeRes[adjacents[node][j]][indexForAdjacents[node][j]] = result
            accum = merge(accum, childSubtreeRes[node][j])
        nodeRes[node] = addNode(accum, node)
    return nodeRes

n_ = 2 * 10**5 + 20
mod = 10**9 + 7
fun = [1] * (n_ + 1)
for i in range(1, n_ + 1):
    fun[i] = fun[i - 1] * i % mod
rev = [1] * (n_ + 1)
rev[n_] = pow(fun[n_], mod - 2, mod)
for i in range(n_ - 1, 0, -1):
    rev[i] = rev[i + 1] * (i + 1) % mod
  
mod = 10**9 + 7
int1 = lambda x: int(x) - 1
merge = lambda p, q: (p[0] * q[0] * rev[q[1]] % mod, p[1] + q[1])
addNode = lambda value, idx: (value[0] * fun[value[1]] % mod, value[1] + 1)
N = int(input())
identity = (1, 0)
edges = [tuple(map(int1, input().split())) for _ in range(N-1)]
res = reroot(N, edges, identity, merge, addNode)
for x, y in res:
    print(x)

Buggy submission (verdict: Runtime Error):
def reroot(nodeCount, edges, identity, merge, addNode):
    adjacents = [list() for _ in range(nodeCount)]
    indexForAdjacents = [list() for _ in range(nodeCount)]
    for u, v in edges:
        indexForAdjacents[u].append(len(adjacents[v]))
        indexForAdjacents[v].append(len(adjacents[u]))
        adjacents[u].append(v)
        adjacents[v].append(u)
    childSubtreeRes = [[identity]*len(adjacents[i]) for i in range(nodeCount)]
    nodeRes = [identity]*nodeCount
    parents = [-1]*nodeCount
    order = [0]*nodeCount
    index = 0
    stack = [0]
    while stack:
        node = stack.pop()
        order[index] = node
        index += 1
        for adjacent in adjacents[node]:
            if adjacent == parents[node]:
                continue
            stack.append(adjacent)
            parents[adjacent] = node
    for node in reversed(order):
        parent = parents[node]
        result = identity
        parentIndex = -1
        for j in range(len(adjacents[node])):
            if adjacents[node][j] == parent:
                parentIndex = j
                continue
            result = merge(result, childSubtreeRes[node][j])
        childSubtreeRes[parent][indexForAdjacents[node][parentIndex]] = addNode(result, node)
    for node in order:
        accumsFromTail = [identity]*len(adjacents[node])
        for j in range(len(adjacents[node])-1, 0, -1):
            accumsFromTail[j - 1] = merge(childSubtreeRes[node][j], accumsFromTail[j])
        accum = identity
        for j in range(len(adjacents[node])):
            result = addNode(merge(accum, accumsFromTail[j]), node)
            childSubtreeRes[adjacents[node][j]][indexForAdjacents[node][j]] = result
            accum = merge(accum, childSubtreeRes[node][j])
        nodeRes[node] = addNode(accum, node)
    return nodeRes

def fun(v):
    ret = 1
    for i in range(2, v+1):
        ret = ret * i % mod
    return ret
  
mod = 10**9 + 7
int1 = lambda x: int(x) - 1
merge = lambda p, q: (p[0] * q[0] * pow(fun(q[1]), mod-2, mod) % mod, p[1] + q[1])
addNode = lambda value, idx: (value[0] * fun(value[1]) % mod, value[1] + 1)
N = int(input())
identity = (1, 0)
edges = [tuple(map(int1, input().split())) for _ in range(N-1)]
res = reroot(N, edges, identity, merge, addNode)
for x, y in res:
    print(x)

Buggy submission (verdict: Runtime Error):
def reroot(nodeCount, edges, identity, merge, addNode):
    adjacents = [list() for _ in range(nodeCount)]
    indexForAdjacents = [list() for _ in range(nodeCount)]
    for u, v in edges:
        indexForAdjacents[u].append(len(adjacents[v]))
        indexForAdjacents[v].append(len(adjacents[u]))
        adjacents[u].append(v)
        adjacents[v].append(u)
    childSubtreeRes = [[identity]*len(adjacents[i]) for i in range(nodeCount)]
    nodeRes = [identity]*nodeCount
    parents = [-1]*nodeCount
    order = [0]*nodeCount
    index = 0
    stack = [0]
    while stack:
        node = stack.pop()
        order[index] = node
        index += 1
        for adjacent in adjacents[node]:
            if adjacent == parents[node]:
                continue
            stack.append(adjacent)
            parents[adjacent] = node
    for node in reversed(order):
        parent = parents[node]
        result = identity
        parentIndex = -1
        for j in range(len(adjacents[node])):
            if adjacents[node][j] == parent:
                parentIndex = j
                continue
            result = merge(result, childSubtreeRes[node][j])
        childSubtreeRes[parent][indexForAdjacents[node][parentIndex]] = addNode(result, node)
    for node in order:
        accumsFromTail = [identity]*len(adjacents[node])
        for j in range(len(adjacents[node])-1, 0, -1):
            accumsFromTail[j - 1] = merge(childSubtreeRes[node][j], accumsFromTail[j])
        accum = identity
        for j in range(len(adjacents[node])):
            result = addNode(merge(accum, accumsFromTail[j]), node)
            childSubtreeRes[adjacents[node][j]][indexForAdjacents[node][j]] = result
            accum = merge(accum, childSubtreeRes[node][j])
        nodeRes[node] = addNode(accum, node)
    return nodeRes

def fun(v):
    ret = 1
    for i in range(2, v+1):
        ret = ret * i % mod
    return ret
  
mod = 10**9 + 7
int1 = lambda x: int(x) - 1
merge = lambda p, q: (p[0] * q[0] * pow(fun(q[1]), mod-2, mod) % mod, p[1] + q[1])
addNode = lambda value, idx: (value[0] * fun(value[1]) % mod, value[1] + 1)
N = int(input())
identity = (1, 0)
edges = [tuple(map(int1, input().split())) for _ in range(N-1)]
res = reroot(N, edges, identity, merge, addNode)
for x, y in res:
    print(x)