// Charan Sriramula

#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll logx=30;
const ll N=1e5+5;
const ll M=1e3+5;
const ll mod=1e9+7;
const ll INF=1e18+5;
const double PI = 3.14159265358979323846;

#define ints(n) scanf("%d",&n)
#define intp(n) printf("%d\n",n)
#define longs(n) scanf("%intd",&n)
#define longp(n) printf("%intd\n",n)

inline ll mul(ll a, ll b,ll p){ return (a * 1ll * b) % p; }
inline ll sub(ll a, ll b,ll p){ ll c = a - b; if(c < 0) c += p; return c; }
inline ll add(ll a, ll b,ll p){ ll c = a + b; if(c > p) c -= p; return c; }

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x,y) make_pair(x,y)
#define GCD(a,b) __gcd((a),(b))
#define all(v) v.begin(),v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a,b) ((a)*(b))/GCD((a),(b))
#define ms(dp,val) memset(dp,val,sizeof(dp))
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
template<typename T> T power(T x,T y,ll m){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1ll;x=(x*x)%m;}return ans%m;}

#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl

ll n,m,dp[N][2],outdp[N],seg[4*N],idx[N];
vector<ll> vec,g[N],lev[N],subx[N];

void Build(ll low,ll high,ll pos)
{
  if(low==high)
  {
    seg[pos]=((dp[vec[low]][0]%m)+(dp[vec[low]][1]%m))%m;
    return;
  }
  ll mid=(low+high)/2;
  Build(low,mid,2*pos+1);
  Build(mid+1,high,2*pos+2);
  seg[pos]=((seg[2*pos+1]%m)*(seg[2*pos+2]%m))%m;
}

ll Query(ll low,ll high,ll qlow,ll qhigh,ll pos)
{
  if(qlow>high||qhigh<low) return 1;
  if(qlow<=low&&qhigh>=high) return seg[pos]%m;
  ll mid=(low+high)/2;
  return ((Query(low,mid,qlow,qhigh,2*pos+1)%m)*(Query(mid+1,high,qlow,qhigh,2*pos+2)%m))%m;
}

void DFS(ll cur,ll par,ll dep)
{
  ll ways=1;
  dp[cur][0]=1;
  lev[dep].pb(cur);
  for(auto i:g[cur])
  {
    if(i==par){continue;}
    DFS(i,cur,dep+1);
    subx[cur].pb(i);
    ways=mul(ways,((dp[i][0]%m)+(dp[i][1]%m))%m,m);
  }
  dp[cur][1]=ways;
}

void xDFS(ll cur,ll par,ll dep)
{
  if(cur==1){outdp[cur]=1;}
  else
  {
    ll p=lev[dep].size();
    if(p==1){outdp[cur]=((outdp[par]%m)+(dp[par][0]%m))%m;}
    else
    {
      ll pro=1,l=idx[subx[par][0]],r=idx[subx[par][subx[par].size()-1]],mid=idx[cur];
      ll lef=Query(0,n-1,l,mid-1,0);
      ll rig=Query(0,n-1,mid+1,r,0);
      pro=mul(pro,lef,m);
      pro=mul(pro,rig,m);
      pro=mul(pro,outdp[par],m);
      outdp[cur]=((pro%m)+(dp[par][0]%m))%m;
    }
  }
  for(auto i:g[cur])
  {
    if(i==par){continue;}
    xDFS(i,cur,dep+1);
  }
}

int main()
{
  fast;
  ll cnt=0;
  cin>>n>>m;
  for(ll i=1;i<n;i++)
  {
    ll u,v;
    cin>>u>>v;
    g[u].pb(v);
    g[v].pb(u);
  }
  DFS(1,0,0);
  for(ll i=0;i<n;i++)
  {
    for(auto j:lev[i])
    {
      vec.pb(j);
      idx[j]=cnt++;
    }
  }
  Build(0,n-1,0);
  xDFS(1,0,0);
  for(ll i=1;i<=n;i++)
  {
    ll ans=dp[i][1]%m;
    ans=mul(ans,outdp[i],m);
    cout<<ans<<endl;
  }
}


Measured memory_bytes: 29432