### BUGGY CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    E=[[] for _ in range(n)]
    for _ in range(n-1):
        x,y=map(int,input().split())
        x-=1; y-=1
        E[x].append(y)
        E[y].append(x)

    # 木DP(0-rooted tree)
    dp=[1]*n
    def dfs1(v,p)->NoneType:
        for nv in E[v]:
            if(nv==p): continue
            dfs1(nv,v)
            dp[v]*=(dp[nv]+1)
            dp[v]%=m
    dfs1(0,-1)

    # 全方位木DP
    ans=[1]*n
    def dfs2(v,p):
        # dpがv-rootedの状態なので、答えを確定させる
        for nv in E[v]:
            ans[v]*=dp[nv]+1
            ans[v]%=m
        # vの子nvに対して、nvの処理を行う前に、dp[v]をnv-rootedに変更する
        k=len(E[v])
        l=[0]*k
        r=[0]*k
        for i in range(k):
            l[i]=r[i]=dp[E[v][i]]+1
        for i in range(1,k):
            l[i]*=l[i-1]
            l[i]%=m
        for i in range(k-2,-1,-1):
            r[i]*=r[i+1]
            r[i]%=m
        for i in range(k):
            if(E[v][i]==p): continue
            dp[v]=1
            if(i>0): dp[v]*=l[i-1]
            if(i<k-1): dp[v]*=r[i+1]
            dp[v]%=m
            dfs2(E[v][i],v)
    dfs2(0,-1)

    # output
    print(*ans,sep='\n')
resolve()

### FIXED CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n, MOD = map(int,input().split())
    if n == 1:
        print(1)
        return

    E = [[] for _ in range(n)]
    nv_to_idx = [{} for _ in range(n)]
    for _ in range(n - 1):
        u, v = map(int,input().split())
        u -= 1; v -= 1
        nv_to_idx[u][v] = len(E[u])
        nv_to_idx[v][u] = len(E[v])
        E[u].append(v)
        E[v].append(u)
    deg = [len(E[v]) - 1 for v in range(n)]
    deg[0] += 1

    topological_sort = []
    parent = [None] * n
    stack = [(~0, -1), (0, -1)] # (v, p)
    while stack:
        v, p = stack.pop()
        if v >= 0:
            parent[v] = p
            for nv in E[v]:
                if nv == p:
                    continue
                stack.append((~nv, v))
                stack.append((nv, v))
        else:
            if deg[~v] == 0:
                topological_sort.append(~v)
            deg[p] -= 1

    # tree DP
    dp = [None] * n
    for v in topological_sort:
        res = 1
        for nv in E[v]:
            if nv == parent[v]:
                continue
            res = res * (dp[nv] + 1) % MOD
        dp[v] = res

    # rerooting
    def rerooting(v, p):
        # rerooting を先に済ませる
        if p != -1 and v != -1:
            idx = nv_to_idx[p][v]
            p_res = 1
            if idx > 0:
                p_res *= cum_left[p][idx - 1]
            if idx < len(E[p]) - 1:
                p_res *= cum_right[p][idx + 1]
            dp[p] = p_res % MOD

        if v == -1:
            return
        # monoid に対する cumulative sum を計算
        if cum_left[v] is None:
            left = [(dp[nv] + 1) % MOD for nv in E[v]]
            right = [(dp[nv] + 1) % MOD for nv in E[v]]
            k = len(E[v])
            for i in range(k - 1):
                left[i + 1] *= left[i]
                left[i + 1] %= MOD
            for i in range(k - 1, 0, -1):
                right[i - 1] *= right[i]
                right[i - 1] %= MOD
            cum_left[v] = left
            cum_right[v] = right
        dp[v] = cum_left[v][-1]

    ans = [None] * n
    ans[0] = dp[0]
    stack = [(~0, -1), (0, -1)]
    cum_left = [None] * n
    cum_right = [None] * n
    while stack:
        v, p = stack.pop()
        if v >= 0:
            rerooting(v, p)
            ans[v] = dp[v]
            for nv in E[v]:
                if nv == p:
                    continue
                stack.append((~nv, v))
                stack.append((nv, v))
        else:
            rerooting(p, ~v)

    print(*ans, sep = '\n')
resolve()