CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)

N, MOD = list(map(int, input().split()))


G = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    x, y = list(map(int, input().split()))
    G[x].append(y)
    G[y].append(x)
G[0].append(1)
G[1].append(0)

DP1 = [-1] * (N + 1)
DP1[0] = 0
parent = [-1] * (N + 1)
parent[1] = 0

def dfs(x, p):
    tmp = 1
    for next_ in G[x]:
        if next_ == p:
            continue
        parent[next_] = x
        tmp *= (dfs(next_, x) + 1)
        tmp %= MOD
    DP1[x] = tmp
    return tmp

dfs(1, 0)
# print (DP1)

DP2 = [1] * (N + 1)
stack = [1]

#next_とyとで2重ループになってO(N^2) (星型のとき)
while stack:
    x = stack.pop()

    # 累積積のパート

    #ここが同じ値を返すかチェックする
    n = len(G[x])
    lst1 = [1] * (n + 1)
    lst2 = [1] * (n + 1)
    for i in range(n):
        #親のときだけは違うルールで対処
        if G[x][i] == parent[x]:
            lst1[i + 1] = lst1[i] * DP2[x] % MOD
        else:
            lst1[i + 1] = lst1[i] * (DP1[G[x][i]] + 1) % MOD

        if G[x][n - 1 - i] == parent[x]:
            lst2[n - i - 1] = lst2[n - i] * DP2[x] % MOD
        else:
            lst2[n - i - 1] = lst2[n - i] * (DP1[G[x][n - 1 - i]] + 1) % MOD
    
    for i in range(n):
        if G[x][i] == parent[x]:
            continue
        tmp = lst1[i] * lst2[i + 1] % MOD
        tmp += 1 #白色の分
        DP2[G[x][i]] = tmp % MOD
        stack.append(G[x][i])

# print (DP2)

for i in range(1, N + 1):
    print(((DP1[i] * DP2[i]) % MOD))

# print (DP1)
# print (DP2)

# Optimized solution
import sys
input = sys.stdin.readline

# sys.setrecursionlimit(10 ** 7)

N, MOD = list(map(int, input().split()))


G = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    x, y = list(map(int, input().split()))
    G[x].append(y)
    G[y].append(x)
G[0].append(1)
G[1].append(0)

DP1 = [0] * (N + 1)

parent = [-1] * (N + 1)
parent[1] = 0

order = []
order_append = order.append

stack = [1]
stack_pop = stack.pop
stack_append = stack.append

while stack:
    x = stack_pop()
    order_append(x)
    for next_ in G[x]:
        if next_ == parent[x]:
            continue
        parent[next_] = x
        stack_append(next_)

for v in order[::-1]:
    tmp = 1
    for next_ in G[v]:
        tmp *= DP1[next_] + 1
        tmp %= MOD
    DP1[v] = tmp
# print (DP1)

DP2 = [1] * (N + 1)

for x in order:
    # 累積積のパート
    #ここが同じ値を返すかチェックする
    n = len(G[x])
    lst1 = [1] * (n + 1)
    lst2 = [1] * (n + 1)
    for i in range(n):
        #親のときだけは違うルールで対処
        if G[x][i] == parent[x]:
            lst1[i + 1] = lst1[i] * DP2[x] % MOD
        else:
            lst1[i + 1] = lst1[i] * (DP1[G[x][i]] + 1) % MOD

        if G[x][n - 1 - i] == parent[x]:
            lst2[n - i - 1] = lst2[n - i] * DP2[x] % MOD
        else:
            lst2[n - i - 1] = lst2[n - i] * (DP1[G[x][n - 1 - i]] + 1) % MOD
    
    for i in range(n):
        if G[x][i] == parent[x]:
            continue
        tmp = lst1[i] * lst2[i + 1] % MOD
        tmp += 1 #白色の分
        DP2[G[x][i]] = tmp
        # stack.append(G[x][i])

# print (DP2)

for i in range(1, N + 1):
    print(((DP1[i] * DP2[i]) % MOD))

# print (DP1)
# print (DP2)
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

def solve():
    N, MOD = list(map(int, input().split()))
    adjL = [[] for _ in range(N)]
    for _ in range(N-1):
        x, y = list(map(int, input().split()))
        x, y = x-1, y-1
        adjL[x].append(y)
        adjL[y].append(x)

    dp = [[] for _ in range(N)]
    outdegs = [0] * N
    iPars = [-1] * N
    def dfsDP(v, vPar):
        outdeg = outdegs[v] = len(adjL[v])
        dp[v] = [0] * outdeg
        res = 1
        for i, v2 in enumerate(adjL[v]):
            if v2 == vPar:
                iPars[v] = i
                continue
            dp[v][i] = dfsDP(v2, v)
            res = res * dp[v][i] % MOD
        return (res + 1) % MOD

    anss = [0] * N
    def dfsAns(v, vPar, resPar):
        if vPar != -1:
            dp[v][iPars[v]] = resPar

        outdeg = outdegs[v]
        dpL = [1] * (outdeg+1)
        for i in range(outdeg):
            v2 = adjL[v][i]
            dpL[i+1] = dpL[i] * dp[v][i] % MOD
        dpR = [1] * (outdeg+1)
        for i in reversed(list(range(outdeg))):
            v2 = adjL[v][i]
            dpR[i] = dpR[i+1] * dp[v][i] % MOD
        anss[v] = dpL[-1]

        for i, v2 in enumerate(adjL[v]):
            if v2 == vPar: continue
            dfsAns(v2, v, (dpL[i] * dpR[i+1] + 1) % MOD)


    dfsDP(0, -1)
    dfsAns(0, -1, 0)

    print(('\n'.join(map(str, anss))))


solve()


# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

N, MOD = list(map(int, input().split()))
adjL = [[] for _ in range(N)]
for _ in range(N-1):
    x, y = list(map(int, input().split()))
    x, y = x-1, y-1
    adjL[x].append(y)
    adjL[y].append(x)

def ReRooting(adjL):
    def merge(val1, val2):
        return val1 * val2 % MOD
    def calcValue(val1):
        return (val1 + 1) % MOD

    numV = len(adjL)
    vRoot = 0
    useds = [False] * numV
    useds[vRoot] = True
    vs = [vRoot]
    pars = [-1] * numV
    stack = [vRoot]
    while stack:
        v = stack.pop()
        for v2 in adjL[v]:
            if not useds[v2]:
                useds[v2] = True
                vs.append(v2)
                pars[v2] = v
                stack.append(v2)

    anss = [0] * numV
    dpUpFrom, dpDownTo, accLs, accRs = [1]*numV, [1]*numV, [1]*numV, [1]*numV
    for v in reversed(vs):
        vPar = pars[v]
        tmp = 1
        for v2 in adjL[v]:
            if v2 == vPar: continue
            accLs[v2] = tmp
            tmp = merge(tmp, dpUpFrom[v2])
        tmp = 1
        for v2 in reversed(adjL[v]):
            if v2 == vPar: continue
            accRs[v2] = tmp
            tmp = merge(tmp, dpUpFrom[v2])
        anss[v] = tmp
        dpUpFrom[v] = calcValue(tmp)

    for v in vs:
        vPar = pars[v]
        dpDownToV = dpDownTo[v]
        anss[v] = merge(anss[v], dpDownToV)
        for v2 in adjL[v]:
            if v2 == vPar: continue
            tmp = merge(accLs[v2], accRs[v2])
            tmp = merge(tmp, dpDownToV)
            dpDownTo[v2] = calcValue(tmp)
    return anss

anss = ReRooting(adjL)
print(('\n'.join(map(str, anss))))
# Original solution


import sys
sys.setrecursionlimit(1000000)

def dfs1(u, pu):
    dp[u] = 1
    for v in tree[u]:
        if v != pu:
            dfs1(v, u)
            dp[u] *= (dp[v] + 1)
            dp[u] %= MOD

def dfs2(u, pu, pval):
    resp[u] = (dp[u] * pval) % MOD

    chlds = [v for v in tree[u] if v != pu]
    cumulr = [1] * len(chlds)
    for i in range(len(chlds) - 2, -1, -1):
        cumulr[i] = cumulr[i + 1] * (1 + dp[chlds[i + 1]]) % MOD

    pref = pval
    for i in range(len(chlds)):
        v = chlds[i]
        vval = (1 + pref * cumulr[i]) % MOD

        dfs2(v, u, vval)

        pref *= (dp[v] + 1)
        pref %= MOD


n, MOD = list(map(int, input().split()))
tree = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v = [int(u) - 1 for u in input().split()]
    tree[u].append(v)
    tree[v].append(u)

dp = [0] * n
resp = [0] * n
dfs1(0, -1)
dfs2(0, -1, 1)
for i in range(n):
    print((resp[i]))


# Optimized solution


n, MOD = list(map(int, input().split()))
tree = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v = [int(u) - 1 for u in input().split()]
    tree[u].append(v)
    tree[v].append(u)


cola = [(0, -1)]
qind = 0
while qind < len(cola):
    u, pu = cola[qind]
    qind += 1
    for v in tree[u]:
        if v != pu:
            cola.append((v, u))

dp = [1] * n
for i in range(n - 1, -1, -1):
    u, pu = cola[i]
    for v in tree[u]:
        if v != pu:
            dp[u] *= (dp[v] + 1)
            dp[u] %= MOD

resp = [0] * n
pval = [1] * n
for j in range(n):
    u, pu = cola[j]
    resp[u] = (dp[u] * pval[u]) % MOD

    chlds = [v for v in tree[u] if v != pu]
    cumulr = [1] * len(chlds)
    for i in range(len(chlds) - 2, -1, -1):
        cumulr[i] = cumulr[i + 1] * (1 + dp[chlds[i + 1]]) % MOD

    pref = pval[u]
    for i in range(len(chlds)):
        v = chlds[i]
        pval[v] = (1 + pref * cumulr[i]) % MOD
        pref *= (dp[v] + 1)
        pref %= MOD

for i in range(n):
    print((resp[i]))
# Original solution
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline


def cnt(i, oya_i):
    if len(ko[i]) == 1 and oya_i:
        return 1
    if memo[i][oya_i] != 0:
        return memo[i][oya_i]
    re = 1
    for ko_i in ko[i]:
        if ko_i == oya_i: continue
        re *= (cnt(ko_i, i) + 1)
    memo[i][oya_i] = re
    return re


def f(n, m):
    for _ in range(n - 1):
        x, y = list(map(int, input().split()))
        ko[x] += [y]
        ko[y] += [x]
    for start_i in range(1, n + 1):
        print((cnt(start_i, 0) % m))


n, m = list(map(int, input().split()))
memo = [[0] * (n + 1) for _ in range(n + 1)]
ko = [[] for _ in range(n + 1)]
f(n, m)


# Optimized solution
import sys

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


def cnt(i, oya_i):
    if len(ko[i]) == 1 and oya_i:
        return 1
    if memo[i][oya_i] != 0:
        return memo[i][oya_i]
    re = 1
    for ko_i in ko[i]:
        if ko_i == oya_i: continue
        re *= (cnt(ko_i, i) + 1)
    memo[i][oya_i] = re = re % m
    return re


def f(n, m):
    for _ in range(n - 1):
        x, y = list(map(int, input().split()))
        ko[x] += [y]
        ko[y] += [x]
    for start_i in range(1, n + 1):
        print((cnt(start_i, 0) % m))


n, m = list(map(int, input().split()))
memo = [[0] * (n + 1) for _ in range(n + 1)]
ko = [[] for _ in range(n + 1)]
f(n, m)
# Original solution
import sys
from collections import deque

def solve():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    Edge = [[] for _ in range(N)]
    for _ in range(N-1):
        x, y = list(map(int, input().split()))
        Edge[x-1].append(y-1)
        Edge[y-1].append(x-1)
    Par = [-1] * N
    Son = [[] for _ in range(N)]
    SonNum = [0] * N
    q = deque()
    for s in Edge[0]: q.append((s, 0))
    while q:
        nn, pn = q.popleft()
        Par[nn] = pn
        Son[pn].append(nn)
        SonNum[pn] += 1
        for ne in Edge[nn]:
            if ne != pn: q.append((ne, nn))

    DP = [1 for i in range(N)]
    visited = [0] * N
    for i, s in enumerate(SonNum):
        if s == 0 and Par[i] > -1:
            q.append((Par[i], 1))
            DP[i] = 1
    while q:
        nn, v = q.popleft()
        DP[nn] *= v + 1
        visited[nn] += 1
        if visited[nn] == SonNum[nn] and nn > 0: q.append((Par[nn], DP[nn]))

    for s in Son[0]: q.append((s, DP[0]))
    while q:
        nn, v = q.popleft()
        DP[nn] *= v // (DP[nn] + 1) + 1
        for s in Son[nn]: q.append((s, DP[nn]))

    for i, d in enumerate(DP): print((d % M))

    return 0

if __name__ == "__main__":
    solve()

# Optimized solution
import sys
from collections import deque

def solve():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    Edge = [[] for _ in range(N)]
    for _ in range(N-1):
        x, y = list(map(int, input().split()))
        Edge[x-1].append(y-1)
        Edge[y-1].append(x-1)
    Par = [-1] * N
    Son = [[] for _ in range(N)]
    SonNum = [0] * N
    q = deque()
    for s in Edge[0]: q.append((s, 0))
    while q:
        nn, pn = q.popleft()
        Par[nn] = pn
        Son[pn].append(nn)
        SonNum[pn] += 1
        for ne in Edge[nn]:
            if ne != pn: q.append((ne, nn))

    DP = [1 for i in range(N)]
    visited = [0] * N
    for i, s in enumerate(SonNum):
        if s == 0 and i > 0: q.append((Par[i], 1))
        
    while q:
        nn, v = q.popleft()
        DP[nn] *= v + 1
        visited[nn] += 1
        if visited[nn] == SonNum[nn] and nn > 0: q.append((Par[nn], DP[nn]))

    for s in Son[0]: q.append((s, DP[0]))
    while q:
        nn, v = q.popleft()
        DP[nn] *= (v // (DP[nn] + 1) + 1) % M
        for s in Son[nn]: q.append((s, DP[nn]))

    for i, d in enumerate(DP): print((d % M))

    return 0

if __name__ == "__main__":
    solve()
# Original solution
import sys,os,io
input = sys.stdin.readline
# input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

N, M = map(int, input().split())
edge = [[] for _ in range(N)]
for i in range(N-1):
  a,b = list(map(int, input().split()))
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)

def dfs(start):
  stack = [start]
  parent = [N]*N
  parent[start] = -1
  while stack:
    v = stack[-1]
    marker = 0
    for u in edge[v]:
      if u==parent[v]:
        continue
      if parent[u]==N: #子へ降ろす
        marker = 1
        parent[u]=v
        stack.append(u)
      else: #子から吸い上げる
        ans[v] *= ans[u]+1
        ans[v] %=M
    if marker==0:
      stack.pop()
  return

def dfs2(start):
  stack = [start]
  parent = [N]*N
  parent[start] = -1
  p_value = [0]*N
  while stack:
    v = stack.pop()
    cum1 = [1]*(len(edge[v])+1)
    cum2 = [1]*(len(edge[v])+1)
    for i,u in enumerate(edge[v]):
      if u==parent[v]:
        cum1[i+1] = cum1[i]*(p_value[v]+1)%M
      else:
        cum1[i+1] = cum1[i]*(ans[u]+1)%M
    for i,u in enumerate(edge[v][::-1]):
      if u==parent[v]:
        cum2[-i-2] = cum2[-i-1]*(p_value[v]+1)%M
      else:
        cum2[-i-2] = cum2[-i-1]*(ans[u]+1)%M
    for i,u in enumerate(edge[v]):
      if u==parent[v]:
        continue
      parent[u]=v
      p_value[u] = cum1[i]*cum2[i+1]%M
      ans[u] *= (p_value[u]+1)
      ans[u] %= M
      stack.append(u)
    ans[v] %= M
  return

ans = [1]*N
dfs(0)
dfs2(0)
print(*ans, sep='\n')


# Optimized solution
import sys,os,io
# input = sys.stdin.readline
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

N, M = map(int, input().split())
edge = [[] for _ in range(N)]
for i in range(N-1):
  a,b = list(map(int, input().split()))
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)

def dfs(start):
  stack = [start]
  parent = [N]*N
  parent[start] = -1
  while stack:
    v = stack[-1]
    marker = 0
    for u in edge[v]:
      if u==parent[v]:
        continue
      if parent[u]==N: #子へ降ろす
        marker = 1
        parent[u]=v
        stack.append(u)
      else: #子から吸い上げる
        ans[v] *= ans[u]+1
        ans[v] %=M
    if marker==0:
      stack.pop()
  return

def dfs2(start):
  stack = [start]
  parent = [N]*N
  parent[start] = -1
  p_value = [0]*N
  while stack:
    v = stack.pop()
    cum1 = [1]*(len(edge[v])+1)
    cum2 = [1]*(len(edge[v])+1)
    for i,u in enumerate(edge[v]):
      if u==parent[v]:
        cum1[i+1] = cum1[i]*(p_value[v]+1)%M
      else:
        cum1[i+1] = cum1[i]*(ans[u]+1)%M
    for i,u in enumerate(edge[v][::-1]):
      if u==parent[v]:
        cum2[-i-2] = cum2[-i-1]*(p_value[v]+1)%M
      else:
        cum2[-i-2] = cum2[-i-1]*(ans[u]+1)%M
    for i,u in enumerate(edge[v]):
      if u==parent[v]:
        continue
      parent[u]=v
      p_value[u] = cum1[i]*cum2[i+1]%M
      ans[u] *= (p_value[u]+1)
      ans[u] %= M
      stack.append(u)
    ans[v] %= M
  return

ans = [1]*N
dfs(0)
dfs2(0)
print(*ans, sep='\n')
# Original solution
n,m,*t=list(map(int,open(0).read().split()))
e=[[]for _ in range(n)]
for a,b in zip(*[iter(t)]*2):
  e[a-1]+=b-1,
  e[b-1]+=a-1,
o=[]
s=[0]
f=[1]*n
for v in s:
  o+=v,
  f[v]=0
  t=[]
  for w in e[v]:
    if f[w]:
      s+=w,
      t+=w,
  e[v]=t
dp1=[0]*n
c=[[]for _ in range(n)]
for v in o[::-1]:
  c1,c2=[1],[1]
  for w in e[v]:
    c1+=c1[-1]*(dp1[w]+1)%m,
  for w in e[v][::-1]:
    c2+=c2[-1]*(dp1[w]+1)%m,
  dp1[v]=c1[-1]
  c[v]=c1,c2[-2::-1]
dp2=[0]*n
dp2[0]=1
for v in o:
  t=dp2[v]
  for w,c1,c2 in zip(e[v],*c[v]):
    dp2[w]=(t*c1*c2+1)%m
for dp1,dp2 in zip(dp1,dp2):
  print((dp1*dp2%m))

# Optimized solution
n,m,*t=list(map(int,open(0).read().split()))
e=[[]for _ in range(n)]
for a,b in zip(*[iter(t)]*2):
  e[a-1]+=b-1,
  e[b-1]+=a-1,
o=[]
s=[0]
f=[1]*n
for v in s:
  o+=v,
  f[v]=0
  t=[]
  for w in e[v]:
    if f[w]:
      s+=w,
      t+=w,
  e[v]=t
dp1=[0]*n
c=[[]for _ in range(n)]
for v in o[::-1]:
  x,y=[1],[1]
  for w in e[v]:x+=x[-1]*(dp1[w]+1)%m,
  for w in e[v][::-1]:y+=y[-1]*(dp1[w]+1)%m,
  dp1[v]=x[-1]
  c[v]=x,y[-2::-1]
dp2=[1]+[0]*n
for v in o:
  for w,x,y in zip(e[v],*c[v]):dp2[w]=(dp2[v]*x*y+1)%m
for x,y in zip(dp1,dp2):print((x*y%m))
# Original solution
from collections import deque
from functools import reduce
def f(a):
    return (a + 1) % M
def g(a, b):
    return a * b % M
init = 1

N, M = list(map(int, input().split()))
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = list(map(int, input().split()))
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = deque([0])
T = []
while Q:
    i = deque.popleft(Q)
    T.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

BU = [0] * N # Bottom Up
TD = [1] * N # Top Down
for i in T[::-1]:
    BU[i] = reduce(g, [f(BU[j]) for j in X[i]]) if len(X[i]) else init

for i in T:
    B = [1] * (len(X[i]) + 1)
    for k, j in enumerate(X[i]):
        B[k+1] = g(B[k], f(BU[j]))
    a = init
    for k in range(len(X[i]))[::-1]:
        j = X[i][k]
        TD[j] = f(g(TD[i], g(a, B[k])))
        a = g(a, f(BU[j]))

for t in [g(a, b) for a, b in zip(BU, TD)]:
    print(t)

# Optimized solution
from collections import deque
from functools import reduce
f = lambda a: (a+1) % M
g = lambda a, b: a * b % M
init = 1

N, M = list(map(int, input().split()))
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = list(map(int, input().split()))
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = deque([0])
T = []
while Q:
    i = deque.popleft(Q)
    T.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

BU = [0] * N # Bottom Up
TD = [1] * N # Top Down
for i in T[::-1]:
    BU[i] = reduce(g, [f(BU[j]) for j in X[i]]) if len(X[i]) else init

for i in T:
    B = [1] * (len(X[i]) + 1)
    for k, j in enumerate(X[i]):
        B[k+1] = g(B[k], f(BU[j]))
    a = init
    for k in range(len(X[i]))[::-1]:
        j = X[i][k]
        TD[j] = f(g(TD[i], g(a, B[k])))
        a = g(a, f(BU[j]))

for t in [g(a, b) for a, b in zip(BU, TD)]:
    print(t)
# Original solution
from collections import deque
from functools import reduce
f = lambda a: (a+1) % M
g = lambda a, b: a * b % M
init = 1

N, M = list(map(int, input().split()))
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = list(map(int, input().split()))
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = deque([0])
T = []
while Q:
    i = deque.popleft(Q)
    T.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

BU = [0] * N # Bottom Up
TD = [1] * N # Top Down
for i in T[::-1]:
    BU[i] = reduce(g, [f(BU[j]) for j in X[i]]) if len(X[i]) else init

for i in T:
    B = [1] * (len(X[i]) + 1)
    for k, j in enumerate(X[i]):
        B[k+1] = g(B[k], f(BU[j]))
    a = init
    for k in range(len(X[i]))[::-1]:
        j = X[i][k]
        TD[j] = f(g(TD[i], g(a, B[k])))
        a = g(a, f(BU[j]))

for t in [g(a, b) for a, b in zip(BU, TD)]:
    print(t)


# Optimized solution
import sys
input = sys.stdin.readline

from collections import deque
from functools import reduce
f = lambda a: (a+1) % M
g = lambda a, b: a * b % M
init = 1

N, M = list(map(int, input().split()))
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = list(map(int, input().split()))
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = deque([0])
T = []
while Q:
    i = deque.popleft(Q)
    T.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

BU = [0] * N # Bottom Up
TD = [1] * N # Top Down
for i in T[::-1]:
    BU[i] = reduce(g, [f(BU[j]) for j in X[i]]) if len(X[i]) else init

for i in T:
    B = [1] * (len(X[i]) + 1)
    for k, j in enumerate(X[i]):
        B[k+1] = g(B[k], f(BU[j]))
    a = init
    for k in range(len(X[i]))[::-1]:
        j = X[i][k]
        TD[j] = f(g(TD[i], g(a, B[k])))
        a = g(a, f(BU[j]))

for t in [g(a, b) for a, b in zip(BU, TD)]:
    print(t)
# Original solution
import sys
input = sys.stdin.readline

from collections import deque
from functools import reduce
f = lambda a: (a+1) % M
g = lambda a, b: a * b % M
init = 1

N, M = list(map(int, input().split()))
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = list(map(int, input().split()))
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = deque([0])
T = []
while Q:
    i = deque.popleft(Q)
    T.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

BU = [0] * N # Bottom Up
TD = [1] * N # Top Down
for i in T[::-1]:
    BU[i] = reduce(g, [f(BU[j]) for j in X[i]]) if len(X[i]) else init

for i in T:
    B = [1] * (len(X[i]) + 1)
    for k, j in enumerate(X[i]):
        B[k+1] = g(B[k], f(BU[j]))
    a = init
    for k in range(len(X[i]))[::-1]:
        j = X[i][k]
        TD[j] = f(g(TD[i], g(a, B[k])))
        a = g(a, f(BU[j]))

for t in [g(a, b) for a, b in zip(BU, TD)]:
    print(t)

# Optimized solution
import sys
input = sys.stdin.readline
from collections import deque
N, M = list(map(int, input().split()))
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = list(map(int, input().split()))
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

BU = [0] * N
TD = [1] * N
for i in R[::-1]:
    a = 1
    for j in X[i]:
        a = a * (BU[j] + 1) % M
    BU[i] = a
for i in R:
    c = len(X[i])
    AL = [1] * (c + 1)
    AR = [1] * (c + 1)
    for j in range(c):
        AL[j+1] = AL[j] * (BU[X[i][j]] + 1) % M
    for j in range(c)[::-1]:
        AR[j] = AR[j+1] * (BU[X[i][j]] + 1) % M
    for j in range(c):
        TD[X[i][j]] = (TD[i] * AL[j] * AR[j+1] + 1) % M

for i in range(N):
    print((BU[i] * TD[i] % M))
# Original solution
# 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)
    for i, dpvi in enumerate(dp[v]):
        accl[i+1] = accl[i]*(dpvi+1)%MOD

    accr = [1]*(deg+1)
    for i, dpvir in enumerate(reversed(dp[v])):
        accr[-i-2] = accr[-i-1]*(dpvir+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)

# Optimized solution
# 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)