/*
___mennat khoday ra azz o va jall ke taatash mojebe ghorbat ast o be shokr andarash mazide nemat.
___har nafasi ke foro miravad momedde hayatast o chon bar miayad mofarrahe zat.
___pas dar har nafasi 2 nemat mojod ast o bar har nematy shokry vajeb.

___az dast o zabane ke barayad ?
___kaz ohdeye shokrash be dar ayad !


___man be to dami gharar natvanam kard
___va ehsane to ra shomar natvanam kard
####################
___gar bar tane man zaban shavad har moyee
___yek shokre to az hezar natvanam kard

*/

#include <bits/stdc++.h>

#define pb push_back
#define SALI main
#define left(x) ((x)*2)
#define right(x) (((x)*2)+1)
#define fuck(x) cout << #x << " : " << x << endl;
#define findLowVec(v,x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define findUpVec(v,x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define findLowArr(a,n,x) (lower_bound(a, a+n, x) - a)
#define findUpArr(a,n,x) (upper_bound(a, a+n, x) - a)
#define int long long

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;

template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p)
{return out << "(" << p.first << ", " << p.second << ")";}

template<class A> ostream& operator <<(ostream& out, const vector<A> &v)
{out << "[";for(int i = 0; i < v.size(); i++) {if(i) out << ", ";out << v[i];}return out << "]";}

template<class A,class B> ostream& operator <<(ostream& out, const set<A,B> &s)
{out << "[";for(auto i = s.begin(); i != s.end(); i++) {out << ", ";out << *i;}return out << "]";}

template<class A> ostream& operator <<(ostream& out, const set<A> &s)
{out << "[";for(auto i = s.begin(); i != s.end(); i++) {out << ", ";out << *i;}return out << "]";}

const int N = 1e5 + 10;
int n,mod;
vector<int> G[N];
int dp[N];
int ans[N];
vector<int> pre[N];
vector<int> suf[N];

void fucksort() {for(int i = 1; i <= n; i++) sort(G[i].begin(),G[i].end());}
void modo(int& x) {if(x < 0)x += mod; x %= mod;}

void dfs(int u,int p)
{
    dp[u] = 1;
    for(auto v : G[u]){
        if(v != p){
            dfs(v,u);
            dp[u] *= dp[v];
            modo(dp[u]);
        }
    }
    dp[u]++;
    modo(dp[u]);
}

void swich(int u, int v) // u root and v child
{
    int w = findLowVec(G[u],v);
    int res = pre[u][w];
    res *= suf[u][w+1];
    modo(res);
    dp[u] = res + 1;
    modo(dp[u]);
    dp[v]--;
    modo(dp[v]);
    dp[v] *= dp[u];
    dp[v]++;
    modo(dp[v]);

}

void pre_suf(int u)
{
    // 2 3 4  dp
    // 1   2 6 24 pre
    // 24 12 4 1  suf

    pre[u].pb(1);
    suf[u].pb(1);
    int n = G[u].size();
    for(int i = 0; i < n; i++){
        int v = G[u][i];
        int vv = G[u][n-i-1];
        pre[u].pb(pre[u][i] * dp[v]);
        modo(pre[u][i+1]);
        suf[u].pb(suf[u][i] * dp[vv]);
        modo(suf[u][i+1]);
    }
    reverse(suf[u].begin(),suf[u].end());

}
void sdf(int u, int p)
{
    pre_suf(u);
    ans[u] = dp[u]-1;
    modo(ans[u]);
    for(auto v : G[u]){
        if(v != p){
            swich(u,v);
            sdf(v,u);
            swich(v,u);
        }
    }
}

int32_t SALI()
{
    //ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    scanf("%d%d",&n,&mod);
    for(int i = 1; i < n; i++){
        int u,v; cin >> u >> v;
        G[u].pb(v); G[v].pb(u);
    }
    fucksort();
    dfs(1,1);
    sdf(1,1);
    for(int i = 1; i <= n; i++){
        printf("%d\n",ans[i]);
    }
}




/**< WRITEN BY ALI ADELKHAH */



Measured memory_bytes: 28752