Buggy python submission (verdict: Runtime Error):
def reroot(nodeCount, edges, identity, merge, addNode, root=0):
    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 = [root]
    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

int1 = lambda x: int(x) - 1
n, mod = map(int, input().split())
if n == 1:
  print(1)
  exit(0)
merge = lambda u, v: u * v % mod
addNode = lambda val, id: val + 1
edges = [tuple(map(int1, input().split())) for _ in range(n-1)]
res = list(map(int1, reroot(n, edges, 1, merge, addNode)))
print(*res, sep='\n')

Fixed submission:
def reroot(nodeCount, edges, identity, merge, addNode, root=0):
    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 = [root]
    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])
        if parentIndex >= 0:
            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

int1 = lambda x: int(x) - 1
n, mod = map(int, input().split())
merge = lambda u, v: u * v % mod
addNode = lambda val, id: val + 1
edges = [tuple(map(int1, input().split())) for _ in range(n-1)]
res = list(map(int1, reroot(n, edges, 1, merge, addNode)))
print(*res, sep='\n')