#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) for (long long i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (long long i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) begin(obj), end(obj)
#define RALL(obj) rbegin(obj), rend(obj)
#define fi first
#define se second
using ii = pair<int, int>;
vector<ii> dirs = {
  {1, 0}, {0, 1}, {-1, 0}, {0, -1},  // 4方向
  {1, 1}, {-1, 1}, {-1, -1}, {1, -1},  // 斜め
  {0, 0},  // 自身
};
template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

// debug
template <class T> ostream& operator<<(ostream &s, vector<T>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; }
template <class T> ostream& operator<<(ostream &s, vector<vector<T>>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; }
template <class T, class S> ostream& operator<<(ostream &s, pair<T, S>& p) { s << "{" << p.first << ", " << p.second << "}"; return s; }
template <class T, class S> ostream& operator<<(ostream& s, map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
#ifdef _MY_DEBUG
  #define dump(...) cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), cerr << "*/\n\n";
#else
  #define dump(...)
  #define endl "\n"
#endif
void dump_func() { cerr << endl; }
template <class Head, class... Tail> void dump_func(Head&& h, Tail&&... t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); }

struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
constexpr int MOD = 1000000007;
// *************** TEMPLATE END *************** 

template <class T>
T pow(T x, int n, const T UNION = 1) {
  T ret = UNION;
  while (n) {
    if (n & 1) ret *= x;
    x *= x; n >>= 1;
  }
  return ret;
}

struct ModInt {
  static int MD;
  static void set_mod(int mod) { MD = mod; }
  int x;
  ModInt() : x(0) {}
  ModInt(int x_) { if ((x = x_ % MD + MD) >= MD) x -= MD; }

  ModInt& operator+=(ModInt that) { if ((x += that.x) >= MD) x -= MD; return *this; }
  ModInt& operator*=(ModInt that) { x = (unsigned long long)x * that.x % MD; return *this; }
  ModInt& operator-=(ModInt that) { if ((x -= that.x) < 0) x += MD; return *this; }
  ModInt& operator/=(ModInt that) { x = (unsigned long long)x * that.inv().x % MD; return *this; }

  ModInt operator-() const { return -x < 0 ? MD - x : -x; }
  ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
  ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
  ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
  ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
  bool operator==(ModInt that) const { return x == that.x; }
  bool operator!=(ModInt that) const { return x != that.x; }
  ModInt inv() const { return pow(*this, MD - 2); }
  friend ostream& operator<<(ostream& s, ModInt a) { s << a.x; return s; }
  friend istream& operator>>(istream& s, ModInt& a) { s >> a.x; return s; }
};
int ModInt::MD = 1000000007;
using mint = ModInt;

signed main() {
  int n, m; cin >> n >> m;
  mint::set_mod(m);
  vector<vector<int>> g(n);
  {
    vector<vector<int>> gg(n);
    REP (i, n - 1) {
      int u, v; cin >> u >> v;
      u--; v--;
      gg[u].push_back(v);
      gg[v].push_back(u);
    }
    function<void(int, int)> dfs0 = [&](int v, int p) {
      for (int s: gg[v]) if (s != p) {
        g[v].push_back(s);
        dfs0(s, v);
      }
    };
    dfs0(0, -1);
  }

  vector<mint> dp(n, 1);
  function<void(int, int)> dfs1 = [&](int v, int p) {
    for (int s: g[v]) if (s != p) {
      dfs1(s, v);
      dp[v] *= dp[s] + 1;
    }
  };
  dfs1(0, -1);

  vector<mint> rev(n), ans(n);
  ans[0] = dp[0];
  function<void(int, int)> dfs2 = [&](int v, int p) {
    int k = g[v].size();
    vector<vector<mint>> acc(2, vector<mint>(k + 1, 1));
    REP (i, k) acc[0][i + 1] = acc[0][i] * (dp[g[v][i]] + 1);
    for (int i = k - 1; i >= 0; i--) acc[1][i] = acc[1][i + 1] * (dp[g[v][i]] + 1);
    REP (i, k) {
      int s = g[v][i];
      rev[s] = (rev[v] + 1) * acc[0][i] * acc[1][i + 1];
      ans[s] = (rev[s] + 1) * dp[s];
      dfs2(s, v);
    }
  };
  dfs2(0, -1);

  REP (i, n) cout << ans[i] << endl;
}


Measured memory_bytes: 26748