#include <iostream>
#include <math.h>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cstdlib>
#include <unordered_map>
#include <numeric>

using namespace std;

#define fs first
#define sc second

typedef long long ll;
typedef pair<int, int> P;

ll mod = 1e9+7;
ll fact[200200];
ll invfact[200200];

inline ll take_mod(ll a){
    return (a % mod + mod) % mod;
}

inline ll add(ll a, ll b){
    return take_mod(a+b);
}

inline ll sub(ll a, ll b){
    return take_mod(a-b);
}


inline ll mul(ll a, ll b){
    return take_mod(a * b);
}

inline ll pow(ll x, ll n){
    ll res = 1LL;
    while(n > 0){
        if(n & 1) res = mul(res, x);
        x = mul(x, x);
        n >>= 1;
    }
    return res;
}

ll mod_inv(ll x){
    return pow(x, mod-2);
}

// nは上限
void make_fact(ll n){
    fact[0] = 1;
    ll res = 1;
    for(int i = 1; i <= n; i++){
        fact[i] = res;
        res = mul(res, i+1);
    }
}

// nは上限
void make_invfact(ll n){
    invfact[0] = 1;
    invfact[n] = mod_inv(fact[n]);
    for(int i = n-1; i >= 1; i--){
        invfact[i] = mul(invfact[i + 1], i + 1);
    }
}

ll perm(ll n, ll k){
    return mul(fact[n], invfact[n-k]);
}

ll comb(ll n, ll k){
    return mul(mul(fact[n], invfact[n-k]), invfact[k]);
}

vector<int> edge[101000];
ll stateNum1[101000];
ll stateNum2[101000];

ll dfs1(int s, int prev){
    ll res = 1;  // 自分が黒に塗られた場合
    for(auto v: edge[s]){
        if(v == prev){
            continue;
        }
        res = mul(res, dfs1(v, s));
    }
    res = add(res, 1);   // 自分が塗られない場合を足す。
    stateNum1[s] = res;
    return res;
}

void dfs2(int s, int prev, ll prevState){
    int n = edge[s].size();

    ll *hist1;
    ll *hist2;
    hist1 = new ll[n+2];   // i番目より前
    hist2 = new ll[n+2];   // i番目より後
    hist1[0] = 1;

    for(int i = 0; i < n-1; i++){
        int v = edge[s][i];
        ll tmp;
        if(v == prev) {
            tmp = mul(hist1[i], prevState);
        }
        else{
            tmp = mul(hist1[i], stateNum1[v]);
        }
        hist1[i+1] = tmp;
    }

    hist2[n-1] = 1;
    for(int i = n-2; i >= 0; i--){
        int v = edge[s][i+1];
        ll tmp;
        if(v == prev) {
            tmp = mul(hist2[i+1], prevState);
        }
        else{
            tmp = mul(hist2[i+1], stateNum1[v]);
        }
        hist2[i] = tmp;
    }
//    for(int i = 0; i < n; i++){
//        cout << s << " " << hist1[i] << " " << hist2[i] << endl;
//    }

    ll res = 1;
    for(auto v: edge[s]){
        if(v == prev){
            res = mul(res, prevState);
        }
        else{
            res = mul(res, stateNum1[v]);
        }
    }
    stateNum2[s] = res;
    for(int i = 0; i < n; i++){
        int v = edge[s][i];
        if(v == prev){
            continue;
        }
        ll newPrevState = add(mul(hist1[i], hist2[i]), 1);
        dfs2(v, s, newPrevState);
    }
    delete[] hist1;
    delete[] hist2;
}


int main(){
    ll N, M;
    cin >> N >> M;
    mod = M;
    for(int i = 0; i < N-1; i++){
        int x, y;
        cin >> x >> y;
        x--; y--;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }

    if(N == 1){
        cout << 1 << endl;
        return 0;
    }
    dfs1(0, 0);
    dfs2(0, 0, 0);
//    for(int i = 0; i < N; i++){
//        cout << stateNum1[i] << endl;
//    }
    for(int i = 0; i < N; i++){
        cout << stateNum2[i] << endl;
    }

    return 0;
}

Measured memory_bytes: 22016