/// __Macro's__
#include <bits/stdc++.h>
using namespace std;
#define ll                   long long
#define II                   pair<int,int>
#define III                  pair<int, II>
#define VL                   vector<ll>
#define VI                   vector<int>
#define VII                  vector<II>
#define VIII                 vector<III>
#define VVI                  vector<vector<int>>
#define VVII                 vector<vector<II>>
#define fr                   first
#define sc                   second
#define mkpr                 make_pair
#define PQ                   priority_queue
#define pb                   push_back
#define eb                   emplace_back
#define all(v)               v.begin(), v.end()
#define LINE                 putc('\n', stdout)
#define SPACE                putc(' ' , stdout)
#define TAB                  putc('\t', stdout)
#define fillarr(arr, val)    fill((int*)arr, (int*)arr+(sizeof(arr)/sizeof(int)), val)
#define Log2(x)              (31^__builtin_clz(x))
#define INF                  1000000007 
#define MOD                  1000000007 
#define getint ReadInt()
const char IO_MODE = 0; // in-->00<--out (MSB-->LSB) | 0: Norm, 1: Fast 
inline ll ReadInt(){ll x=0,s=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')s=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';
c=getchar();}return s*x;}inline void WriteInt(ll x){char c[20];if(!x){putchar('0');return;}if(x<0)putchar('-'),x=-x;int i=0;while(x>0)
c[++i]=x%10,x/=10;while(i)putchar(c[i--]+48);}template <typename T>inline void out(T x){if(IO_MODE&1)WriteInt(x);else if(typeid(x)==typeid(int))
printf("%i", x);else printf("%lld", (ll)x);}template<typename T, typename... Args>inline void out(T x,Args...args){out(x);SPACE;out(args...);}
template <typename T>inline void in(T &x){if(IO_MODE&2) x=ReadInt();else if(typeid(x)==typeid(int))scanf("%i",&x);else if(typeid(x)==typeid(ll))
scanf("%lld",&x);}template<typename T, typename... Args>inline void in(T &x,Args&...args){in(x);in(args...);}
int aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,mm,nn,oo,pp,qq,rr,ss,tt,uu,vv,ww,xx,yy,zz;
int tc;
///
#define int long long 
int n,m;
VVI adjList;
int ans[100100];
int par[100100];
vector<vector<int>> childs, prefix, suffix;

int dfs(int u, int p){
  sort(all(adjList[u]));

  for(auto v:adjList[u])
    if(v!=p) childs[u].pb( dfs(v,u) ) ;
    else     childs[u].pb( 0 );

  prefix[u].resize(childs[u].size());
  suffix[u].resize(childs[u].size());

  int res = 1;
  for(int i=0 ; i<childs[u].size() ; i++)
    prefix[u][i] = res = (res + res*childs[u][i]%m)%m;

  res =1 ;
  for(int i=(int)childs[u].size()-1 ; i>=0 ; i--)
    suffix[u][i] = res = (res + res*childs[u][i]%m)%m;

  return ans[u] = res;
}
void calc(int u, int p){

  int idx = lower_bound(all(adjList[p]), u) - adjList[p].begin();

  int left  = idx==0 ? 1 : prefix[p][idx-1];
  int right = idx==suffix[p].size()-1 ? 1 : suffix[p][idx+1];
  
  par[u] = left*right%m;
  par[u] = (par[u] + par[u]*par[p]%m)%m;

  ans[u] = (ans[u] + ans[u]*par[u]%m)%m;

  for(auto v:adjList[u])
    if(v!=p)
      calc(v, u);
}
main()
{
  in(n,m);
  adjList.resize(n); childs.resize(n); prefix.resize(n); suffix.resize(n);

  for(int i=1 ; i<n ; i++){
    int u=getint-1, v=getint-1;
    adjList[u].pb(v);
    adjList[v].pb(u);
   }
  dfs(0,0);

  for(auto v:adjList[0])
    calc(v, 0);

  for(int i=0 ; i<n ; i++)
    cout << ans[i] << endl;

  return 0;
}

Measured memory_bytes: 30336