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 = list(map(int, input().split()))

g = [[] for _ in range(n)]



for _ in range(n-1):

    x, y = list(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)

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 item you want by using self.depth, self.subsize, etc..

        # Default : self.parent

        return self.parent



    def rerooting(self) -> list:

        # Modify below if you want to use rerooting

        ans = [0]*n

        ans[self.root] = self.result[self.root]



        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



        s = [self.root]

        while s:

            v = s.pop()



            # accumulate nbhd's result from the left side

            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)



            # accumulate nbhd's result from the right side

            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]



            # calculate the sub-result for parent's subtree when you see the child as root.

            # get the answer for each child.

            for i, w in enumerate(self.g[v]):

                if w != self.parent[v]:

                    FromParent[w] = cum1[v][i]*cum2[v][i+1] % m

                    ans[w] = (FromParent[w]+1) * self.result[w]

                    ans[w] %= m

        return ans





n, m = list(map(int, input().split()))

g = [[] for _ in range(n)]



for _ in range(n-1):

    x, y = list(map(int, input().split()))

    g[x-1].append(y-1)

    g[y-1].append(x-1)



T1 = Tree(n, g, 0)



for x in T1.rerooting():

    print(x)

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 = list(map(int, input().split()))
g = [[] for _ in range(n)]
for _ in range(n - 1):
    x, y = list(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)

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 item you want by using self.depth, self.subsize, etc..
        # Default : self.parent
        return self.parent

    def rerooting(self) -> list:
        # Modify below if you want to use rerooting
        ans = [0] * n
        ans[self.root] = self.result[self.root]
        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
        s = [self.root]
        while s:
            v = s.pop()
            # accumulate nbhd's result from the left side
            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)
            # accumulate nbhd's result from the right side
            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]
            # calculate the sub-result for parent's subtree when you see the child as root.
            # get the answer for each child.
            for i, w in enumerate(self.g[v]):
                if w != self.parent[v]:
                    FromParent[w] = cum1[v][i] * cum2[v][i + 1] % m
                    ans[w] = (FromParent[w] + 1) * self.result[w]
                    ans[w] %= m
        return ans


n, m = list(map(int, input().split()))
g = [[] for _ in range(n)]
for _ in range(n - 1):
    x, y = list(map(int, input().split()))
    g[x - 1].append(y - 1)
    g[y - 1].append(x - 1)
T1 = Tree(n, g, 0)
for x in T1.rerooting():
    print(x)

-        # Edit the result you want using self.depth, self.subsize, etc.. +        # Edit the item you want by using self.depth, self.subsize, etc.. +        # Modify below if you want to use rerooting +        ans = [0] * n +        ans[self.root] = self.result[self.root] -        ans = [0] * n -        ans[0] = self.result[0] -            if v != self.root: -                ans[v] = (FromParent[v] + 1) * self.result[v] -                ans[v] %= m +            # accumulate nbhd's result from the left side +            # accumulate nbhd's result from the right side +            # calculate the sub-result for parent's subtree when you see the child as root. +            # get the answer for each child. -                FromParent[w] = cum1[v][i] * cum2[v][i + 1] % m +                if w != self.parent[v]: +                    FromParent[w] = cum1[v][i] * cum2[v][i + 1] % m +                    ans[w] = (FromParent[w] + 1) * self.result[w] +                    ans[w] %= m -ans = T1.rerooting()