#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int maxn = 1e5 + 5;
int n, mod;
vector<int> e[maxn];
ll ways[maxn];
ll mul(ll x, ll y) {
  return (x * y) % mod;
}
ll fastpow(ll x, ll y) {
  ll cur = x;
  ll ans = 1;
  while (y > 0) {
    if (y % 2 == 1) ans = mul(ans, cur);
    cur = mul(cur, cur);
    y /= 2;
  }
  return ans;
}
void precomp(int v, int pre) {
  ways[v] = 1;
  for (int u : e[v]) {
    if (u == pre) continue;
    precomp(u, v);
    ways[v] = mul(ways[v], 1 + ways[u]);
  }
}

ll ans[maxn];
void solve(int v, int pre, ll up) {
  ans[v] = mul(ways[v], 1 + up);
  vector<ll> pref, suff;
  suff.push_back(1);
  for (int i = e[v].size() - 1; i >= 0; i--) {
    if (e[v][i] == pre) continue;
    suff.push_back(mul(suff.back(), 1 + ways[e[v][i]]));
  }
  ll cur = 1 + up;
  int j = suff.size() - 2;
  for (int u : e[v]) {
    if (u == pre) continue;
    solve(u, v, mul(cur, suff[j]));
    cur = mul(cur, 1 + ways[u]);
    j--;
  }
}

int main() {
  ios::sync_with_stdio(false);
  // cin.tie(NULL);

  cin >> n >> mod;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    e[u].push_back(v);
    e[v].push_back(u);
  }
  precomp(1, 0);
  solve(1, 0, 0);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << '\n';
  }

  return 0;
}