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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::collections::HashMap;

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn make_tree(
    cur_idx: usize,
    parent_idx: usize,
    edges: &Vec<Vec<usize>>,
    tree: &mut Vec<Vec<usize>>,
    parent_dict: &mut HashMap<usize, usize>,
) {
    for child_idx in edges[cur_idx].iter() {
        if *child_idx == parent_idx {
            continue;
        }
        tree[cur_idx].push(*child_idx);
        parent_dict.insert(*child_idx, cur_idx);
        make_tree(*child_idx, cur_idx, edges, tree, parent_dict);
    }
}

fn topological_dfs(cur_idx: usize, tree: &Vec<Vec<usize>>, result: &mut Vec<usize>) {
    for child_idx in tree[cur_idx].iter() {
        result.push(*child_idx);
        topological_dfs(*child_idx, tree, result);
    }
}

fn main() {
    let v = read_vec::<u64>();
    let (n, m) = (v[0], v[1]);
    let n = n as usize;
    if n == 1 {
        println!("1");
        return;
    }
    let mut edges: Vec<Vec<usize>> = vec![Vec::new(); 2 * (n - 1)];
    for _ in 0..n - 1 {
        let v = read_vec::<usize>();
        let (a, b) = (v[0] - 1, v[1] - 1);
        edges[a].push(b);
        edges[b].push(a);
    }

    let mut tree: Vec<Vec<usize>> = vec![Vec::new(); n];
    let mut parent_dict: HashMap<usize, usize> = HashMap::new();
    make_tree(0, n, &edges, &mut tree, &mut parent_dict);
    let mut topological_sorted_indexes = vec![0];
    topological_dfs(0, &tree, &mut topological_sorted_indexes);

    let mut dp = vec![1u64; n];
    let mut dp_l = vec![Vec::new(); n];
    let mut dp_r = vec![Vec::new(); n];

    for &ti in topological_sorted_indexes.iter().rev() {
        if !tree[ti].is_empty() {
            for &idx in tree[ti].iter() {
                dp[ti] *= dp[idx] + 1;
                dp[ti] %= m;
            }

            dp_l[ti] = vec![1u64; tree[ti].len()];
            dp_l[ti][0] = dp[tree[ti][0]] + 1;
            for ci in 1..tree[ti].len() {
                let idx = tree[ti][ci];
                dp_l[ti][ci] = dp_l[ti][ci - 1] * (dp[idx] + 1);
                dp_l[ti][ci] %= m;
            }

            dp_r[ti] = vec![1u64; tree[ti].len()];
            dp_r[ti][0] = dp[tree[ti][tree[ti].len() - 1]] + 1;
            for ci in 1..tree[ti].len() {
                let idx = tree[ti][tree[ti].len() - 1 - ci];
                dp_r[ti][ci] = dp_l[ti][ci - 1] * (dp[idx] + 1);
                dp_r[ti][ci] %= m;
            }
        }
    }

    let mut dp_i = vec![1; n];
    dp_i[0] = 0;
    for &ti in topological_sorted_indexes.iter() {
        for (ci, &cidx) in tree[ti].iter().enumerate() {
            dp_i[cidx] = dp_i[ti] + 1;
            if ci > 0 {
                dp_i[cidx] *= dp_l[ti][ci - 1];
                dp_i[cidx] %= m;
            }
            if ci < tree[ti].len() - 1 {
                dp_i[cidx] *= dp_r[ti][tree[ti].len() - 2 - ci];
                dp_i[cidx] %= m;
            }
        }
    }

    // println!("{:?}", dp);
    // println!("{:?}", dp_i);
    for i in 0..n {
        println!("{:?}", dp[i] * (dp_i[i] + 1) % m);
    }
}