Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 8)


n, m = list(map(int, input().split()))
dp = [-1]*n

xx = {}

g = [set() for i in range(n)]

for i in range(n-1):
    x, y = list(map(int, input().split()))
    x-=1
    y-=1
    g[x].add(y)
    g[y].add(x)

# sys.exit(0)

def solve(u, p):

    if (dp[u]!=-1):
        return dp[u]

    ans = 1
    for v in g[u]:
        if v==p:
            continue
        
        ans *= (1+solve(v, u))
    dp[u]=ans
    return ans
    

def solve2(u, p):
    #u is at the root
    xx[u]=dp[u]

    for v in g[u]:
        if v==p:
            continue
        dp[u]/=(1+dp[v])
        dp[v]*=(1+dp[u])
        solve2(v, u)
        dp[v]/=(1+dp[u])
        dp[u]*=(1+dp[v])
        
solve(0,-1)
solve2(0, -1)

for i in range(n):
    print(int(xx[i]%m))



Fixed submission:
# coding: utf-8
# Your code here!
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n,MOD = [int(i) for i in input().split()]

to = [[] for _ in [0]*n]
for _ in range(n-1):
    x,y = [int(i)-1 for i in input().split()]
    to[x].append(y)
    to[y].append(x)


dp = [[0]*len(to_i) for to_i in to]
parent_index = [-9999]*n 
ans = [0]*n

def dfs(v, p):
    res = 1
    for i, vc in enumerate(to[v]):
        if vc == p:
            parent_index[v] = i
            continue
        dp[v][i] = dfs(vc, v)
        res *= dp[v][i]+1
        res %= MOD
    return res

def dfs2(v, res_p, p):
    deg = len(to[v])
    if p >= 0:
        dp[v][parent_index[v]] = res_p

    accl = [1]*(deg+1)
    accr = [1]*(deg+1)

    for i in range(1, deg+1):
        accl[i] = accl[i-1] * (dp[v][i-1] + 1) % MOD
        accr[-i-1] = accr[-i] * (dp[v][-i] + 1) % MOD

    ans[v] = accr[0]
    for i,vc in enumerate(to[v]):
        if vc == p: continue
        dfs2(vc, accl[i]*accr[i+1]%MOD, v)

dfs(0,-1)
#print(dp)
dfs2(0,0,-1)
#print(dp)
for i in ans:
    print(i)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, MOD = [int(i) for i in input().split()]
adj = [[] for _ in [0] * (n+1)]
dp = [[0, 0] for i in range(n+1)]
dp2 = [0] * (n+1)


def dfs1(node, pn):
    dp[node] = [1, 1]
    for i in adj[node]:
        if i == pn:
            continue
        dfs1(i, node)
        dp[node][0] = (dp[node][0] * (dp[i][0] + dp[i][1]))
        dp[node][1] = (dp[node][1] * dp[i][1])
    return



def dfs2(node, pn):
    dp2[node] = dp[node][0] % MOD

    for i in adj[node]:
        if i == pn:
            continue

        dp[node][0] = (dp[node][0] / (dp[i][0] + dp[i][1]));
        dp[node][1] = (dp[node][1] / dp[i][1]);
        dp[i][0] = (dp[i][0] * (dp[node][0] + dp[node][1]));
        dp[i][1] = (dp[i][1] * dp[node][1]);
        dfs2(i, node);
        dp[i][1] = (dp[i][1] / dp[node][1]);
        dp[i][0] = (dp[i][0] / (dp[node][0] + dp[node][1]));
        dp[node][0] = (dp[node][0] * (dp[i][0] + dp[i][1]));
        dp[node][1] = (dp[node][1] * dp[i][1]);
    return



for _ in range(n-1):
    x, y = [int(i) for i in input().split()]
    adj[x].append(y)
    adj[y].append(x)

dfs1(1, -1)
dfs2(1, -1)

for i in range(1, n+1):
    print(int(dp2[i]))


Fixed submission:
# coding: utf-8
# Your code here!
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n,MOD = [int(i) for i in input().split()]

to = [[] for _ in [0]*n]
for _ in range(n-1):
    x,y = [int(i)-1 for i in input().split()]
    to[x].append(y)
    to[y].append(x)


dp = [[0]*len(to_i) for to_i in to]
parent_index = [-9999]*n 
ans = [0]*n

def dfs(v, p):
    res = 1
    for i, vc in enumerate(to[v]):
        if vc == p:
            parent_index[v] = i
            continue
        dp[v][i] = dfs(vc, v)
        res *= dp[v][i]+1
        res %= MOD
    return res

def dfs2(v, res_p, p):
    deg = len(to[v])
    if p >= 0:
        dp[v][parent_index[v]] = res_p

    accl = [1]*(deg+1)
    accr = [1]*(deg+1)

    for i in range(1, deg+1):
        accl[i] = accl[i-1] * (dp[v][i-1] + 1) % MOD
        accr[-i-1] = accr[-i] * (dp[v][-i] + 1) % MOD

    ans[v] = accr[0]
    for i,vc in enumerate(to[v]):
        if vc == p: continue
        dfs2(vc, accl[i]*accr[i+1]%MOD, v)

dfs(0,-1)
#print(dp)
dfs2(0,0,-1)
#print(dp)
for i in ans:
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, MOD = [int(i) for i in input().split()]
adj = [[] for _ in [0] * (n+1)]
dp = [[0, 0] for i in range(n+1)]
dp2 = [0] * (n+1)


def dfs1(node, pn):
    dp[node] = [1, 1]
    for i in adj[node]:
        if i == pn:
            continue
        dfs1(i, node)
        dp[node][0] = (dp[node][0] * (dp[i][0] + dp[i][1]))
        dp[node][1] = (dp[node][1] * dp[i][1])
    return



def dfs2(node, pn):
    dp2[node] = dp[node][0] % MOD

    for i in adj[node]:
        if i == pn:
            continue

        dp[node][0] = (dp[node][0] / (dp[i][0] + dp[i][1]));
        dp[node][1] = (dp[node][1] / dp[i][1]);
        dp[i][0] = (dp[i][0] * (dp[node][0] + dp[node][1]));
        dp[i][1] = (dp[i][1] * dp[node][1]);
        dfs2(i, node);
        dp[i][1] = (dp[i][1] / dp[node][1]);
        dp[i][0] = (dp[i][0] / (dp[node][0] + dp[node][1]));
        dp[node][0] = (dp[node][0] * (dp[i][0] + dp[i][1]));
        dp[node][1] = (dp[node][1] * dp[i][1]);
    return



for _ in range(n-1):
    x, y = [int(i) for i in input().split()]
    adj[x].append(y)
    adj[y].append(x)

dfs1(1, -1)
dfs2(1, -1)

for i in range(1, n+1):
    print(dp2[i])


Fixed submission:
# coding: utf-8
# Your code here!
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n,MOD = [int(i) for i in input().split()]

to = [[] for _ in [0]*n]
for _ in range(n-1):
    x,y = [int(i)-1 for i in input().split()]
    to[x].append(y)
    to[y].append(x)


dp = [[0]*len(to_i) for to_i in to]
parent_index = [-9999]*n 
ans = [0]*n

def dfs(v, p):
    res = 1
    for i, vc in enumerate(to[v]):
        if vc == p:
            parent_index[v] = i
            continue
        dp[v][i] = dfs(vc, v)
        res *= dp[v][i]+1
        res %= MOD
    return res

def dfs2(v, res_p, p):
    deg = len(to[v])
    if p >= 0:
        dp[v][parent_index[v]] = res_p

    accl = [1]*(deg+1)
    accr = [1]*(deg+1)

    for i in range(1, deg+1):
        accl[i] = accl[i-1] * (dp[v][i-1] + 1) % MOD
        accr[-i-1] = accr[-i] * (dp[v][-i] + 1) % MOD

    ans[v] = accr[0]
    for i,vc in enumerate(to[v]):
        if vc == p: continue
        dfs2(vc, accl[i]*accr[i+1]%MOD, v)

dfs(0,-1)
#print(dp)
dfs2(0,0,-1)
#print(dp)
for i in ans:
    print(i)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, MOD = [int(i) for i in input().split()]
adj = [[] for _ in [0] * (n+1)]
dp = [[0, 0] for i in range(n+1)]
dp2 = [0] * (n+1)


def dfs1(node, pn):
    dp[node] = [1, 1]
    for i in adj[node]:
        if i == pn:
            continue
        dfs1(i, node)
        dp[node][0] = (dp[node][0] * (dp[i][0] + dp[i][1]))
        dp[node][1] = (dp[node][1] * dp[i][1])
    return



def dfs2(node, pn):
    dp2[node] = dp[node][0] % MOD

    for i in adj[node]:
        if i == pn:
            continue

        dp[node][0] = (dp[node][0] / (dp[i][0] + dp[i][1]));
        dp[node][1] = (dp[node][1] / dp[i][1]);
        dp[i][0] = (dp[i][0] * (dp[node][0] + dp[node][1]));
        dp[i][1] = (dp[i][1] * dp[node][1]);
        dfs2(i, node);
        dp[i][1] = (dp[i][1] / dp[node][1]);
        dp[i][0] = (dp[i][0] / (dp[node][0] + dp[node][1]));
        dp[node][0] = (dp[node][0] * (dp[i][0] + dp[i][1]));
        dp[node][1] = (dp[node][1] * dp[i][1]);
    return



for _ in range(n-1):
    x, y = [int(i) for i in input().split()]
    adj[x].append(y)
    adj[y].append(x)

dfs1(1, -1)
dfs2(1, -1)

for i in range(1, n+1):
    print(dp2[i])


Fixed submission:
# coding: utf-8
# Your code here!
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n,MOD = [int(i) for i in input().split()]

to = [[] for _ in [0]*n]
for _ in range(n-1):
    x,y = [int(i)-1 for i in input().split()]
    to[x].append(y)
    to[y].append(x)


dp = [[0]*len(to_i) for to_i in to]
parent_index = [-9999]*n 
ans = [0]*n

def dfs(v, p):
    res = 1
    for i, vc in enumerate(to[v]):
        if vc == p:
            parent_index[v] = i
            continue
        dp[v][i] = dfs(vc, v)
        res *= dp[v][i]+1
        res %= MOD
    return res

def dfs2(v, res_p, p):
    deg = len(to[v])
    if p >= 0:
        dp[v][parent_index[v]] = res_p

    accl = [1]*(deg+1)
    accr = [1]*(deg+1)

    for i in range(1, deg+1):
        accl[i] = accl[i-1] * (dp[v][i-1] + 1) % MOD
        accr[-i-1] = accr[-i] * (dp[v][-i] + 1) % MOD

    ans[v] = accr[0]
    for i,vc in enumerate(to[v]):
        if vc == p: continue
        dfs2(vc, accl[i]*accr[i+1]%MOD, v)

dfs(0,-1)
#print(dp)
dfs2(0,0,-1)
#print(dp)
for i in ans:
    print(i)