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 Haskell (GHC 7.10.3) (judge verdict: Accepted)

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE RankNTypes #-}
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe
import Data.List
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Control.Monad
import Control.Monad.ST
-- import Debug.Trace
-- trace _ = id

black :: Int
black = 0
white :: Int
white = 1

solve :: Int -> [(Int,Int)] -> Int
solve bN xys = theFunc (black,1) `mAdd` theFunc (white,1)
  where

  children = vCreate (bN+1) [] act

  act vec = do
    forM_ xys $ \(x,y) -> do
      VM.modify vec (x:) y
      VM.modify vec (y:) x
    mkRt 1 (-1)
    return vec
      
      where
      mkRt j parent = do
        cs <- VM.read vec j
        let newCs = filter (/= parent) cs
        VM.write vec j newCs
        forM_ newCs $ \c -> mkRt c j

  enc (color, i) = color * (bN+1) + i

  theFunc = func_memo enc (memovec enc (2*(bN+1)) mf [(black,1), (white,1)])

  mf rc (color,i)
    | color == black = mProdM [rc (white, j) | j <- cldrn]
    | color == white = mProdM [mAdd <$> rc (white, j) <*> rc (black, j)
                              | j <- cldrn]
    where cldrn = children V.! i
                     
vCreate :: Int -> a ->
           (forall s. VM.MVector s a -> ST s (VM.MVector s a)) ->
           V.Vector a
vCreate n init act = V.create $ do
  vec <- VM.replicate n init
  act vec
  return vec

mPrime = 10^9 + 7

mAdd :: Int -> Int -> Int
mAdd x y = let w = x + y in if w < mPrime then w else w - mPrime

mMul :: Int -> Int -> Int
mMul x y = (x * y) `mod` mPrime

mProdM :: Monad m => [m Int] -> m Int
mProdM mxs = sub 1 mxs where
  sub !acc [] = return acc
  sub !acc (mx:mxs) = do
    x <- mx
    sub (acc `mMul` x) mxs

memovec :: forall a d. (VU.Unbox a, Show a, Show d) =>
           (d -> Int) -> Int -> 
           (forall m. Monad m => (d -> m a) -> d -> m a) ->
           [d] -> VU.Vector a
memovec encode size mf ds = VU.create $ do
  vec <- VUM.new size
  flags <- VUM.replicate size False
  mapM_ (calc vec flags) ds
  return vec
  where
    calc vec flags d = do
      fv <- VUM.read flags (encode d)
      if fv then VUM.read vec (encode d)
        else do
        val <- mf (calc vec flags) d
        VUM.write vec   (encode d) val
        VUM.write flags (encode d) True
        return val

func_memo :: VU.Unbox a => (d->Int) -> VU.Vector a -> d -> a
func_memo encode vecV d = vecV VU.! (encode d)

readBInt :: B.ByteString -> Int
readBInt = fst . fromJust . B.readInt

tmain :: B.ByteString -> Int
tmain cont =
  let remLines0 = map B.words (B.lines cont)
      [bs_bN]:remLines1 = remLines0
      bN = readBInt bs_bN
      xys = map (\[x1,x2] -> (readBInt x1,readBInt x2)) remLines1
  in solve bN xys

outAnswer :: Int -> IO ()
outAnswer = putStrLn . show

main :: IO ()
main = outAnswer . tmain =<< B.getContents