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.
Here, it is not allowed to paint two adjacent vertices both in black.

Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

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
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.

Sample Input 1

3
1 2
2 3

Sample Output 1

5

There are five ways to paint the vertices, as follows:

Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

9

There are nine ways to paint the vertices, as follows:

Sample Input 3

1

Sample Output 3

2

Sample Input 4

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

Sample Output 4

157

Solution in Rust (1.42.0) (judge verdict: Accepted)

// ---------- begin ModInt ----------
mod modint {

    #[allow(dead_code)]
    pub struct Mod;
    impl ConstantModulo for Mod {
        const MOD: u32 = 1_000_000_007;
    }

    #[allow(dead_code)]
    pub struct StaticMod;
    static mut STATIC_MOD: u32 = 0;
    impl Modulo for StaticMod {
        fn modulo() -> u32 {
            unsafe { STATIC_MOD }
        }
    }

    #[allow(dead_code)]
    impl StaticMod {
        pub fn set_modulo(p: u32) {
            unsafe {
                STATIC_MOD = p;
            }
        }
    }

    use std::marker::*;
    use std::ops::*;

    pub trait Modulo {
        fn modulo() -> u32;
    }

    pub trait ConstantModulo {
        const MOD: u32;
    }

    impl<T> Modulo for T
    where
        T: ConstantModulo,
    {
        fn modulo() -> u32 {
            T::MOD
        }
    }

    pub struct ModInt<T>(pub u32, PhantomData<T>);

    impl<T> Clone for ModInt<T> {
        fn clone(&self) -> Self {
            ModInt::new_unchecked(self.0)
        }
    }

    impl<T> Copy for ModInt<T> {}

    impl<T: Modulo> Add for ModInt<T> {
        type Output = ModInt<T>;
        fn add(self, rhs: Self) -> Self::Output {
            let mut d = self.0 + rhs.0;
            if d >= T::modulo() {
                d -= T::modulo();
            }
            ModInt::new_unchecked(d)
        }
    }

    impl<T: Modulo> AddAssign for ModInt<T> {
        fn add_assign(&mut self, rhs: Self) {
            *self = *self + rhs;
        }
    }

    impl<T: Modulo> Sub for ModInt<T> {
        type Output = ModInt<T>;
        fn sub(self, rhs: Self) -> Self::Output {
            let mut d = T::modulo() + self.0 - rhs.0;
            if d >= T::modulo() {
                d -= T::modulo();
            }
            ModInt::new_unchecked(d)
        }
    }

    impl<T: Modulo> SubAssign for ModInt<T> {
        fn sub_assign(&mut self, rhs: Self) {
            *self = *self - rhs;
        }
    }

    impl<T: Modulo> Mul for ModInt<T> {
        type Output = ModInt<T>;
        fn mul(self, rhs: Self) -> Self::Output {
            let v = self.0 as u64 * rhs.0 as u64 % T::modulo() as u64;
            ModInt::new_unchecked(v as u32)
        }
    }

    impl<T: Modulo> MulAssign for ModInt<T> {
        fn mul_assign(&mut self, rhs: Self) {
            *self = *self * rhs;
        }
    }

    impl<T: Modulo> Neg for ModInt<T> {
        type Output = ModInt<T>;
        fn neg(self) -> Self::Output {
            if self.0 == 0 {
                Self::zero()
            } else {
                Self::new_unchecked(T::modulo() - self.0)
            }
        }
    }

    impl<T> std::fmt::Display for ModInt<T> {
        fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl<T: Modulo> std::str::FromStr for ModInt<T> {
        type Err = std::num::ParseIntError;
        fn from_str(s: &str) -> Result<Self, Self::Err> {
            let val = s.parse::<u32>()?;
            Ok(ModInt::new(val))
        }
    }

    impl<T: Modulo> From<usize> for ModInt<T> {
        fn from(val: usize) -> ModInt<T> {
            ModInt::new_unchecked((val % T::modulo() as usize) as u32)
        }
    }

    impl<T: Modulo> From<u64> for ModInt<T> {
        fn from(val: u64) -> ModInt<T> {
            ModInt::new_unchecked((val % T::modulo() as u64) as u32)
        }
    }

    impl<T: Modulo> From<i64> for ModInt<T> {
        fn from(val: i64) -> ModInt<T> {
            let m = T::modulo() as i64;
            ModInt::new((val % m + m) as u32)
        }
    }

    #[allow(dead_code)]
    impl<T> ModInt<T> {
        pub fn new_unchecked(d: u32) -> Self {
            ModInt(d, PhantomData)
        }
        pub fn zero() -> Self {
            ModInt::new_unchecked(0)
        }
        pub fn one() -> Self {
            ModInt::new_unchecked(1)
        }
        pub fn is_zero(&self) -> bool {
            self.0 == 0
        }
    }

    #[allow(dead_code)]
    impl<T: Modulo> ModInt<T> {
        pub fn new(d: u32) -> Self {
            ModInt::new_unchecked(d % T::modulo())
        }
        pub fn pow(&self, mut n: u64) -> Self {
            let mut t = Self::one();
            let mut s = *self;
            while n > 0 {
                if n & 1 == 1 {
                    t *= s;
                }
                s *= s;
                n >>= 1;
            }
            t
        }
        pub fn inv(&self) -> Self {
            assert!(self.0 != 0);
            self.pow(T::modulo() as u64 - 2)
        }
    }

    #[allow(dead_code)]
    pub fn mod_pow(r: u64, mut n: u64, m: u64) -> u64 {
        let mut t = 1 % m;
        let mut s = r % m;
        while n > 0 {
            if n & 1 == 1 {
                t = t * s % m;
            }
            s = s * s % m;
            n >>= 1;
        }
        t
    }
}
// ---------- end ModInt ----------
// ---------- begin graph ----------
struct Graph<T> {
    size: usize,
    edge: Vec<(usize, usize, T)>,
    graph: Vec<(usize, T)>,
    start: Vec<usize>,
    length: Vec<usize>,
    ini: T,
}

#[allow(dead_code)]
impl<T: Clone> Graph<T> {
    fn new(size: usize, ini: T) -> Self {
        Graph {
            size: size,
            edge: vec![],
            graph: Vec::with_capacity(size),
            start: Vec::with_capacity(size),
            length: Vec::with_capacity(size),
            ini: ini,
        }
    }
    fn add_edge(&mut self, src: usize, dst: usize, val: T) {
        assert!(src < self.size && dst < self.size);
        self.edge.push((src, dst, val));
    }
    fn build(&mut self) {
        let size = self.size;
        self.length.clear();
        self.length.resize(size, 0);
        for e in self.edge.iter() {
            self.length[e.0] += 1;
        }
        self.start.clear();
        let mut sum = 0;
        for d in self.length.iter() {
            sum += *d;
            self.start.push(sum);
        }
        assert!(self.edge.len() == sum);
        self.graph.clear();
        self.graph.resize(self.edge.len(), (size, self.ini.clone()));
        while let Some((src, dst, val)) = self.edge.pop() {
            self.start[src] -= 1;
            let k = self.start[src];
            self.graph[k] = (dst, val);
        }
        assert!(self
            .start
            .windows(2)
            .zip(self.length.iter())
            .all(|(s, l)| s[1] - s[0] == *l));
        assert!(*self.start.last().unwrap() + *self.length.last().unwrap() == self.graph.len());
        //        }
    }
    // remove と銘打っているが実際に要素は削除されないので注意
    fn remove(&mut self, v: usize, k: usize) {
        assert!(v < self.size && k < self.length[v]);
        let s = self.start[v];
        let len = self.length[v];
        self.graph.swap(s + k, s + len - 1);
        self.length[v] -= 1;
    }
}

impl<T> std::ops::Index<usize> for Graph<T> {
    type Output = [(usize, T)];
    fn index(&self, v: usize) -> &Self::Output {
        let len = self.length[v];
        let s = self.start[v];
        &self.graph[s..(s + len)]
    }
}

impl<T> std::ops::IndexMut<usize> for Graph<T> {
    fn index_mut(&mut self, v: usize) -> &mut Self::Output {
        let len = self.length[v];
        let s = self.start[v];
        &mut self.graph[s..(s + len)]
    }
}
// ---------- end graph ----------

use proconio::*;
use proconio::marker::*;

use modint::*;

type M = ModInt<Mod>;

fn run() {
    input! {
        n: usize,
        e: [(Usize1, Usize1); n - 1],
    }
    let mut g = Graph::new(n, ());
    for (a, b) in e {
        g.add_edge(a, b, ());
        g.add_edge(b, a, ());
    }
    g.build();
    let mut topo = vec![(0, 0)];
    for i in 0..n {
        let (v, p) = topo[i];
        if let Some(k) = g[v].iter().position(|u| u.0 == p) {
            g.remove(v, k);
        }
        for &(u, _) in g[v].iter() {
            topo.push((u, v));
        }
    }
    let mut dp = vec![(M::one(), M::one()); n];
    for &(v, _) in topo.iter().rev() {
        let mut val = (M::one(), M::one());
        for &(u, _) in g[v].iter() {
            let (a, b) = dp[u];
            val = (val.0 * (a + b), val.1 * a);
        }
        dp[v] = val;
    }
    let ans = dp[0].0 + dp[0].1;
    println!("{}", ans);
}

fn main() {
    run();
}