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.15.1) (judge verdict: Accepted)

struct TreeDP<Lift, Merge, Init, Value, Edge> {
    size: usize,
    edge: Vec<(usize, usize, Edge)>,
    lift: Lift,
    merge: Merge,
    init: Init,
    phantom: std::marker::PhantomData<Value>,
}

impl<Lift, Merge, Init, Value, Edge>  TreeDP<Lift, Merge, Init, Value, Edge>
where Lift: Fn(Value, &Edge) -> Value,
      Merge: Fn(Value, Value) -> Value,
      Init: Fn(usize) -> Value,
{
    fn new(size: usize, lift: Lift, merge: Merge, init: Init) -> Self {
        TreeDP {
            size: size,
            edge: vec![],
            lift: lift,
            merge: merge,
            init: init,
            phantom: std::marker::PhantomData,
        }
    }
    fn add_edge(&mut self, a: usize, b: usize, c: Edge) {
        assert!(a < self.size && b < self.size && a != b);
        self.edge.push((a, b, c));
    }
    fn solve(&self, root: usize) -> Value {
        let size = self.size;
        let mut graph = vec![vec![]; size];
        for (i, e) in self.edge.iter().enumerate() {
            graph[e.0].push((e.1, i));
            graph[e.1].push((e.0, i));
        }
        let mut topo = vec![];
        let mut stack = vec![(root, root)];
        while let Some((v, p)) = stack.pop() {
            topo.push(v);
            if let Some(k) = graph[v].iter().position(|e| e.0 == p) {
                graph[v].remove(k);
            }
            for e in graph[v].iter() {
                stack.push((e.0, v));
            }
        }
        let mut dp: Vec<Option<Value>> = (0..size).map(|_| None).collect();
        for &v in topo.iter().rev() {
            let mut now = (self.init)(v);
            for &(u, k) in graph[v].iter() {
                let b = dp[u].take().unwrap();
                now = (self.merge)(now, (self.lift)(b, &self.edge[k].2));
            }
            dp[v] = Some(now);
        }
        dp[root].take().unwrap()
    }
}

use std::io::Read;

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    const MOD: u64 = 1_000_000_007;
    type Value = (u64, u64);
    type Edge = ();
    let lift = |a: Value, _e: &Edge| -> Value {
        a
    };
    let merge = |a: Value, b: Value| -> Value {
        (a.0 * (b.0 + b.1) % MOD, a.1 * b.0 % MOD)
    };
    let init = |_v: usize| -> Value {
        (1, 1)
    };
    let mut solver = TreeDP::new(n, lift, merge, init);
    for _ in 1..n {
        let x: usize = it.next().unwrap().parse::<usize>().unwrap() - 1;
        let y: usize = it.next().unwrap().parse::<usize>().unwrap() - 1;
        solver.add_edge(x, y, ());
    }
    let ans = solver.solve(0);
    println!("{}", (ans.0 + ans.1) % MOD);
}

fn main() {
    run();
}