Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
from functools import lru_cache
MOD = 10 ** 9 + 7

N = int(input())
G = [[] for _ in range(N)]
for _ in  range(N - 1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

@lru_cache(maxsize = None)
def dfs(v,color,p = -1):
    if color == 0:
        ans = 1
        for e in G[v]:
            if e == p:
                continue
            ans *= dfs(e,0,v) + dfs(e,1,v)
            ans %= MOD
        return ans
    else:
        ans = 1
        for e in G[v]:
            if e == p:
                continue
            ans *= dfs(e,0,v)
            ans %= MOD
        return ans

ans = (dfs(0,0) + dfs(0,1))%MOD
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
MOD = 10 ** 9 + 7

N = int(input())
G = [[] for _ in range(N)]
for _ in  range(N - 1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

dp = [[-1] * 2 for _ in range(N)]
def dfs(v,color,p = -1):
    if dp[v][color] != -1:
        return dp[v][color]
    if color == 0:
        ans = 1
        for e in G[v]:
            if e == p:
                continue
            ans *= dfs(e,0,v) + dfs(e,1,v)
            ans %= MOD
        dp[v][color] = ans
        return ans
    else:
        ans = 1
        for e in G[v]:
            if e == p:
                continue
            ans *= dfs(e,0,v)
            ans %= MOD
        dp[v][color] = ans
        return ans

ans = (dfs(0,0) + dfs(0,1))%MOD
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
from functools import lru_cache
MOD = 10 ** 9 + 7

N = int(input())
G = [[] for _ in range(N)]
for _ in  range(N - 1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

@lru_cache(maxsize = None)
def dfs(v,color,p = -1):
    if color == 0:
        ans = 1
        for e in G[v]:
            if e == p:
                continue
            ans *= dfs(e,0,v) + dfs(e,1,v)
            ans %= MOD
        return ans
    else:
        ans = 1
        for e in G[v]:
            if e == p:
                continue
            ans *= dfs(e,0,v)
            ans %= MOD
        return ans

ans = (dfs(0,0) + dfs(0,1))%MOD
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
MOD = 10 ** 9 + 7

N = int(input())
G = [[] for _ in range(N)]
for _ in  range(N - 1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

dp = [[-1] * 2 for _ in range(N)]
def dfs(v,color,p = -1):
    if dp[v][color] != -1:
        return dp[v][color]
    if color == 0:
        ans = 1
        for e in G[v]:
            if e == p:
                continue
            ans *= dfs(e,0,v) + dfs(e,1,v)
            ans %= MOD
        dp[v][color] = ans
        return ans
    else:
        ans = 1
        for e in G[v]:
            if e == p:
                continue
            ans *= dfs(e,0,v)
            ans %= MOD
        dp[v][color] = ans
        return ans

ans = (dfs(0,0) + dfs(0,1))%MOD
print(ans)