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)

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)
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)

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)

+import sys + +input = sys.stdin.readline