#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <utility>
#include <cmath>
#include <cstdio>
#include <functional>

using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;

template <typename T>
class SegmentTree {
  int N;
  vector<T> dat;
  function< T (T, T)> oper;
  T identity ;
public:
  SegmentTree(int n, function<T(T, T)> op, T id) {
    oper = op;
    identity = id;
    N = 1;
    while(N < n) N *= 2;
    dat.resize(2*N, identity);
   }
  // update k th element
  void update(int k, T a) {
    k += N-1; // leaf
    dat[k] = a;
    while(k > 0) {
      k = (k - 1) / 2;
      dat[k] = oper(dat[k*2+1], dat[k*2+2]);
    }
  }
  // query[a, b)
  T query(int a, int b) { return query(a, b, 0, 0, N); }
  T query(int a, int b, int k, int l, int r) {
    if(r <= a or b <= l) return identity;
    if(a <= l and r <= b) return dat[k];
    int m = (l + r) / 2;
    return oper(query(a, b, k*2+1, l, m), query(a, b, k*2+2, m, r));
  }
};
 

int main() {
    int N, M;
    cin >> N >> M;
    
    vector<vector<int>> G(N);
    for(int i=0; i<N-1; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    queue<int> qu;
    vector<int> order;//(N, 0);
    vector<int> parent(N, -1);
    qu.push(0);
    while(!qu.empty()) {
        int cur = qu.front();
        //cout << cur << endl;
        qu.pop();
        order.push_back(cur);

        for(auto v: G[cur]) {
            if(parent[cur] == v) continue;
            parent[v] = cur;
            qu.push(v);
        }
    }
//    cout << order.size() << endl;
    reverse(order.begin(), order.end());


    vector<LL> dpA(N, 1), dpB(N, 1);
    for(auto v: order) {
        //cout <<"  "<< v << endl;
        dpA[v] ++;
        int to = parent[v];
        if(to == -1) continue;
        dpA[to] *= dpA[v];
        dpA[to] %= M;
    }
    reverse(order.begin(), order.end());
    for(int i=0; i<N; i++) {
        //cout << dpA[i] << endl;
    }

    function<LL(LL, LL)> oper = [&] (LL a, LL b) -> LL {
        return a * b % M;
    };
    LL id = 1;
    vector<int> ans(N, 0);
    for(auto v: order) {
        LL cur = dpA[v] - 1;
        ans[v] = cur * dpB[v] % M;
        vector<LL> matome;
        for(auto u: G[v]) {
            if(parent[v] == u) continue;
            matome.push_back(dpA[u]);
        }
        //cout <<v << " " <<  matome.size() << endl;
        if(matome.empty()) continue;
        matome.push_back(dpB[v]);
        SegmentTree<LL> seg(matome.size(), oper, id);
        for(int i=0; i<matome.size(); i++)
            seg.update(i, matome[i]);
        vector<LL> score;
        for(int i=0; i<matome.size(); i++) {
            LL tmp = seg.query(0,i) * seg.query(i+1,matome.size()) % M;
            //cout << i << " " << tmp << " " << matome[i] << endl;
            score.push_back(tmp);
        }
        int iter = 0;
        for(int i=0; i<G[v].size(); i++) {
            int u = G[v][i];
            if(parent[v] == u) continue;
            dpB[u] = score[iter] + 1;
            iter++;
        }

        

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



    return 0;
}

Measured memory_bytes: 13684