# coding: utf-8
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def f2(n):

    dp = [[0] * n for _ in range(n)]
    s = [1] * n

    def dfs(i, j):  # from i to j
        dpi = dp[i]
        if dpi[j]:
            return(dpi[j])
        dpij = 1
        for u in U[i]:
            if u != j:
                dpij = dpij * (dfs(u, i) + 1) % M
        s[j] = s[j] * (dpij + 1) % M
        dpi[j] = dpij
        return(dpij)

    for i in range(n):
        for u in U[i]:
            dfs(u, i)
    print("\n".join([str(x) for x in s]))

N, M = map(int, input().split()) # 1 <= N <= 10^5, 2 <= M <= 10^9
U = [set() for _ in range(N)]
for x, y in [map(int, input().split()) for _ in range(N - 1)]:
    U[x - 1].add(y - 1)
    U[y - 1].add(x - 1)
f2(N)
