Problem:
Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices.

You are given a positive integer M.
For each v (1 \leq v \leq N), answer the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq x_i, y_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print N lines.
The v-th (1 \leq v \leq N) line should contain the answer to the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Sample Input 1

3 100
1 2
2 3

Sample Output 1

3
4
3

There are seven ways to paint the vertices, as shown in the figure below.
Among them, there are three ways such that Vertex 1 is black, four ways such that Vertex 2 is black and three ways such that Vertex 3 is black.

Sample Input 2

4 100
1 2
1 3
1 4

Sample Output 2

8
5
5
5

Sample Input 3

1 100

Sample Output 3

1

Sample Input 4

10 2
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4

0
0
1
1
1
0
1
0
1
1

Be sure to print the answers modulo M.

Solution in PyPy3 (7.3.0) (judge verdict: Runtime Error)

from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

from itertools import accumulate, combinations
from functools import lru_cache

M = mod = 10 ** 9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)

def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]




n, m = li()

if n == 1:
    print(1 % m)
    exit()

g = defaultdict(set)

for i in range(n - 1):
    a, b = li()
    g[a].add(b)
    g[b].add(a)


for i in g:
    if len(g[i]) == 1:
        root = i
        break

dp = [0] * (n + 1)

def dfs(root = 1, par = -1):
    global dp
    currval = 1
    atleast1 = 0
    for j in g[root]:
        if j != par:
            atleast1 = 1
            currval = (currval * (1 + dfs(j, root))) % mod
    
    if not atleast1:ans = 1 % mod
    else:ans = (currval) % mod

    dp[root] = ans
    return ans

def dfs2(root, par):
    # print(root, par, dp[root], dp[par])


    dp[root] = dp[root] * (dp[par] * inv_mod(dp[root] + 1) + 1  )
    dp[root] = dp[root] % mod
    for j in g[root]:
        if j != par:dfs2(j, root)



dfs(root, -1)



dfs2(g[root].pop(), root)



for i in dp[1:]:print(i % m)