#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const int INF = (1<<30)-1;
const long long LINF = (1LL<<62)-1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<class T> class ReRooting {
  vector<T> res;
public:
  ReRooting(int n, vector<vector<int>> &_to, T identity, function<T(T, T)> operate, function<T(T, int)> add_node) {
    res.resize(n);
    if (n == 1) {
      res[0] = add_node(identity, 0);
      return;
    }
    vector<vector<int>> to(n), idx(n);
    for (int x = 0; x < n; x++) {
      for (int i = 0; i < _to[x].size(); i++) {
        int y = _to[x][i];
        if (x >= y)
          continue;
        idx[x].push_back(to[y].size());
        idx[y].push_back(to[x].size());
        to[x].push_back(y);
        to[y].push_back(x);
      }
    }
    vector<vector<T>> dp(n);
    for (int i = 0; i < n; i++)
      dp[i].resize(to[i].size());
    vector<int> ord = {0}, par(n);
    par[0] = -1;
    stack<int> st;
    st.push(0);
    while (!st.empty()) {
      int cur = st.top();
      st.pop();
      for (auto next : to[cur]) {
        if (next == par[cur])
          continue;
        par[next] = cur;
        ord.push_back(next);
        st.push(next);
      }
    }
    for (int i = n - 1; i > 0; i--) {
      int cur = ord[i], par_idx;
      T accum = identity;
      for (int j = 0; j < to[cur].size(); j++) {
        if (to[cur][j] == par[cur])
          par_idx = j;
        else
          accum = operate(accum, dp[cur][j]);
      }
      dp[par[cur]][idx[cur][par_idx]] = add_node(accum, cur);
    }
    for (int i = 0; i < n; i++) {
      int cur = ord[i];
      vector<T> r_accum(to[cur].size());
      r_accum.back() = identity;
      for (int j = (int)r_accum.size() - 1; j > 0; j--)
        r_accum[j-1] = operate(r_accum[j], dp[cur][j]);
      T l_accum = identity;
      for (int j = 0; j < r_accum.size(); j++) {
        T accum = operate(l_accum, r_accum[j]);
        dp[to[cur][j]][idx[cur][j]] = add_node(accum, cur);
        l_accum = operate(l_accum, dp[cur][j]);
      }
      res[cur] = add_node(l_accum, cur);
    }
  }
  T query(int root) {
    return res[root];
  }
};

long long mod_pow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

struct Data {
  ll num;
  int mod;
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n);
  rep(i, n-1) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ReRooting<Data> dp(n, to, Data({1, m}), [](auto x, auto y) {
    ll a = x.num, b = y.num;
    int mod = x.mod;
    return Data({a*b%mod, mod});
  }, [&](auto x, auto y) {
    x.num++;
    return x;
  });
  rep(i, n) {
    int res = (dp.query(i).num-1+m)%m;
    cout << res << '\n';
  }
  return 0;
}

Measured memory_bytes: 29304