Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**5 + 1)
n, m = map(int, input().split())

graph = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())

    graph[a].append(b)
    graph[b].append(a)

rooted = [[] for _ in range(n)]
rev_rooted = [[] for _ in range(n)]
que = [(0, -1)]
while que:
    node, parent = que.pop()
    for child in graph[node]:
        if child != parent:
            que.append((child, node))
            rooted[node].append(child)
            rev_rooted[child].append(node)
            # print(node, child)

def mod(v):
    if v >= m:
        return v % m
    return v

dp = [None] * n
def dfs(node):
    if dp[node] is not None:
        return dp[node]

    pattern = 1 # 自身のみのパターン
    for child in rooted[node]:
        pattern = mod(pattern * (dfs(child) + 1))
    dp[node] = pattern
    return pattern

rev_dp = [None] * n
def rev_dfs(node):
    if rev_dp[node] is not None:
        return rev_dp[node]

    pattern = 1 # 自身のみのパターン
    if not rev_rooted[node]:
        pattern = 0
    else:
        parent = rev_rooted[node][0]
        pattern = mod(pattern * (rev_dfs(parent) + 1))
        for child in rooted[parent]:
            if child != node:
                pattern = mod(pattern * (dp[child] + 1))
    rev_dp[node] = pattern
    return pattern

# print("-----")
for i in range(n):
    dfs(i)
for i in range(n):
    print(mod(dp[i] * (rev_dfs(i) + 1)))

Fixed submission:
import itertools
import sys
import heapq
sys.setrecursionlimit(10**5 + 1)
n, m = map(int, input().split())

graph = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())

    graph[a].append(b)
    graph[b].append(a)

rooted = [[] for _ in range(n)]
rev_rooted = [[] for _ in range(n)]
que = [(0, -1, 0)]
bfs_target = []
while que:
    node, parent, distance = que.pop()
    heapq.heappush(bfs_target, (-distance, node))
    for child in graph[node]:
        if child != parent:
            que.append((child, node, distance + 1))
            rooted[node].append(child)
            rev_rooted[child].append(node)
            # print(node, child)

def mod(v):
    if v >= m:
        return v % m
    return v


dp = [1] * n
while bfs_target:
    node = heapq.heappop(bfs_target)[1]
    for parent in rev_rooted[node]:
        dp[parent] = mod(dp[parent] * (dp[node] + 1))
        que.append(parent)

rev_dp = [0] * n
que = [0]
while que:
    node = que.pop()
    cp = [1]
    rev_cp = [1]
    for i, child in enumerate(rooted[node]):
        cp.append(mod(cp[i] * (dp[child] + 1)))
    for i, child in enumerate(reversed(rooted[node])):
        rev_cp.append(mod(rev_cp[i] * (dp[child] + 1)))
    rev_cp = rev_cp[::-1]
    for i, child in enumerate(rooted[node]):
        rev_dp[child] = mod(cp[i] * rev_cp[i + 1] * (rev_dp[node] + 1))
        que.append(child)


# print(dp)
for v1, v2 in zip(dp, rev_dp):
    print(mod(v1 * (v2 + 1)))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**5 + 1)
n, m = map(int, input().split())

graph = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())

    graph[a].append(b)
    graph[b].append(a)

rooted = [[] for _ in range(n)]
rev_rooted = [[] for _ in range(n)]
que = [(0, -1)]
while que:
    node, parent = que.pop()
    for child in graph[node]:
        if child != parent:
            que.append((child, node))
            rooted[node].append(child)
            rev_rooted[child].append(node)

def mod(v):
    if v >= m:
        return v % m
    return v

dp = [None] * n
def dfs(node):
    if dp[node] is not None:
        return dp[node]

    pattern = 1 # 自身のみのパターン
    for child in rooted[node]:
        pattern = mod(pattern * (dfs(child) + 1))
    dp[node] = pattern
    return pattern

rev_dp = [None] * n
def rev_dfs(node):
    if rev_dp[node] is not None:
        return rev_dp[node]

    pattern = 1 # 自身のみのパターン
    if not rev_rooted[node]:
        pattern = 0
    for parent in rev_rooted[node]: # 親は0or1人
        pattern = mod(pattern * (rev_dfs(parent) + 1))
        for child in rooted[parent]:
            if child != node:
                pattern = mod(pattern * (dfs(child) + 1))
    rev_dp[node] = pattern
    return pattern

# print("-----")
for i in range(n):
    print(mod(dfs(i) * (rev_dfs(i) + 1)))


Fixed submission:
import itertools
import sys
import heapq
sys.setrecursionlimit(10**5 + 1)
n, m = map(int, input().split())

graph = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())

    graph[a].append(b)
    graph[b].append(a)

rooted = [[] for _ in range(n)]
rev_rooted = [[] for _ in range(n)]
que = [(0, -1, 0)]
bfs_target = []
while que:
    node, parent, distance = que.pop()
    heapq.heappush(bfs_target, (-distance, node))
    for child in graph[node]:
        if child != parent:
            que.append((child, node, distance + 1))
            rooted[node].append(child)
            rev_rooted[child].append(node)
            # print(node, child)

def mod(v):
    if v >= m:
        return v % m
    return v


dp = [1] * n
while bfs_target:
    node = heapq.heappop(bfs_target)[1]
    for parent in rev_rooted[node]:
        dp[parent] = mod(dp[parent] * (dp[node] + 1))
        que.append(parent)

rev_dp = [0] * n
que = [0]
while que:
    node = que.pop()
    cp = [1]
    rev_cp = [1]
    for i, child in enumerate(rooted[node]):
        cp.append(mod(cp[i] * (dp[child] + 1)))
    for i, child in enumerate(reversed(rooted[node])):
        rev_cp.append(mod(rev_cp[i] * (dp[child] + 1)))
    rev_cp = rev_cp[::-1]
    for i, child in enumerate(rooted[node]):
        rev_dp[child] = mod(cp[i] * rev_cp[i + 1] * (rev_dp[node] + 1))
        que.append(child)


# print(dp)
for v1, v2 in zip(dp, rev_dp):
    print(mod(v1 * (v2 + 1)))