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: Accepted)

#[allow(unused_imports)]
use std::cmp::Ordering;

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get {
    ([$t: ty]) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

use self::modint::ModInt;
mod modint {
    use std;
    #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
    pub struct ModInt {
        value: usize,
        module: usize,
    }
    impl ModInt {
        pub fn new(value: usize, module: usize) -> Self {
            ModInt {
                value: value % module,
                module: module,
            }
        }
    }
    impl std::ops::Add for ModInt {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            let mut value = self.value + other.value;
            if value > self.module {
                value -= self.module;
            }
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Add<usize> for ModInt {
        type Output = Self;
        fn add(self, other: usize) -> Self {
            let mut value = self.value + other;
            if value > self.module {
                value -= self.module;
            }
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Sub for ModInt {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            let mut value = self.value;
            if value < other.value {
                value += self.module;
            }
            value -= other.value;
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Sub<usize> for ModInt {
        type Output = Self;
        fn sub(self, other: usize) -> Self {
            let mut value = self.value;
            if value < other {
                value += self.module;
            }
            value -= other;
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Mul for ModInt {
        type Output = Self;
        fn mul(self, other: Self) -> Self {
            let value = self.value * other.value % self.module;
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Mul<usize> for ModInt {
        type Output = Self;
        fn mul(self, other: usize) -> Self {
            let value = self.value * other % self.module;
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Div for ModInt {
        type Output = Self;
        fn div(self, other: Self) -> Self {
            let mut exp = self.module - 2;
            let mut value = self.value;
            let mut ovalue = other.value;
            while exp > 0 {
                if exp % 2 == 1 {
                    value = value * ovalue % self.module;
                }
                ovalue = ovalue * ovalue % self.module;
                exp /= 2;
            }
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Div<usize> for ModInt {
        type Output = Self;
        fn div(self, other: usize) -> Self {
            let mut exp = self.module - 2;
            let mut value = self.value;
            let mut ovalue = other;
            while exp > 0 {
                if exp % 2 == 1 {
                    value = value * ovalue % self.module;
                }
                ovalue = ovalue * ovalue % self.module;
                exp /= 2;
            }
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::AddAssign for ModInt {
        fn add_assign(&mut self, other: Self) {
            *self = *self + other;
        }
    }
    impl std::ops::AddAssign<usize> for ModInt {
        fn add_assign(&mut self, other: usize) {
            *self = *self + other;
        }
    }
    impl std::ops::SubAssign for ModInt {
        fn sub_assign(&mut self, other: Self) {
            *self = *self - other;
        }
    }
    impl std::ops::SubAssign<usize> for ModInt {
        fn sub_assign(&mut self, other: usize) {
            *self = *self - other;
        }
    }
    impl std::ops::MulAssign for ModInt {
        fn mul_assign(&mut self, other: Self) {
            *self = *self * other;
        }
    }
    impl std::ops::MulAssign<usize> for ModInt {
        fn mul_assign(&mut self, other: usize) {
            *self = *self * other;
        }
    }
    impl std::ops::DivAssign for ModInt {
        fn div_assign(&mut self, other: Self) {
            *self = *self / other;
        }
    }
    impl std::ops::DivAssign<usize> for ModInt {
        fn div_assign(&mut self, other: usize) {
            *self = *self / other;
        }
    }
    impl std::fmt::Debug for ModInt {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.value)
        }
    }
    impl std::fmt::Display for ModInt {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.value)
        }
    }
}

fn dfs1(
    node: usize,
    parent: usize,
    nodes: &Vec<Vec<usize>>,
    dp: &mut Vec<ModInt>,
    m: usize,
) -> ModInt {
    let mut res = ModInt::new(1, m);
    for &next in &nodes[node] {
        if next == parent {
            continue;
        }
        res *= dfs1(next, node, nodes, dp, m) + 1;
    }
    dp[node] = res;
    res
}

fn dfs2(
    node: usize,
    parent: usize,
    nodes: &Vec<Vec<usize>>,
    dp: &Vec<ModInt>,
    res: &mut Vec<ModInt>,
    pval: ModInt,
    m: usize,
) {
    let mut muls = vec![vec![ModInt::new(1, m); 2]; nodes[node].len() + 1];
    for (i, &next) in nodes[node].iter().enumerate() {
        muls[i + 1][0] = muls[i][0]
            * if next == parent {
                pval + 1
            } else {
                dp[next] + 1
            };
    }
    for (i, &next) in nodes[node].iter().enumerate().rev() {
        muls[i][1] = muls[i + 1][1]
            * if next == parent {
                pval + 1
            } else {
                dp[next] + 1
            };
    }
    let mut r = pval + 1;
    for (i, &next) in nodes[node].iter().enumerate().rev() {
        if next == parent {
            continue;
        }
        r *= dp[next] + 1;
        dfs2(next, node, nodes, dp, res, muls[i][0] * muls[i + 1][1], m);
    }
    res[node] = r;
}

fn solve() {
    let (n, m) = get!(usize, usize);
    let xy = get!(usize, usize; n-1);

    let mut nodes = vec![Vec::new(); n + 1];
    for (x, y) in xy {
        nodes[x].push(y);
        nodes[y].push(x);
    }

    let mut dp = vec![ModInt::new(0, m); n + 1];
    let mut res = vec![ModInt::new(1, m); n + 1];
    dfs1(1, 0, &nodes, &mut dp, m);
    dfs2(1, 0, &nodes, &dp, &mut res, ModInt::new(0, m), m);
    for i in 1..n + 1 {
        println!("{}", res[i]);
    }
}