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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

/**
 * @author derrick20
 */
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String args[]) throws Exception {
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        N = sc.nextInt();
        mod = sc.nextInt();
        adjList = new ArrayList[N];
        for (int i = 0; i < N; i++) {
            adjList[i] = new ArrayList<>();
        }

        for (int i = 0; i < N - 1; i++) {
            int u = sc.nextInt() - 1;
            int v = sc.nextInt() - 1;
            adjList[u].add(v);
            adjList[v].add(u);
        }

        dpDown = new long[N];
        dpUp = new long[N];
        solveDown(0, 0);

        dpUp[0] = 1;
        solveUp(0, 0);

//        System.out.println(Arrays.toString(dpUp));
//        System.out.println(Arrays.toString(dpDown));
        for (int i = 0; i < N; i++) {
            long ans = dpUp[i] * dpDown[i];
            ans %= mod;
            out.println(ans);
        }

        out.close();
    }

    static int N, mod;
    static ArrayList<Integer>[] adjList;
    static long[] dpUp;
    static long[] dpDown;
    // dpDown[node] stores the number of ways for a node's subtree to be colored
    // in a connected way, with node ALWAYS being colored
    // Transition: use all our children + 1, since each one can also be uncolored

    static long solveDown(int node, int par) {
        long ways = 1;
        for (int adj : adjList[node]) {
            if (adj != par) {
                ways *= (solveDown(adj, node) + 1);
                ways %= mod;
            }
        }
        return dpDown[node] = ways;
    }

    static void solveUp(int node, int par) {
        int size = adjList[node].size();
        long[] prefixDown = new long[size + 1];
        // store the product of everything up to certain points
        // WE CAN'T DO DIVISION, so we snake around the problem by using groups/clustering
        prefixDown[0] = 1;
        for (int i = 1; i <= size; i++) {
            prefixDown[i] = prefixDown[i - 1];
            int adj = adjList[node].get(i - 1);
            if (adj != par) {
                prefixDown[i] *= (dpDown[adj] + 1);
                prefixDown[i] %= mod;
            }
        }
        long[] suffixDown = new long[size + 1];
        // store the product of the last i nodes
        suffixDown[0] = 1;
        for (int i = 1; i <= size; i++) {
            suffixDown[i] = suffixDown[i - 1];
            int adj = adjList[node].get((size - 1) - (i - 1));
            if (adj != par) {
                suffixDown[i] *= (dpDown[adj] + 1);
                suffixDown[i] %= mod;
            }
        }

        // Now, we have an O(1) way to get the product of everything BUT the current node
        for (int i = 1; i <= size; i++) {
            int adj = adjList[node].get(i - 1);
            if (adj != par) {
                // prefix + suffix (exclude me)
                // Before, i used dpDown[adj] / (dpDown[adj] + 1).
                // However, I knew the modulo being nonprime also made it impossible to do mod inverse, so
                // we need to tackle it head on. Logically, we need to reuse this info, so we should
                // think to precomp it! We don't need a global prefix sum array since we only use the dpDown
                // info during this specific node's adjacents
                long above = dpUp[node];
                long below = (prefixDown[i - 1] * suffixDown[size - i]) % mod;
                dpUp[adj] = above * below + 1;
                dpUp[adj] %= mod;

                solveUp(adj, node);
            }
        }
    }

    static class FastScanner {
        public int BS = 1<<16;
        public char NC = (char)0;
        byte[] buf = new byte[BS];
        int bId = 0, size = 0;
        char c = NC;
        double cnt = 1;
        BufferedInputStream in;

        public FastScanner() {
            in = new BufferedInputStream(System.in, BS);
        }

        public FastScanner(String s) {
            try {
                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
            }
            catch (Exception e) {
                in = new BufferedInputStream(System.in, BS);
            }
        }

        private char getChar(){
            while(bId==size) {
                try {
                    size = in.read(buf);
                }catch(Exception e) {
                    return NC;
                }
                if(size==-1)return NC;
                bId=0;
            }
            return (char)buf[bId++];
        }

        public int nextInt() {
            return (int)nextLong();
        }

        public int[] nextInts(int N) {
            int[] res = new int[N];
            for (int i = 0; i < N; i++) {
                res[i] = (int) nextLong();
            }
            return res;
        }

        public long[] nextLongs(int N) {
            long[] res = new long[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextLong();
            }
            return res;
        }

        public long nextLong() {
            cnt=1;
            boolean neg = false;
            if(c==NC)c=getChar();
            for(;(c<'0' || c>'9'); c = getChar()) {
                if(c=='-')neg=true;
            }
            long res = 0;
            for(; c>='0' && c <='9'; c=getChar()) {
                res = (res<<3)+(res<<1)+c-'0';
                cnt*=10;
            }
            return neg?-res:res;
        }

        public double nextDouble() {
            double cur = nextLong();
            return c!='.' ? cur:cur+nextLong()/cnt;
        }

        public String next() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c>32) {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public String nextLine() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c!='\n') {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public boolean hasNext() {
            if(c>32)return true;
            while(true) {
                c=getChar();
                if(c==NC)return false;
                else if(c>32)return true;
            }
        }
    }
}