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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <bitset>
#include <tuple>
#include <cmath>
#include <complex>
#include <algorithm>
#include <utility>
#include <regex>
#include <cstdint>
#include <numeric>
#include <functional>
#include <cassert>

using namespace std;
using ll = int64_t;
using ull = uint64_t;

constexpr ll INF = 1000000000;/* 1e+9a */
namespace utils{

    class Repeater{
        int st;
        int ed;
        int d;
    public:
        class rep_iterator{
            int i; int d;
        public:
            rep_iterator(int x, int pd) :i(x), d(pd) {}

            int& operator*(){ return i; }
            bool operator==(const rep_iterator& k) const { return i == k.i; }
            bool operator!=(const rep_iterator& k) const { return i != k.i; }
            rep_iterator& operator++(){ i+=d; return *this; }
        };

        explicit Repeater(int N) :st(0), ed(N), d(1) {}
        Repeater(int b, int e) :st(b), ed(e), d(1) {if(st > ed){ st--; ed--; d = -1; } }

        rep_iterator begin() const{ return rep_iterator(st, d); }
        rep_iterator end() const{ return rep_iterator(ed, d); }

    };

    template<class T, class Compare> using p_queue = priority_queue<T, vector<T>, Compare>;
    template<class T> using min_queue = p_queue<T, greater<T>>;
    template<class T> using max_queue = p_queue<T, less<T>>;

    template<class T> bool min_update(T& X, const T& A){ if(X > A) {X = A; return true;} return false; }
    template<class T> bool max_update(T& X, const T& A){ if(X < A) {X = A; return true;} return false; }
#define ALL(x) begin(x), end(x)
#define rALL(x) rbegin(x), rend(x)
}
using namespace utils;
vector<map<int, ll>> dpf, dpb;

vector<set<int>> edge;
ll N, M;

ll dfs(ll v, ll p = -1){
    ll ans = 1;
    map<int, ll> s;
    for (auto &&u : edge[v]) {
        if(u == p) continue;
        auto t =  (dfs(u, v) + 1);
        s[u] = t;
        ans *= t;
        ans %= M;
    } // end u
    ll f = 1;
    for (auto &&u : s) {
        dpf[v][u.first] = f;
        f *= u.second;
        f %= M;
    } // end u
    ll b = 1;
    for(auto u = s.rbegin(); u != s.rend(); u++){
        dpb[v][u->first] =  b;
        b *= u->second;
        b %= M;
    }
    dpb[v][-1] = ans;

    return ans;
}

void sub_main(istream &is) {
    is >> N >> M;
    if(!is) return;
    edge.clear();
    edge.resize(N);
    for (int i = 0; i < N - 1; ++i) {
        int x, y;
        is >> x >> y;
        x--; y--;
        edge[x].insert(y);
        edge[y].insert(x);
    } // end i
    dpf.clear(); dpb.clear();
    dpf.resize(N); dpb.resize(N);
    vector<ll> ans(N);
    vector<ll> check(N, 1);
    ans[0] = dfs(0);
    
    queue<int> bfq;
    for (auto &&u : edge[0]) { bfq.push(u); } // end u
    while(!bfq.empty()){
        int v = bfq.front(); bfq.pop();
        ll a = 1;
        for (auto &&u : edge[v]) {
            if(dpf[v].count(u) == 0){
                ll b =  (dpf[u][v] * dpb[u][v] * check[u]) + 1;
                b %= M;
                check[v] = b;
                a *= b;
            }
            else{
                a *= dpb[u][-1] + 1;
                bfq.push(u);
            }
            a %= M;
        } // end u
        ans[v] = a;
    }

    for (int j = 0; j < N; ++j) {
        cout << ans[j] << endl;
    } // end j

}

int main(int argc, char *argv[]) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(16) << scientific;
#ifdef DEBUG
    string test_cases = "test_V.txt";
    cerr << "DEBUG MODE" <<  endl;
    cerr << test_cases << " -->" << endl;
    auto fs = fstream(test_cases, fstream::in);
    int lp = 0;
    while(fs) {
        lp++;
        cout << lp <<  "#------\n";
        sub_main(fs);
    }
    cout << "------#" << endl;
    if(lp <= 1) sub_main(cin);
#else
    sub_main(cin);
#endif
    return 0;
}