#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<unordered_map>
typedef unsigned long long ull;
typedef signed long long sll;
using namespace std;

#define INF 0x7FFFFFFFFFFFFFFF
#define BIG_PRIME 1000000007

vector<vector<int>> edge_idx;
vector<pair<int,int>> edge;
vector<ull> comb_L;
vector<ull> comb_R;
vector<ull> comb;
ull M;

ull calc_comb_to(int point,int prev)
{
  ull pattern=1;
  for(ull i=0;i<edge_idx[point].size();++i)
  {
    if(edge[edge_idx[point][i]].first==point)
    {
      if(edge[edge_idx[point][i]].second!=prev)
      {
        comb_R[edge_idx[point][i]] = calc_comb_to(edge[edge_idx[point][i]].second,point);
        pattern = (pattern*comb_R[edge_idx[point][i]])%M;
      }
    }
    else
    {
      if(edge[edge_idx[point][i]].first!=prev)
      {
        comb_L[edge_idx[point][i]] = calc_comb_to(edge[edge_idx[point][i]].first,point);
        pattern = (pattern*comb_L[edge_idx[point][i]])%M;
      }
    }
  }
  return (pattern+1)%M;
}

void calc_comb(int point,int prev)
{
  ull pattern = 1;
  unordered_map<ull,ull> mp;
  for(ull i=0;i<edge_idx[point].size();++i)
  {
    if(edge[edge_idx[point][i]].first==point)
    {
      if(edge[edge_idx[point][i]].second!=prev)
      {
        auto itr = mp.find(comb_R[edge_idx[point][i]]);
        if(itr==mp.end())
        {
          mp[comb_R[edge_idx[point][i]]]=pattern;
          for(ull j=i+1;j<edge_idx[point].size();++j)
          {
            if(edge[edge_idx[point][j]].first==point)
            {
              mp[comb_R[edge_idx[point][i]]] = (mp[comb_R[edge_idx[point][i]]]*comb_R[edge_idx[point][j]])%M;
            }
            else
            {
              mp[comb_R[edge_idx[point][i]]] = (mp[comb_R[edge_idx[point][i]]]*comb_L[edge_idx[point][j]])%M;
            }
          }
          mp[comb_R[edge_idx[point][i]]] = (mp[comb_R[edge_idx[point][i]]]+1)%M;
        }
        comb_L[edge_idx[point][i]] = mp[comb_R[edge_idx[point][i]]];
        comb[edge[edge_idx[point][i]].second] = (comb_L[edge_idx[point][i]]*(comb_R[edge_idx[point][i]]+M-1))%M;
        calc_comb(edge[edge_idx[point][i]].second,point);
      }
      pattern = (pattern*comb_R[edge_idx[point][i]])%M;
    }
    else
    {
      if(edge[edge_idx[point][i]].first!=prev)
      {
        auto itr = mp.find(comb_L[edge_idx[point][i]]);
        if(itr==mp.end())
        {
          mp[comb_L[edge_idx[point][i]]]=pattern;
          for(ull j=i+1;j<edge_idx[point].size();++j)
          {
            if(edge[edge_idx[point][j]].first==point)
            {
              mp[comb_L[edge_idx[point][i]]] = (mp[comb_L[edge_idx[point][i]]]*comb_R[edge_idx[point][j]])%M;
            }
            else
            {
              mp[comb_L[edge_idx[point][i]]] = (mp[comb_L[edge_idx[point][i]]]*comb_L[edge_idx[point][j]])%M;
            }
          }
          mp[comb_L[edge_idx[point][i]]] = (mp[comb_L[edge_idx[point][i]]]+1)%M;
        }
        comb_R[edge_idx[point][i]] = mp[comb_L[edge_idx[point][i]]];
        comb[edge[edge_idx[point][i]].first] = (comb_R[edge_idx[point][i]]*(comb_L[edge_idx[point][i]]+M-1))%M;
        calc_comb(edge[edge_idx[point][i]].first,point);
      }
      pattern = (pattern*comb_L[edge_idx[point][i]])%M;
    }
  }
}

int main()
{
  ull i,j,k,N;
  int x,y;
  cin >> N >> M;
  edge_idx.resize(N);
  comb_L.resize(N-1,INF);
  comb_R.resize(N-1,INF);
  comb.resize(N,INF);
  for(i=0;i<N-1;++i)
  {
    cin >> x >> y;
    edge.push_back(make_pair(x-1,y-1));
    edge_idx[x-1].push_back(i);
    edge_idx[y-1].push_back(i);
  }
  
  comb[0] = (calc_comb_to(0,INF)+M-1)%M;
  calc_comb(0,INF);
  
  for(i=0;i<N;++i)
  {
    cout << comb[i] << endl;
  }
  
  return 0;
}


Measured memory_bytes: 25716