#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i32 = int32_t;
using i64 = int64_t;
constexpr char newl = '\n';

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
//#define F first
//#define S second
#define debug(x) cout << #x << ": " << x << '\n';
template<class T> void Print(vector<T> v) {
  F0R(i, v.size()) {
    cout << v[i] << ' ';
  }
  cout << newl;
}

#if 1

template<int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v% MOD) {
    if (val < 0) val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator - () const noexcept {
    return val ? MOD - val : 0;
  }
  constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp& operator += (const Fp& r) noexcept {
    val += r.val;
    if (val >= MOD) val -= MOD;
    return *this;
  }
  constexpr Fp& operator -= (const Fp& r) noexcept {
    val -= r.val;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr Fp& operator *= (const Fp& r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp& operator /= (const Fp& r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b; swap(a, b);
      u -= t * v; swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr bool operator == (const Fp& r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator != (const Fp& r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream& operator << (ostream& os, const Fp<MOD>& x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD>& a, long long n) noexcept {
    if (n == 0) return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1) t = t * a;
    return t;
  }
};


// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;

  vector<vector<int>> G(N, vector<int>());
  F0R(i, N - 1) {
    int x, y;
    cin >> x >> y;
    x--; y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  vector<int> dp(N);

  // 昇順、降順で乗算を記録する
  vector<vector<int>> ml(N), mr(N);

  auto dfs = [&](auto dfs, int v, int p)->int {

    i64 result = 1;

    ml[v].resize(G[v].size());
    mr[v].resize(G[v].size());

    F0R (i, G[v].size()) {
      auto c = G[v][i];
      if (c == p) {
        ml[v][i] = i > 0 ? ml[v][i - 1] : 1;
        continue;
      }

      result *= dfs(dfs, c, v) + 1;
      result %= M;

      ml[v][i] = result;
    }

    result = 1;
    RF0(i, G[v].size()) {
      auto c = G[v][i];
      if (c == p) {
        mr[v][i] = i < G[v].size()-1 ? mr[v][i + 1] : 1;
        continue;
      }

      result *= dp[c] + 1;
      result %= M;

      mr[v][i] = result;
    }

    return dp[v] = result;
  };
  dfs(dfs, 0, -1);
  //Print(dp);

  // 処理の流れはこれでいいんだけど、今回modは素数じゃないことも
  // あるから、そうするとmod除算はできない。なのでこの方法ではだめ
  /*
  auto dfs2 = [&](auto dfs2, int v, int p, int score)->void {
    
    if (score >= 0) {
      debug(score);
      dp[v] *= score + 1;
      dp[v] %= M;
    }

    for (auto c : G[v]) {
      if (c == p) continue;

      //cout << dp[v] << "/" << (dp[c] + 1) << "=" << fac.Div(dp[v], dp[c] + 1) << newl;
      dfs2(dfs2, c, v, (int)fac.Div(dp[v], dp[c] + 1));
    }
  };
  dfs2(dfs2, 0, -1, -1);
  Print(dp);
  */

  auto dfs3 = [&](auto dfs3, int v, int p, int parentScore)->void {

    if (parentScore >= 0) {
      //debug(parentScore);
      dp[v] = (int)((i64)dp[v] * (parentScore + 1) % M);
    }

    F0R(i, G[v].size()) {
      auto c = G[v][i];
      if (c == p) continue;

      // 子の分を割り、かわりにさっき計算に含められなかった親の分をかける
      i64 score = parentScore >= 0 ? parentScore + 1 : 1;
      if (i > 0) score = score * ml[v][i - 1] % M;
      if (i < G[v].size() - 1) score = score * mr[v][i + 1] % M;

      dfs3(dfs3, c, v, (int)score);
    }
  };
  dfs3(dfs3, 0, -1, -1);
  //Print(dp);

  F0R(i, N) {
    cout << dp[i] << newl;
  }
}
#endif


Measured memory_bytes: 25344