#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define Ans(f, y, n) if(f) cout << y << endl; else cout << n << endl;
#define int long long
#define INF 1000000000000000000
using namespace std;

using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using PiP = pair<int, Pii>;
using PPi = pair<Pii, int>;
using bools = vector<bool>;

template<typename T>
void readv(vector<T> &a){ REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a){ REP(i, a.size()){cin >> a[i]; a[i]--;} }
template<typename T>
void debug(vector<vector<T>> m){ REP(i, m.size()){ REP(j, m[i].size()){ cout << m[i][j] << ","; } cout << endl;} }

int mod;

struct edge{int to, cost;};

class Graph
{
public:
    int V;
    vector<vector<edge>> G;
    vec par, dp, dp2, ans, id;
    mat l, r;

    Graph(int V): V(V){
        G = vector<vector<edge>>(V, vector<edge>(0));
        par.resize(V);
        dp.resize(V);
        dp2.resize(V);
        ans.resize(V);
        id.resize(V);
        l.resize(V);
        r.resize(V);
    }

    void add_edge(int from, int to, int cost = 0){
        G[from].push_back(edge({to, cost}));
    }

    void add_edge2(int v1, int v2, int cost = 0){
        add_edge(v1, v2, cost);
        add_edge(v2, v1, cost);
    }

    void dfs(int v = 0, int p = -1){
        par[v] = p;
        dp[v] = 1;
        int k = 0;
        l[v].push_back(1);
        for(auto e: G[v]) if(e.to != par[v]){
            dfs(e.to, v);
            dp[v] *= 1 + dp[e.to];
            dp[v] %= mod;

            id[e.to] = k;
            k++;
            l[v].push_back(1 + dp[e.to]);
            r[v].push_back(1 + dp[e.to]);
        }
        r[v].push_back(1);
        REP(i, k){
            l[v][i + 1] *= l[v][i];
            l[v][i + 1] %= mod;
        }
        IREP(i, k){
            r[v][i] *= r[v][i + 1];
            r[v][i] %= mod;
        }
    }

    void dfs2(int v = 0){
        ans[v] = dp[v];
        dp2[v] = 0;
        if(par[v] != -1){

            dp2[v] = 1;

            dp2[v] *= 1 + dp2[par[v]];
            /*
            for(auto e: G[par[v]]){
                if(e.to != par[par[v]] && e.to != v){
                    dp2[v] *= 1 + dp[e.to];
                    dp2[v] %= mod;
                }
            }*/
            dp2[v] *= (l[par[v]][id[v]] * r[par[v]][id[v] + 1]) % mod;
            dp2[v] %= mod; 

            ans[v] *= 1 + dp2[v];
            ans[v] %= mod;
        }
        for(auto e: G[v]) if(e.to != par[v]) dfs2(e.to);
    }

    void calcans(){
        dfs();
        dfs2();
        //REP(i, V) cout << dp[i] << "," << dp2[i] << "," << ans[i] << endl;
        REP(i, V) cout << ans[i] << endl;
    }

};


signed main(){

    int N, M; cin >> N >> M;
    Graph G(N);
    mod = M;
    int x, y;
    REP(i, N - 1){
        cin >> x >> y;
        G.add_edge2(x - 1, y - 1);
    }
    G.calcans();

    return 0;
}


Measured memory_bytes: 28288