#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>(b);i--)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x3fffffffffffffff;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//template end

int mod=1e9+7;
struct Mint {
  int val;
  Mint inv() const{
    int tmp,a=val,b=mod,x=1,y=0;
    while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
    return Mint(x);
  }
public:
  Mint():val(0){}
  Mint(ll x):val(x>=0?x%mod:x%mod+mod){}
  Mint pow(ll t){Mint res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;}return res;}
  Mint& operator+=(const Mint& x){if((val+=x.val)>=mod)val-=mod;return *this;}
  Mint& operator-=(const Mint& x){if((val+=mod-x.val)>=mod)val-=mod; return *this;}
  Mint& operator*=(const Mint& x){val=(ll)val*x.val%mod; return *this;}
  Mint& operator/=(const Mint& x){return *this*=x.inv();}
  Mint operator+(const Mint& x) const{return Mint(*this)+=x;}
  Mint operator-(const Mint& x) const{return Mint(*this)-=x;}
  Mint operator*(const Mint& x) const{return Mint(*this)*=x;}
  Mint operator/(const Mint& x) const{return Mint(*this)/=x;}
};
struct factorial {
  vector<Mint> Fact, Finv;
public:
  factorial(int maxx){
    Fact.resize(maxx+1),Finv.resize(maxx+1); Fact[0]=Mint(1); rep(i,0,maxx)Fact[i+1]=Fact[i]*(i+1);
    Finv[maxx]=Mint(1)/Fact[maxx]; rrep(i,maxx,0)Finv[i-1]=Finv[i]*i;
  }
  Mint fact(int n,bool inv=0){if(inv)return Finv[n];else return Fact[n];}
  Mint nPr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[n-r];}
  Mint nCr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[r]*Finv[n-r];}
};

template< typename Data, typename T >
struct ReRooting {
  struct Node { int to, rev; Data data; };
  using F1 = function< T(T, T) >; using F2 = function< T(T, Data) >;
  vector<vector<Node>> g; vector<vector<T>> ldp, rdp; vector<int> lptr,rptr;
  const F1 f1; const F2 f2; const T ident;
  ReRooting(int n, const F1 &f1, const F2 &f2, const T &ident) :
      g(n), ldp(n), rdp(n), lptr(n), rptr(n), f1(f1), f2(f2), ident(ident) {}
  void add_edge(int u, int v, const Data &d) {
    g[u].emplace_back((Node) {v, (int) g[v].size(), d});
    g[v].emplace_back((Node) {u, (int) g[u].size() - 1, d});
  }
  void add_edge_bi(int u, int v, const Data &d, const Data &e) {
    g[u].emplace_back((Node) {v, (int) g[v].size(), d});
    g[v].emplace_back((Node) {u, (int) g[u].size() - 1, e});
  }
  T dfs(int idx, int par) {
    while(lptr[idx] != par && lptr[idx] < g[idx].size()) {
      auto &e = g[idx][lptr[idx]];
      ldp[idx][lptr[idx] + 1] = f1(ldp[idx][lptr[idx]], f2(dfs(e.to, e.rev), e.data));
      ++lptr[idx];
    }
    while(rptr[idx] != par && rptr[idx] >= 0) {
      auto &e = g[idx][rptr[idx]];
      rdp[idx][rptr[idx]] = f1(rdp[idx][rptr[idx] + 1], f2(dfs(e.to, e.rev), e.data));
      --rptr[idx];
    }
    if(par < 0) return rdp[idx][0];
    return f1(ldp[idx][par], rdp[idx][par + 1]);
  }
  vector<T> solve() {
    rep(i,0,g.size()){
      ldp[i].assign(g[i].size() + 1, ident);
      rdp[i].assign(g[i].size() + 1, ident);
      lptr[i] = 0;
      rptr[i] = (int) g[i].size() - 1;
    }
    vector<T> ret;
    rep(i,0,g.size())ret.push_back(dfs(i, -1));
    return ret;
  }
};

int main(){
  int n; scanf("%d%d",&n,&mod);
  auto f1=[](Mint a,Mint b){return a*b;};
  auto f2=[](Mint a,int b){return a+1;};
  ReRooting<int,Mint> g(n,f1,f2,1);
  rep(i,0,n-1){
    int a,b; scanf("%d%d",&a,&b); a--; b--;
    g.add_edge(a,b,0);
  }
  vector<Mint> ans=g.solve();
  rep(i,0,n)printf("%d\n",ans[i].val);
  return 0;
}

Measured memory_bytes: 36860