### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline


class Tree:
    def __init__(self, crdinal, adjgrph, root):
        self.n = crdinal
        self.g = adjgrph
        self.root = root
        self.parent = [-1]*self.n
        self.depth = [-1]*self.n
        self.subsize = [1]*self.n
        self.result = [1]*self.n  # 0 : additive, 1 : multiplicative

        s = [self.root]
        while s:
            v = s.pop()
            if self.depth[v] == -1:
                if v == self.root:
                    self.depth[v] = 0
                else:
                    self.depth[v] = self.depth[self.parent[v]] + 1
                    s.append(v)
                for w in self.g[v]:
                    if self.depth[w] == -1:
                        self.parent[w] = v
                        s.append(w)
            else:
                self.subsize[self.parent[v]] += self.subsize[v]
                self.calc(v)

    def calc(self, v):
        # The following formula is calculated by bottom-up:
        self.result[self.parent[v]] *= self.result[v]+1
        self.result[self.parent[v]] %= m
        return None

    def wanted(self):
        # Edit the result you want using self.depth, self.subsize, etc..
        # Default : self.parent
        return self.parent

    def rerooting(self) -> list:
        cum1 = [[1] for i in range(n)]  # 0 : additive, 1 : multiplicative
        cum2 = [[1] for i in range(n)]  # 0 : additive, 1 : multiplicative
        SisterAll = [{} for _ in range(n)]
        ans = [0]*n
        ans[0] = self.result[0]

        s = [self.root]
        while s:
            v = s.pop()
            if v != self.root:
                ans[v] = SisterAll[v][self.parent[v]]*self.result[v]
                ans[v] %= m

            for w in self.g[v]:
                if w != self.parent[v]:
                    cum1[v].append(cum1[v][-1]*(self.result[w]+1))
                    s.append(w)
                else:
                    cum1[v].append(cum1[v][-1]*SisterAll[v][w])
            for w in self.g[v][::-1]:
                if w != self.parent[v]:
                    cum2[v].append(cum2[v][-1]*(self.result[w]+1))
                else:
                    cum2[v].append(cum2[v][-1]*SisterAll[v][w])
            cum2[v] = cum2[v][::-1]

            for i, w in enumerate(self.g[v]):
                SisterAll[w][v] = cum1[v][i]*cum2[v][i+1]+1
        return ans


n, m = map(int, input().split())
g = [[] for _ in range(n)]

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


T1 = Tree(n, g, 0)

ans = T1.rerooting()


for x in T1.rerooting():
    print(x)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline


class Tree:
    def __init__(self, crdinal, adjgrph, root):
        self.n = crdinal
        self.g = adjgrph
        self.root = root
        self.parent = [-1]*self.n
        self.depth = [-1]*self.n
        self.subsize = [1]*self.n
        self.result = [1]*self.n  # 0 : additive, 1 : multiplicative

        s = [self.root]
        while s:
            v = s.pop()
            if self.depth[v] == -1:
                if v == self.root:
                    self.depth[v] = 0
                else:
                    self.depth[v] = self.depth[self.parent[v]] + 1
                    s.append(v)
                for w in self.g[v]:
                    if self.depth[w] == -1:
                        self.parent[w] = v
                        s.append(w)
            else:
                self.subsize[self.parent[v]] += self.subsize[v]
                self.calc(v)

    def calc(self, v):
        # The following formula is calculated by bottom-up:
        self.result[self.parent[v]] *= self.result[v]+1
        self.result[self.parent[v]] %= m
        return None

    def wanted(self):
        # Edit the result you want using self.depth, self.subsize, etc..
        # Default : self.parent
        return self.parent

    def rerooting(self) -> list:
        cum1 = [[1] for i in range(n)]  # 0 : additive, 1 : multiplicative
        cum2 = [[1] for i in range(n)]  # 0 : additive, 1 : multiplicative
        FromParent = [0]*n

        ans = [0]*n
        ans[0] = self.result[0]

        s = [self.root]
        while s:
            v = s.pop()
            if v != self.root:
                ans[v] = (FromParent[v]+1)*self.result[v]
                ans[v] %= m

            for w in self.g[v]:
                if w != self.parent[v]:
                    cum1[v].append(cum1[v][-1]*(self.result[w]+1) % m)
                    s.append(w)
                else:
                    cum1[v].append(cum1[v][-1]*(FromParent[v]+1) % m)
            for w in self.g[v][::-1]:
                if w != self.parent[v]:
                    cum2[v].append(cum2[v][-1]*(self.result[w]+1) % m)
                else:
                    cum2[v].append(cum2[v][-1]*(FromParent[v]+1) % m)
            cum2[v] = cum2[v][::-1]

            for i, w in enumerate(self.g[v]):
                FromParent[w] = cum1[v][i]*cum2[v][i+1] % m
        return ans


n, m = map(int, input().split())
g = [[] for _ in range(n)]

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


T1 = Tree(n, g, 0)

ans = T1.rerooting()


for x in T1.rerooting():
    print(x)