output_description: 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.
  
  * * *
input_description: 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}
attempt: # これは逆元を用いてやる？
  # この問題の場合、逆元をうまく設定するのが難しいので
  # 左右からのmergeになりそう
  from collections import deque
  import sys
  
  input = sys.stdin.buffer.readline
  
  
  n, m = map(int, input().split())
  # 各々の頂点の次数
  deg = [0] * (n + 1)
  # 隣接リストにグラフをまとめる
  graph = [[] for _ in range(n + 1)]
  for _ in range(n - 1):
      x, y = map(int, input().split())
      graph[x].append(y)
      graph[y].append(x)
      deg[x] += 1
      deg[y] += 1
  
  # dp1は葉向きに見たときの塗り方
  # 自身が黒い時、下の候補はどうなるか？
  dp1 = [0] * (n + 1)
  
  # dp2は根向きに見たときの塗り方
  # 自身が黒い時、親側の候補はどうなるか？
  dp2 = [0] * (n + 1)
  
  # 1回目
  start = 1
  par = [-1] * (n + 1)
  que = deque([start])
  topo = []
  while que:
      v = que.popleft()
      topo.append(v)
      for i in range(deg[v]):
          u = graph[v][i]
          if par[v] == u:
              continue
          par[u] = v
          que.append(u)
  
  # dpするついでにi=0からの積とi=deg[v]-1からの積を取っておくか。
  dpl = [[] for _ in range(n + 1)]
  dpr = [[] for _ in range(n + 1)]
  
  # 頂点番号から何番目かがわかるようにしたいから辞書で持っておく？
  edge_num = [{} for _ in range(n + 1)]
  
  for v in topo[::-1]:
      temp = 1
      for i in range(deg[v]):
          u = graph[v][i]
          edge_num[v][
              u
          ] = i  # edge_num[v]で頂点vに関する辞書。頂点uからidxが欲しいので記録
          if u == par[v]:
              dpl[v].append(temp)
              continue
          temp *= dp1[u] + 1
          temp %= m
          dpl[v].append(temp)
      dp1[v] = temp
      temp = 1
      for i in range(deg[v] - 1, -1, -1):
          u = graph[v][i]
          if u == par[v]:
              dpr[v].append(temp)
              continue
          temp *= dp1[u] + 1
          temp %= m
          dpr[v].append(temp)
  # これで普通の木DPに加えて
  # 左右からの累積積
  # 頂点番号からidxへの辞書
  # を獲得したはず
  
  # dpは欲しい答えとしておく
  # dp1とdp2から求める
  dp = [0] * (n + 1)
  
  for v in topo:
      # dp2の計算
      p = par[v]
      if p == -1:  # 例外、親がいない
          dp[v] = dp1[v]
          dp[v] %= m
          continue
      # 根側の処理
      # 自分以外の積を作りたい
      idx = edge_num[p][v]
      num = deg[p]
      dp2[v] = 1
      if idx - 1 >= 0:
          dp2[v] *= dpl[p][idx - 1]
          dp2[v] %= m
      if num - 2 - idx >= 0:
          dp2[v] *= dpr[p][num - 2 - idx]
          dp2[v] %= m
      # 根側の根側の処理
      pp = par[p]  # 親の親
      if pp != -1:  # 親の親がいるならば
          dp2[v] *= dp2[p] + 1  # 白=1か黒=dp2[pp]か
          dp2[v] %= m
      # dpの計算
      dp[v] = dp1[v] * (dp2[v] + 1)
      dp[v] %= m
  print(*dp[1:], sep="\n")
  
problem_description: 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.
samples: [{"input": "3 100\n    1 2\n    2 3", "output": "3\n    4\n    3\n    \n\nThere are seven ways to paint the vertices, as shown in the figure below.\nAmong them, there are three ways such that Vertex 1 is black, four ways such\nthat Vertex 2 is black and three ways such that Vertex 3 is black.\n\n![](https://img.atcoder.jp/dp/subtree_0_muffet.png)\n\n* * *"}, {"input": "4 100\n    1 2\n    1 3\n    1 4", "output": "8\n    5\n    5\n    5\n    \n\n* * *"}, {"input": "1 100", "output": "1\n    \n\n* * *"}, {"input": "10 2\n    8 5\n    10 8\n    6 5\n    1 5\n    4 8\n    2 10\n    3 6\n    9 2\n    1 7", "output": "0\n    0\n    1\n    1\n    1\n    0\n    1\n    0\n    1\n    1\n    \n\nBe sure to print the answers modulo M."}]