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 C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

#if DEBUG
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            var scanner = new IO.StreamScanner(Console.OpenStandardInput());

            var n = scanner.Integer();
            var map = new int[n, n];
            for (int i = 0; i < n - 1; i++)
            {
                var x1 = scanner.Integer() - 1;
                var x2 = scanner.Integer() - 1;
                map[x1, x2] = 1;
                map[x2, x1] = 1;
            }

            var ans = GetAns(n, map);
            IO.Printer.Out.WriteLine(ans);
            IO.Printer.Out.Flush();
        }

        public static long GetAns(int n, int[,] map)
        {
            var graph = new Dictionary<int, List<int>>();
            for (int i = 0; i < n; i++) graph.Add(i, new List<int>());

            for (int i = 0; i < n - 1; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (map[i, j] == 0) continue;
                    graph[i].Add(j);
                    graph[j].Add(i);
                }
            }

            return Dfs(0, 0, graph, -1);
        }

        static Dictionary<int, long> _memo = new Dictionary<int, long>();

        public static long Dfs(int i, int parentColor, Dictionary<int, List<int>> graph, int parent)
        {
            var mod = 1000000007L;
            var ret = 0L;
            var key = i << 2 | parentColor;
            if (_memo.ContainsKey(key)) return _memo[key];

            // no children
            if (!graph[i].Any(c => c != parent))
            {
                ret = parentColor == 0 ? 2 : 1;
            }
            else
            {

                // white
                var wPattern = 1L;
                foreach (var c in graph[i])
                {
                    if (c == parent) continue;
                    wPattern *= Dfs(c, 0, graph, i);
                    wPattern %= mod;
                }

                // black
                var bPattern = 0L;
                if(parentColor == 0)
                {
                    bPattern = 1L;
                    foreach (var c in graph[i])
                    {
                        if (c == parent) continue;
                        bPattern *= Dfs(c, 1, graph, i);
                        bPattern %= mod;
                    }
                }

                ret = wPattern + bPattern;
                ret %= mod;
            }            

            _memo[key] = ret;
            return ret;
        }
    }

#if DEBUG

    [TestClass]
    public class Test
    {
        [TestMethod]
        public void Test1()
        {
            var n = 3;
            var a = new int[,]
            {
                {0, 1, 0 },
                {1, 0, 1 },
                {0, 1, 0 },
            };
        var exp = 5;
        var ans = Program.GetAns(n, a);
        Assert.AreEqual(exp, ans);
        }
    }

#endif
}

namespace IO
{
    using System.IO;
    using System.Text;
    using System.Globalization;

    public class Printer : StreamWriter
    {
        static Printer()
        {
            Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false };
        }

        public static Printer Out { get; set; }

        public override IFormatProvider FormatProvider
        {
            get { return CultureInfo.InvariantCulture; }
        }

        public Printer(Stream stream)
            : base(stream, new UTF8Encoding(false, true))
        {
        }

        public Printer(Stream stream, Encoding encoding)
            : base(stream, encoding)
        {
        }

        public void Write<T>(string format, T[] source)
        {
            base.Write(format, source.OfType<object>().ToArray());
        }

        public void WriteLine<T>(string format, T[] source)
        {
            base.WriteLine(format, source.OfType<object>().ToArray());
        }
    }

    public class StreamScanner
    {
        public StreamScanner(Stream stream)
        {
            str = stream;
        }

        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof;

        public bool IsEndOfStream
        {
            get { return isEof; }
        }

        private byte read()
        {
            if (isEof) return 0;
            if (ptr < len) return buf[ptr++];
            ptr = 0;
            if ((len = str.Read(buf, 0, 1024)) > 0) return buf[ptr++];
            isEof = true;
            return 0;
        }

        public char Char()
        {
            byte b;
            do b = read(); while ((b < 33 || 126 < b) && !isEof);
            return (char)b;
        }

        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }

        public string ScanLine()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b != '\n'; b = (char)read())
                if (b == 0) break;
                else if (b != '\r') sb.Append(b);
            return sb.ToString();
        }

        public long Long()
        {
            if (isEof) return long.MinValue;
            long ret = 0;
            byte b;
            var ng = false;
            do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b));
            if (b == 0) return long.MinValue;
            if (b == '-')
            {
                ng = true;
                b = read();
            }
            for (; ; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                ret = ret * 10 + b - '0';
            }
        }

        public int Integer()
        {
            return (isEof) ? int.MinValue : (int)Long();
        }

        public double Double()
        {
            var s = Scan();
            return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN;
        }

        static T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        public char[] Char(int n)
        {
            return enumerate(n, Char);
        }

        public string[] Scan(int n)
        {
            return enumerate(n, Scan);
        }

        public double[] Double(int n)
        {
            return enumerate(n, Double);
        }

        public int[] Integer(int n)
        {
            return enumerate(n, Integer);
        }

        public long[] Long(int n)
        {
            return enumerate(n, Long);
        }
    }
}