#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
int MOD=1000000007;
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(int (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=2e5+5;

struct mint {
    lint x; // typedef long long ll;
    mint(lint x=0):x((x%MOD+MOD)%MOD){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(lint t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(MOD-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

struct DP {
    mint dp;
    int sub;
    lint far;
    DP(mint dp=1,int sub=0,lint far=0):dp(dp),sub(sub),far(far){}
    DP operator+(const DP& a) const {
        return DP(dp*a.dp,sub+a.sub,max(far,a.far));
    }
    DP addRoot(int W){
        return DP(dp+1,sub+1,far+W);
    }
};
 
vector<DP> dp[MAX_N];
DP ans[MAX_N];
vector<int> to[MAX_N];
map<pint,int> edleng;

void addedge(int a,int b,int w=1){
    to[a].push_back(b);
    to[b].push_back(a);
    edleng[pint(a,b)]=w;
    edleng[pint(b,a)]=w;
}
 
DP rerootdfs(int v, int p=-1) {
    DP dpSum;
    dp[v] = vector<DP>(to[v].size());
    rep(i,to[v].size()) {
        int u = to[v][i];
        int W=edleng[pint(v,u)];
        if (u == p) continue;
        dp[v][i] = rerootdfs(u,v);
        dpSum = dpSum + dp[v][i];
        }
    return dpSum.addRoot(edleng[pint(v,p)]);
}
void rerootbfs(int v, const DP& dpP=DP(), int p=-1) {
    int deg = to[v].size();
    rep(i,deg) if (to[v][i] == p) dp[v][i] = dpP;
 
    vector<DP> dpLeft(deg+1);
    rep(i,deg) dpLeft[i+1] = dpLeft[i] + dp[v][i];
  
    vector<DP> dpRight(deg+1);
    for (int i = deg-1; i >= 0; --i) dpRight[i] = dpRight[i+1] + dp[v][i];
  
    ans[v] = dpLeft[deg].addRoot(edleng[pint(v,v)]);
 
    rep(i,deg) {
        int u = to[v][i];
        if (u == p) continue;
        DP d = dpLeft[i] + dpRight[i+1];
        rerootbfs(u, d.addRoot(edleng[pint(u,v)]), v);
    }
}

void reroot(){
    rerootdfs(0);
    rerootbfs(0);
}

int main(void){
    int N,M;
    cin >> N >> M;
    MOD=M;
    rep(i,N-1){
        int a,b;
        cin >> a >> b;
        a--,b--;
        addedge(a,b);
    }
    reroot();
    rep(i,N) cout << ans[i].dp-1 << endl;
}

Measured memory_bytes: 69248