#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
typedef long long ll;
ll M;
template <typename X>
struct Edge{
  int from;
  int to;
  X cost;
  int idx;
  
  Edge() = default;

  Edge(int from, int to, X cost, int idx) : from(from), to(to), cost(cost), idx(idx) {}
};

template <typename X>
struct Node{
  int idx;
  int par;
  X depth;
  vector<Edge<X>> edge;
  map<int,int> to_index;
  vector<X> dp;
  vector<X> lsum, rsum;
  int cnt;
  
  Node() = default;

  explicit Node(int idx) : idx(idx) {
    cnt = 0;
  }
  
  void Make_to_index() {
    rep(i,edge.size()) {
      to_index[edge[i].to] = edge[i].idx;
    }
    to_index[idx] = -1;
    dp.resize(edge.size(), -1);
  }

  void Make_Sum(X id) {
    int n = dp.size();
    lsum.resize(n+1, id);
    rsum.resize(n+1, id);
    rep(i,n) {
      lsum[i+1] = (lsum[i] * (dp[i] + 1)) % M;
    }
    for (int i = n; i > 0; --i) {
      rsum[i-1] = (rsum[i] * (dp[i-1] + 1)) % M;
    }
  }

  void Show() {
    rep(i,dp.size()) cout << dp[i] << " ";
    cout  << "\n";
  }
  
};

template <typename X>
class Tree{
private:
  int n; // number of node
  vector<Node<X>> node;

public:
  Tree() = default;

  Tree(int n) : n(n) {
    rep(i,n) node.emplace_back(i);
  }

  Tree(int n, vector<int> a, vector<int> b) : n(n) {
    rep(i,n) node.emplace_back(i);
    rep(i,n-1) {
      add_edge(a[i], b[i]);
      add_edge(b[i], a[i]);  // indirected edge
    }
  }

  Tree(int n, vector<int> a, vector<int> b, vector<X> c) : n(n) {
    rep(i,n) node.emplace_back(i);
    rep(i,n-1) {
      add_edge(a[i], b[i], c[i]);
      add_edge(b[i], a[i], c[i]);  // indirected edge
    }
  }  

  void add_edge(int from, int to, X cost = 1) {
    node[from].edge.emplace_back(from, to, cost, node[from].edge.size());
  }

  void DFS_Init(int v, int p, int d) {
    node[v].par = p;
    node[v].depth = d;
    for(auto next: node[v].edge) {
      int w = next.to;
      X cost = next.cost;
      if(w == p) continue;
      DFS_Init(w, v, d + cost);
    }
  }

  void Init_Node(int root) {
    DFS_Init(root, -1, 0);
    rep(i,n) node[i].Make_to_index();
  }
  
  X ReRoot(int p, int v) {
    int index = node[p].to_index[v];
    if(index != -1) if(node[p].dp[index] >= 0) return node[p].dp[index];
    if(node[v].cnt == node[v].edge.size()) {
      if(node[v].cnt == 0) return 1;
      int index2 = node[v].to_index[p];
      if(index2 == -1) {
	return node[v].rsum[0];
      }
      else {
	node[p].dp[index] = (node[v].lsum[index2] * node[v].rsum[index2+1]) % M;
	node[p].cnt++;
	if(node[p].cnt == node[p].edge.size()) {
	  node[p].Make_Sum(1);
	}
	return node[p].dp[index];
      }
    }
    X res = 1;
    for(auto next: node[v].edge) {
      int w = next.to;
      if(w == p) continue;
      res = (res * (ReRoot(v, w) + 1) ) % M;
    }
    
    if(index == -1) return res;
    node[p].dp[index] = res;
    node[p].cnt++;
    if(node[p].cnt == node[p].edge.size()) {
      node[p].Make_Sum(1);
    }
    return res;
  }
  
};

int main()
{
  int n;cin >> n;
  cin >> M;
  vector<int> a(n-1), b(n-1);
  vector<int> inout(n, 0);
  rep(i,n-1) {
    cin >> a[i] >> b[i];
    a[i]--; b[i]--;
    inout[a[i]]++;
    inout[b[i]]++;
  }

  Tree<ll> tr(n, a, b);
  tr.Init_Node(0);
  rep(i,n) cout << tr.ReRoot(i, i) << "\n";
  // vector<ll> res(n);
  // vector<pair<int,int>> p(n);
  // rep(i,n) p[i] = {-inout[i], i};
  // sort(p.begin(), p.end());
  // rep(i,n) {
  //   int index = p[i].second;
  //   res[index] = tr.ReRoot(index, index);
  // }
  // rep(i,n) cout << res[i] << "\n";

  
  return 0;
}


Measured memory_bytes: 57308