#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#include <ext/pb_ds/assoc_container.hpp> // Common file  
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional> // for less   
using namespace __gnu_pbds;  
using namespace std;  
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> set1;
ll find(ll a,ll p[])
{
   //cout<<"find "<<a<<endl;
   if(a==p[a])
   return a;
   else return p[a]=find(p[a],p);

}
void join(ll a,ll b,ll p[],ll sz[])
{
   a=find(a,p);
   b=find(b,p);
   if(a!=b)
   {
      if(sz[a]<sz[b])
      swap(a,b);
      sz[a]+=sz[b];
      p[b]=a;
   }
}
ll power(ll a,ll b,ll mod)
{
   ll res=1;
   while(b>0)
   {
      if(b&1)
      res=(res*a)%mod;
      b=b/2;
      a=(a*a)%mod;
   }
   return res;
}
ll inverse(ll x, ll p)
{
   return power(x,p-2,p);
}
ll hash1(ll x,ll y)
{
   return x+y+x*y;
}
ll f[200000]={0};
ll mod=1000000007;
void ini()
{
   f[0]=1;
   for(ll i=1;i<=200000;i++)
   f[i]=(i*f[i-1])%mod;
}



ll ncr(ll n,ll r)
{
   ll res=1;
   ll k=f[n];
   ll v=inverse(f[n-r],mod);
   ll u=inverse(f[r],mod);
   ll x=(u*v)%mod;
   k=(k*x)%mod;
   return k;
}
void dfs(ll v,ll p,vector<ll> adj[],ll dp[],ll m,vector<ll> left[],vector<ll> right[])
{
   ll k=adj[v].size();
  // if(k==1 && v!=1)
   dp[v]=1;
   for(ll i=0;i<k;i++)
   {
      ll s=adj[v].at(i);
      if(s!=p)
      {
         dfs(s,v,adj,dp,m,left,right);
         dp[v]=(dp[v]*(dp[s]+1))%m;
      }
   }
  
      
    
 //  cout<<"dp of "<<v<<" "<<dp[v]<<endl;
}
void root(ll v,ll p,vector<ll> adj[],ll dp[],ll m,ll ans[],vector<ll> left[],vector<ll> right[])
{
   ll k=adj[v].size();
    ll cnt=0;
   // cout<<"root of v "<<v<<endl;
    //cout<<"size of list of "<<v<<" "<<k<<endl;
   for(ll i=0;i<k;i++)
   {
      ll s=adj[v].at(i);
      if(s!=p && left[v].size()==0)
      {
         left[v].pb(dp[s]+1);
         cnt++;
      }
      else if(s!=p)
      {
         ll u=(left[v].at(cnt-1)*(dp[s]+1))%m;
         left[v].pb(u);
         cnt++;
      }
   }
   cnt=0;
  // cout<<"done "<<endl;
   for(ll i=k-1;i>=0;i--)
   {
      ll s=adj[v].at(i);
      if(s!=p && right[v].size()==0)
      {
         right[v].pb(dp[s]+1);
         cnt++;

      }
      else if(s!=p)
      {
         ll u=(right[v].at(cnt-1)*(dp[s]+1))%m;
         right[v].pb(u);
         cnt++;
      }
   }
 //  ll mmm=right[v].size();
 //  cout<<"list of "<<v<<endl;
   /*for(ll i=0;i<k;i++)
   {
      ll s=adj[v].at(i);
     cout<<"child "<<s<<" right "<<right[v].at(i)<<" left "<<left[v].at(i)<<" dp "<<dp[s]<<endl;
   }*/
 //  cout<<"done 2 "<<endl; //  cout<<"dfs of "<<v<<" with parent "<<p<<" and dp "<<dp[v]<<endl;*/
  cnt=0;
   for(ll i=0;i<k;i++)
   {
      ll s=adj[v].at(i);
     // cout<<s<<" is "<<endl;
      
      if(ans[s]==-1)
      {
     //    cout<<s<<" cnt "<<cnt<<endl;
      //   cout<<s<<endl;
         ll v_res=dp[v];
         ll s_res=dp[s];
        // ll mm=left[v].size();
         if(k==1)
         dp[v]=1;
        else if(cnt==0)
         {
            
            
            dp[v]=right[v].at(k-2);
         }
         else if(cnt==k-1)
         {
            dp[v]=left[v].at(k-2);
         }
         else 
         {
            dp[v]=((left[v].at(cnt-1))*(right[v].at(k-2-cnt)))%m;
         }
         dp[s]=(dp[s]*(dp[v]+1))%m;
         ans[s]=dp[s];
     //    cout<<"for calculation of ans of "<<s<<" dp of parent "<<v<<" is "<<dp[v]<<" and dp of  "<<s<<" "<<dp[s]<<endl;
       //  cout<<"ans of "<<s<<" "<<ans[s]<<endl;
        root(s,-1,adj,dp,m,ans,left,right);
        dp[s]=s_res;
        dp[v]=v_res;
        cnt++;
      }
      else cnt++;
   }
}


int main()
{
  // ini();
  ll n,m;
  cin>>n>>m;
  vector<ll> adj[n+1];
  for(ll i=0;i<n-1;i++)
  {
     ll u,v;
     cin>>u>>v;
     adj[u].pb(v);
     adj[v].pb(u);
  }
  ll dp[n+1]={0};
  ll ans[n+1]={0};
 // cout<<"dfs"<<endl;
 vector<ll> left[n+1];
 vector<ll> right[n+1];
 for(ll i=1;i<=n;i++)
 ans[i]=-1;

ll checkans[n+1];
/*for(ll i=1;i<=n;i++)
{
  ll dpp[n+1]={0};
   dfs(i,-1,adj,dpp,m,left,right);
   checkans[i]=dpp[i];
}*/
  dfs(1,-1,adj,dp,m,left,right);
 // cout<<"end"<<endl;
  ans[1]=dp[1];
 // cout<<"root"<<endl;
 
  root(1,-1,adj,dp,m,ans,left,right);
 // cout<<"done"<<endl;
for(ll i=1;i<=n;i++)
cout<<ans[i]<<endl;
return 0;
}

Measured memory_bytes: 31872