Problem:
Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices.

You are given a positive integer M.
For each v (1 \leq v \leq N), answer the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq x_i, y_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print N lines.
The v-th (1 \leq v \leq N) line should contain the answer to the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Sample Input 1

3 100
1 2
2 3

Sample Output 1

3
4
3

There are seven ways to paint the vertices, as shown in the figure below.
Among them, there are three ways such that Vertex 1 is black, four ways such that Vertex 2 is black and three ways such that Vertex 3 is black.

Sample Input 2

4 100
1 2
1 3
1 4

Sample Output 2

8
5
5
5

Sample Input 3

1 100

Sample Output 3

1

Sample Input 4

10 2
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4

0
0
1
1
1
0
1
0
1
1

Be sure to print the answers modulo M.

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <tuple>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <cctype>
#include <locale>
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(),(a).end()
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
typedef pair<int,int> P;
typedef pair<long long,long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
//s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
//vector<vector<ll>> dp(n,vector<ll>(n))
//exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD) % MOD;
//2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
//3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d)));

int MOD;

struct Rerooting{
    /*  begin here  */
    struct DP{
        long long dp;
        DP(long long dp_):dp(dp_){}
    };
    const DP identity=DP(1);
    function<DP(DP,DP)> merge = [](DP dp_cum,DP d) -> DP{ return DP((dp_cum.dp*d.dp)%MOD); };
    function<DP(DP)> add_root = [](DP d) -> DP{ return DP(d.dp+1); };
    /*  end here   */
    struct Edge{
        int to;
    };
    using Graph=vector<vector<Edge>>;
    vector<vector<DP>> dp; //dp[i][j]:=from i number j path
    vector<DP> ans;
    Graph G;
    Rerooting(int N):G(N){
        dp.resize(N);
        ans.assign(N, identity);
    }
    void add_edge(int a,int b){
        G[a].push_back({b});
    }
    void build(){
        dfs(0);
        bfs(0,identity);
    }
    DP dfs(int v_,int par_=-1){
        DP dp_cum_=identity;
        int deg_=(int)G[v_].size();
        dp[v_]=vector<DP>(deg_,identity);
        for(int i_=0;i_<deg_;i_++){
            int u_=G[v_][i_].to;
            if(u_==par_) continue;
            dp[v_][i_]=dfs(u_,v_);
            dp_cum_=merge(dp_cum_,dp[v_][i_]);
        }
        return add_root(dp_cum_);
    }
    void bfs(int v_,const DP &dp_par,int par_=-1){
        int deg_=(int)G[v_].size();
        for(int i_=0;i_<deg_;i_++){
            if(G[v_][i_].to==par_) dp[v_][i_]=dp_par;
        }
        vector<DP> dp_l(deg_+1,identity),dp_r(deg_+1,identity);
        for(int i_=0;i_<deg_;i_++){
            dp_l[i_+1] = merge(dp_l[i_],dp[v_][i_]);
        }
        for(int i_=deg_-1;i_>=0;i_--){
            dp_r[i_] = merge(dp_r[i_+1],dp[v_][i_]);
        }
        ans[v_]=add_root(dp_l[deg_]);
        for(int i_=0;i_<deg_;i_++){
            int u_=G[v_][i_].to;
            if(u_==par_) continue;
            bfs(u_,add_root(merge(dp_l[i_], dp_r[i_+1])),v_);
        }
    }
};

signed main(){
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    MOD=m;
    Rerooting tree(n);
    rep(i,n-1){
        int x,y; cin >> x >> y;
        x--; y--;
        tree.add_edge(x, y);
        tree.add_edge(y, x);
    }
    tree.build();
    rep(i,n) cout << tree.ans[i].dp-1 << endl;
}