//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 922337203685477580LL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

int mod;
//const int mod = 998244353;

struct mint {
  int64_t x;
  mint(int64_t x):x((x%mod+mod)%mod){}
  mint():x(0) {}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(int64_t t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  //for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) {return mint(*this) /= a;}
};

istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

const int N = 1e5+10;

//head

int n;
vi G[N];
vector<pair<mint, mint>> info[N];

inline mint sum(pair<mint, mint> &p) {return p.first + p.second;}

pair<mint, mint> dfs1(int i, int j) {
  mint b(1), w(1);
  int siz = G[i].size();
  info[i].resize(siz);
  rep(id, siz) {
    int ne = G[i][id];
    if(ne == j) continue;
    mint bb, ww;
    tie(bb, ww) = dfs1(ne, i);
    info[i][id] = make_pair(bb, ww);
    b *= bb+ww;
    w *= ww;
  }
  return make_pair(b, w);
}
void dfs2(int i, int j, mint b, mint w) {
  //cout << P(i, j) << ' ' << b << ' ' << w << endl;
  int siz = G[i].size();
  vector<mint> lefb(siz+1), righb(siz+1), lefw(siz+1), righw(siz+1);
  lefb[0] = lefw[0] = righb[siz] = righw[siz] = 1;
  rep(id, siz) {
    if(G[i][id] == j) {
      lefb[id+1] = lefb[id] * (b+w);
      lefw[id+1] = lefw[id] * w;
    }
    else {
      lefb[id+1] = lefb[id] * sum(info[i][id]);
      lefw[id+1] = lefw[id] * info[i][id].second;
    }
    if(G[i][siz-id-1] == j) {
      righb[siz-id-1] = righb[siz-id] * (b+w);
      righw[siz-id-1] = righw[siz-id] * w;
    }
    else {
      righb[siz-id-1] = righb[siz-id] * sum(info[i][siz-id-1]);
      righw[siz-id-1] = righw[siz-id] * info[i][siz-id-1].second;
    }
  }
  rep(id, siz) {
    int ne = G[i][id];
    if(ne == j) {
      info[i][id] = make_pair(b, w);
      continue;
    }
    mint bb = lefb[id] * righb[id+1];
    mint ww = lefw[id] * righw[id+1];
    dfs2(ne, i, bb, ww);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> mod;
  rep(i, n-1) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }
  dfs1(0, -1);
  dfs2(0, -1, 0, 1);


  rep(i, n) {
    mint ans(1);
    rep(id, info[i].size()) ans *= sum(info[i][id]);
    cout << ans << '\n';
  }
}

Measured memory_bytes: 36748