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)

#![allow(non_snake_case, unused)]
use std::collections::*;

////////// missing functions in 1.15.1 //////////
macro_rules! eprint {
	($($t:tt)*) => {{
		use ::std::io::Write;
		let _ = write!(::std::io::stderr(), $($t)*);
	}};
}
macro_rules! eprintln {
	() => { eprintln!(""); };
	($($t:tt)*) => {{
		use ::std::io::Write;
		let _ = writeln!(::std::io::stderr(), $($t)*);
	}};
}
macro_rules! dbg {
	($v:expr) => {{
		let val = $v;
		eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($v), val);
		val
	}}
}
/////////////////////////////////////////////////
pub trait SetMinMax {
	fn setmin(&mut self, v: Self) -> bool;
	fn setmax(&mut self, v: Self) -> bool;
}
impl<T> SetMinMax for T where T: PartialOrd {
	fn setmin(&mut self, v: T) -> bool {
		*self > v && { *self = v; true }
	}
	fn setmax(&mut self, v: T) -> bool {
		*self < v && { *self = v; true }
	}
}

macro_rules! mat {
	($($e:expr),*) => { Vec::from(vec![$($e),*]) };
	($($e:expr,)*) => { Vec::from(vec![$($e),*]) };
	($e:expr; $d:expr) => { Vec::from(vec![$e; $d]) };
	($e:expr; $d:expr $(; $ds:expr)+) => { Vec::from(vec![mat![$e $(; $ds)*]; $d]) };
}

#[macro_export]
macro_rules! ok {
	($a:ident$([$i:expr])*.$f:ident()$(@$t:ident)*) => {
		$a$([$i])*.$f($($t),*)
	};
	($a:ident$([$i:expr])*.$f:ident($e:expr$(,$es:expr)*)$(@$t:ident)*) => { {
		let t = $e;
		ok!($a$([$i])*.$f($($es),*)$(@$t)*@t)
	} };
}

pub fn readln() -> String {
	let mut line = String::new();
	::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e));
	line
}

macro_rules! read {
	($($t:tt),*; $n:expr) => {{
		let stdin = ::std::io::stdin();
		let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
			let line = line.unwrap();
			let mut it = line.split_whitespace();
			_read!(it; $($t),*)
		}).collect::<Vec<_>>();
		ret
	}};
	($($t:tt),*) => {{
		let line = readln();
		let mut it = line.split_whitespace();
		_read!(it; $($t),*)
	}};
}

macro_rules! _read {
	($it:ident; [char]) => {
		_read!($it; String).chars().collect::<Vec<_>>()
	};
	($it:ident; [u8]) => {
		Vec::from(_read!($it; String).into_bytes())
	};
	($it:ident; usize1) => {
		$it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
	};
	($it:ident; [usize1]) => {
		$it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
	};
	($it:ident; [$t:ty]) => {
		$it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
	};
	($it:ident; $t:ty) => {
		$it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
	};
	($it:ident; $($t:tt),+) => {
		($(_read!($it; $t)),*)
	};
}

pub fn main() {
	let _ = ::std::thread::Builder::new().name("run".to_string()).stack_size(32 * 1024 * 1024).spawn(run).unwrap().join();
}


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

pub trait Int {
	fn as_i32(self) -> i32;
	fn as_i64(self) -> i64;
	fn as_u32(self) -> u32;
	fn as_u64(self) -> u64;
	fn as_usize(self) -> usize;
}

macro_rules! impl_int {
	() => {};
	($t:ty $(, $ts:ty)*) => {
		impl Int for $t {
			fn as_i32(self) -> i32 {
				self as i32
			}
			fn as_i64(self) -> i64 {
				self as i64
			}
			fn as_u32(self) -> u32 {
				self as u32
			}
			fn as_u64(self) -> u64 {
				self as u64
			}
			fn as_usize(self) -> usize {
				self as usize
			}
		}
		impl_int!($($ts),*);
	}
}

impl_int!(i8, i16, i32, i64, u8, u16, u32, u64, isize, usize);

pub trait Mod: Copy + Default + Ord {
	fn m() -> u32;
}

#[derive(Clone, Copy, Default, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct ModP<M>(pub u32, PhantomData<M>);

impl<M> ::std::fmt::Debug for ModP<M> {
	fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
		self.0.fmt(f)
	}
}

impl<M> ::std::fmt::Display for ModP<M> {
	fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
		self.0.fmt(f)
	}
}

impl<M: Mod> ModP<M> {
	/// Assume 0 <= a < m.
	pub fn new<I: Int>(a: I) -> ModP<M> {
		ModP(a.as_u32(), PhantomData)
	}
	pub fn pow<I: Int>(&self, b: I) -> ModP<M> {
		let mut b = b.as_i64();
		if b < 0 {
			b = b % (M::m() - 1) as i64 + M::m() as i64 - 1;
		}
		let mut res = ModP::new(1);
		let mut a = *self;
		while b > 0 {
			if (b & 1) > 0 { res *= a; }
			a *= a;
			b >>= 1;
		}
		res
	}
	pub fn inv(&self) -> ModP<M> {
		self.pow(M::m() as u64 - 2)
	}
}

impl<'a, M: Mod> Add for &'a ModP<M> {
	type Output = ModP<M>;
	fn add(self, a: &ModP<M>) -> ModP<M> {
		let v = self.0 + a.0;
		if v >= M::m() { ModP::new(v - M::m()) } else { ModP::new(v) }
	}
}

impl<'a, M: Mod> Sub for &'a ModP<M> {
	type Output = ModP<M>;
	fn sub(self, a: &ModP<M>) -> ModP<M> {
		if self.0 < a.0 { ModP::new(M::m() - a.0 + self.0) } else { ModP::new(self.0 - a.0 ) }
	}
}

impl<'a, M: Mod> Mul for &'a ModP<M> {
	type Output = ModP<M>;
	fn mul(self, a: &ModP<M>) -> ModP<M> {
		ModP::new(((self.0 as u64 * a.0 as u64) % M::m() as u64) as u32)
	}
}

impl<'a, M: Mod> Div for &'a ModP<M> {
	type Output = ModP<M>;
	fn div(self, a: &ModP<M>) -> ModP<M> {
		self * a.inv()
	}
}

impl<'a, M: Mod> Neg for &'a ModP<M> {
	type Output = ModP<M>;
	fn neg(self) -> ModP<M> {
		ModP::new(if self.0 == 0 { 0 } else { M::m() - self.0 })
	}
}

impl<M: Mod> Neg for ModP<M> {
	type Output = ModP<M>;
	fn neg(self) -> ModP<M> {
		(&self).neg()
	}
}

macro_rules! impl_all {
	($t:ident$(<$($g:ident),*>)*; $Op:ident:$op:ident:$Opa:ident:$opa:ident) => {
		impl<$($($g),*)*> $Op for $t$(<$($g),*>)* where for<'b> &'b $t$(<$($g),*>)*: $Op<Output = $t$(<$($g),*>)*> {
			type Output = $t$(<$($g),*>)*;
			fn $op(self, a: $t$(<$($g),*>)*) -> $t$(<$($g),*>)* { (&self).$op(&a) }
		}
		impl<'a, $($($g),*)*> $Op<&'a $t$(<$($g),*>)*> for $t$(<$($g),*>)* where for<'b> &'b $t$(<$($g),*>)*: $Op<Output = $t$(<$($g),*>)*> {
			type Output = $t$(<$($g),*>)*;
			fn $op(self, a: &$t$(<$($g),*>)*) -> $t$(<$($g),*>)* { (&self).$op(&a) }
		}
		impl<'a, $($($g),*)*> $Op<$t$(<$($g),*>)*> for &'a $t$(<$($g),*>)* where for<'b> &'b $t$(<$($g),*>)*: $Op<Output = $t$(<$($g),*>)*> {
			type Output = $t$(<$($g),*>)*;
			fn $op(self, a: $t$(<$($g),*>)*) -> $t$(<$($g),*>)* { (&self).$op(&a) }
		}
		impl<$($($g),*)*> $Opa for $t$(<$($g),*>)* where for<'b> &'b $t$(<$($g),*>)*: $Op<Output = $t$(<$($g),*>)*> {
			fn $opa(&mut self, a: $t$(<$($g),*>)*) { *self = (&*self).$op(&a) }
		}
	}
}

impl_all!(ModP<M>; Add:add:AddAssign:add_assign);
impl_all!(ModP<M>; Sub:sub:SubAssign:sub_assign);
impl_all!(ModP<M>; Mul:mul:MulAssign:mul_assign);
impl_all!(ModP<M>; Div:div:DivAssign:div_assign);

#[macro_export]
macro_rules! define_mod {
	($name:ident; $e:expr) => {
		#[derive(Clone, Copy, Default, Hash, PartialEq, PartialOrd, Eq, Ord)]
		pub struct _Mod;
		pub type $name = $crate::ModP<_Mod>;
		impl $crate::Mod for _Mod {
			fn m() -> u32 { $e }
		}
	}
}

impl<M: Mod, I: Int> From<I> for ModP<M> {
	/// Assume 0 <= a < m.
	fn from(a: I) -> ModP<M> { ModP::new(a) }
}

fn run() {
	let N = read!(usize);
	let mut g = vec![vec![]; N];
	for (x, y) in read!(usize1, usize1; N - 1) {
		g[x].push(y);
		g[y].push(x);
	}
	let mut order = vec![0];
	let mut parent = vec![!0; N];
	parent[0] = !1;
	for i in 0..N {
		for &v in &g[order[i]] {
			if parent[v] == !0 {
				order.push(v);
				parent[v] = order[i];
			}
		}
	}
	define_mod!(P; 1_000_000_007);
	let mut dp = mat![P::new(1); N; 2];
	for i in order.into_iter().rev() {
		for &j in &g[i] {
			if j != parent[i] {
				dp[i][0] *= dp[j][0] + dp[j][1];
				dp[i][1] *= dp[j][0];
			}
		}
	}
	println!("{}", dp[0][0] + dp[0][1]);
}