Problem p02728 (python)

Fixed submission:
import sys
sys.setrecursionlimit(200005)
n = int(input())
MOD = 10**9+7
class mint:
    def __init__(self, x):
        self.x = x % MOD if isinstance(x, int) else int(x) % MOD
    def __str__(self):
        return str(self.x)
    __repr__ = __str__
    def __iadd__(self, other):
        self.x += other.x if isinstance(other, mint) else other
        self.x -= MOD if self.x >= MOD else 0
        return self
    def __isub__(self, other):
        self.x += MOD-other.x if isinstance(other, mint) else MOD-other
        self.x -= MOD if self.x >= MOD else 0
        return self
    def __imul__(self, other):
        self.x *= other.x if isinstance(other, mint) else other
        self.x %= MOD
        return self
    def __imod__(self, other):
        self.x %= other.x if isinstance(other, mint) else other
        self.x %= MOD
        return self
    def __add__(self, other):
        return (
            mint(self.x + other.x) if isinstance(other, mint) else
            mint(self.x + other)
        )
    def __sub__(self, other):
        return (
            mint(self.x - other.x) if isinstance(other, mint) else
            mint(self.x - other)
        )
    def __mul__(self, other):
        return (
            mint(self.x * other.x) if isinstance(other, mint) else
            mint(self.x * other)
        )
    def __mod__(self, other):
        return (
            mint(self.x % other.x) if isinstance(other, mint) else
            mint(self.x % other)
        )
    def __truediv__(self, other):
        return (
            mint(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, mint) else
            mint(self.x * pow(other, MOD - 2, MOD))
        )
    def __pow__(self, other):
        return (
            mint(pow(self.x, other.x, MOD)) if isinstance(other, mint) else
            mint(pow(self.x, other, MOD))
        )
    __radd__ = __add__
    def __rsub__(self, other):
        return (
            mint(other.x - self.x) if isinstance(other, mint) else
            mint(other - self.x)
        )
    __rmul__ = __mul__
    def __rmod__(self, other):
        return (
            mint(other.x % self.x) if isinstance(other, mint) else
            mint(other % self.x)
        )
    def __rtruediv__(self, other):
        return (
            mint(
                other.x * pow(self.x, MOD - 2, MOD)
            ) if isinstance(other, mint) else
            mint(other * pow(self.x, MOD - 2, MOD))
        )
    def __rpow__(self, other):
        return (
            mint(pow(other.x, self.x, MOD)) if isinstance(other, mint) else
            mint(pow(other, self.x, MOD))
        )

class comb():
    # class <mint> should be imported
    def __init__(self, n):
        self.n = n
        fact = [0]*(n+1) # 1-indexed
        ifact = [0]*(n+1) # 逆元
        fact[0] = mint(1)
        for i in range(1, n+1):
            fact[i] = fact[i-1]*i
        ifact[-1] = pow(fact[-1], MOD-2)
        for i in range(n, 0, -1):
            ifact[i-1] = ifact[i]*i
        self.fact = fact
        self.ifact = ifact
    def calc(self, n, k):
        if n == 0:
            return 1
        if k<0 or k>n:
            return 0
        return self.fact[n]*self.ifact[k]*self.ifact[n-k]
cb = comb(200005)

class datum:
    def __init__(self, size=0, case=None):
        self.size = size
        if case is None:
            self.case = mint(1)
        else:    
            self.case = case

def merge(now, plus):
    ressize = now.size + plus.size
    rescase = now.case*plus.case
    rescase *= cb.ifact[plus.size]
    return datum(ressize,rescase)

def add_root(now):
    rescase = now.case * cb.fact[now.size]
    ressize = now.size + 1
    return datum(ressize,rescase)

def dfs(u,p=-1):
    now = datum()
    for v in to[u]:
        if v == p:
            continue
        dp[u][v] = dfs(v,u)
        now = merge(now,dp[u][v])
    now = add_root(now)
    return now

ans = [mint(0) for _ in range(n)]
def bfs(u,p=-1,pdp=None):
    for v in to[u]:
        if v == p:
            dp[u][v] = pdp
    deg = len(to[u])
    l = [datum() for _ in range(deg+1)]
    r = [datum() for _ in range(deg+1)]
    for i in range(deg):
        l[i+1] = merge(l[i], dp[u][to[u][i]])
    for i in reversed(range(1,deg+1)):
        r[i-1] = merge(r[i], dp[u][to[u][i-1]])

    ans[u] = add_root(l[-1]).case

    for i in range(deg):
        v = to[u][i]
        if v == p:
            continue
        argdp = datum()
        argdp.size += l[i].size + r[i+1].size
        argdp.case *= l[i].case * r[i+1].case
        bfs(v,u,add_root(argdp))







to = [list() for _ in range(n)]
dp = [dict() for _ in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    a -=  1
    b -= 1
    to[a].append(b)
    to[b].append(a)

dfs(0)
bfs(0)

print(*ans)

Buggy submission (verdict: Runtime Error):
from copy import deepcopy as copy
n = int(input())
MOD = 10**9+7
class mint:
    def __init__(self, x):
        self.x = x % MOD if isinstance(x, int) else int(x) % MOD
    def __str__(self):
        return str(self.x)
    __repr__ = __str__
    def __iadd__(self, other):
        self.x += other.x if isinstance(other, mint) else other
        self.x -= MOD if self.x >= MOD else 0
        return self
    def __isub__(self, other):
        self.x += MOD-other.x if isinstance(other, mint) else MOD-other
        self.x -= MOD if self.x >= MOD else 0
        return self
    def __imul__(self, other):
        self.x *= other.x if isinstance(other, mint) else other
        self.x %= MOD
        return self
    def __imod__(self, other):
        self.x %= other.x if isinstance(other, mint) else other
        self.x %= MOD
        return self
    def __add__(self, other):
        return (
            mint(self.x + other.x) if isinstance(other, mint) else
            mint(self.x + other)
        )
    def __sub__(self, other):
        return (
            mint(self.x - other.x) if isinstance(other, mint) else
            mint(self.x - other)
        )
    def __mul__(self, other):
        return (
            mint(self.x * other.x) if isinstance(other, mint) else
            mint(self.x * other)
        )
    def __mod__(self, other):
        return (
            mint(self.x % other.x) if isinstance(other, mint) else
            mint(self.x % other)
        )
    def __truediv__(self, other):
        return (
            mint(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, mint) else
            mint(self.x * pow(other, MOD - 2, MOD))
        )
    def __pow__(self, other):
        return (
            mint(pow(self.x, other.x, MOD)) if isinstance(other, mint) else
            mint(pow(self.x, other, MOD))
        )
    __radd__ = __add__
    def __rsub__(self, other):
        return (
            mint(other.x - self.x) if isinstance(other, mint) else
            mint(other - self.x)
        )
    __rmul__ = __mul__
    def __rmod__(self, other):
        return (
            mint(other.x % self.x) if isinstance(other, mint) else
            mint(other % self.x)
        )
    def __rtruediv__(self, other):
        return (
            mint(
                other.x * pow(self.x, MOD - 2, MOD)
            ) if isinstance(other, mint) else
            mint(other * pow(self.x, MOD - 2, MOD))
        )
    def __rpow__(self, other):
        return (
            mint(pow(other.x, self.x, MOD)) if isinstance(other, mint) else
            mint(pow(other, self.x, MOD))
        )

class comb():
    # class <mint> should be imported
    def __init__(self, n):
        self.n = n
        fact = [0]*(n+1) # 1-indexed
        ifact = [0]*(n+1) # 逆元
        fact[0] = mint(1)
        for i in range(1, n+1):
            fact[i] = fact[i-1]*i
        ifact[-1] = pow(fact[-1], MOD-2)
        for i in range(n, 0, -1):
            ifact[i-1] = ifact[i]*i
        self.fact = fact
        self.ifact = ifact
    def calc(self, n, k):
        if n == 0:
            return 1
        if k<0 or k>n:
            return 0
        return self.fact[n]*self.ifact[k]*self.ifact[n-k]
cb = comb(200005)

class datum:
    def __init__(self, size=0, case=None):
        self.size = size
        if case is None:
            self.case = mint(1)
        else:    
            self.case = case

def merge(d, plus):
    now = copy(d)
    now.size += plus.size
    now.case *= plus.case
    now.case *= cb.ifact[plus.size]
    return now

def add_root(d):
    now = copy(d)
    now.case *= cb.fact[now.size]
    now.size += 1
    return now

def dfs(u,p=-1):
    now = datum()
    for v in to[u]:
        if v == p:
            continue
        dp[u][v] = dfs(v,u)
        now = merge(now,dp[u][v])
    now = add_root(now)
    return now

ans = [mint(0) for _ in range(n)]
def bfs(u,p=-1,pdp=None):
    for v in to[u]:
        if v == p:
            dp[u][v] = pdp
    deg = len(to[u])
    l = [datum() for _ in range(deg+1)]
    r = [datum() for _ in range(deg+1)]
    for i in range(deg):
        l[i+1] = merge(l[i], dp[u][to[u][i]])
    for i in reversed(range(1,deg+1)):
        r[i-1] = merge(r[i], dp[u][to[u][i-1]])

    ans[u] = add_root(l[-1]).case

    for i in range(deg):
        v = to[u][i]
        if v == p:
            continue
        argdp = datum()
        argdp.size += l[i].size + r[i+1].size
        argdp.case *= l[i].case * r[i+1].case
        bfs(v,u,add_root(argdp))







to = [list() for _ in range(n)]
dp = [dict() for _ in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    a -=  1
    b -= 1
    to[a].append(b)
    to[b].append(a)

dfs(0)
bfs(0)

print(*ans)

Buggy submission (verdict: Runtime Error):
from copy import deepcopy as copy
n = int(input())
MOD = 10**9+7
class mint:
    def __init__(self, x):
        self.x = x % MOD if isinstance(x, int) else int(x) % MOD
    def __str__(self):
        return str(self.x)
    __repr__ = __str__
    def __iadd__(self, other):
        self.x += other.x if isinstance(other, mint) else other
        self.x -= MOD if self.x >= MOD else 0
        return self
    def __isub__(self, other):
        self.x += MOD-other.x if isinstance(other, mint) else MOD-other
        self.x -= MOD if self.x >= MOD else 0
        return self
    def __imul__(self, other):
        self.x *= other.x if isinstance(other, mint) else other
        self.x %= MOD
        return self
    def __imod__(self, other):
        self.x %= other.x if isinstance(other, mint) else other
        self.x %= MOD
        return self
    def __add__(self, other):
        return (
            mint(self.x + other.x) if isinstance(other, mint) else
            mint(self.x + other)
        )
    def __sub__(self, other):
        return (
            mint(self.x - other.x) if isinstance(other, mint) else
            mint(self.x - other)
        )
    def __mul__(self, other):
        return (
            mint(self.x * other.x) if isinstance(other, mint) else
            mint(self.x * other)
        )
    def __mod__(self, other):
        return (
            mint(self.x % other.x) if isinstance(other, mint) else
            mint(self.x % other)
        )
    def __truediv__(self, other):
        return (
            mint(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, mint) else
            mint(self.x * pow(other, MOD - 2, MOD))
        )
    def __pow__(self, other):
        return (
            mint(pow(self.x, other.x, MOD)) if isinstance(other, mint) else
            mint(pow(self.x, other, MOD))
        )
    __radd__ = __add__
    def __rsub__(self, other):
        return (
            mint(other.x - self.x) if isinstance(other, mint) else
            mint(other - self.x)
        )
    __rmul__ = __mul__
    def __rmod__(self, other):
        return (
            mint(other.x % self.x) if isinstance(other, mint) else
            mint(other % self.x)
        )
    def __rtruediv__(self, other):
        return (
            mint(
                other.x * pow(self.x, MOD - 2, MOD)
            ) if isinstance(other, mint) else
            mint(other * pow(self.x, MOD - 2, MOD))
        )
    def __rpow__(self, other):
        return (
            mint(pow(other.x, self.x, MOD)) if isinstance(other, mint) else
            mint(pow(other, self.x, MOD))
        )

class comb():
    # class <mint> should be imported
    def __init__(self, n):
        self.n = n
        fact = [0]*(n+1) # 1-indexed
        ifact = [0]*(n+1) # 逆元
        fact[0] = mint(1)
        for i in range(1, n+1):
            fact[i] = fact[i-1]*i
        ifact[-1] = pow(fact[-1], MOD-2)
        for i in range(n, 0, -1):
            ifact[i-1] = ifact[i]*i
        self.fact = fact
        self.ifact = ifact
    def calc(self, n, k):
        if n == 0:
            return 1
        if k<0 or k>n:
            return 0
        return self.fact[n]*self.ifact[k]*self.ifact[n-k]
cb = comb(200005)

class datum:
    def __init__(self, size=0, case=mint(1)):
        self.size = size
        self.case = case

def merge(d, p):
    now = copy(d)
    plus = copy(p)
    now.size += plus.size
    now.case *= plus.case
    now.case *= cb.ifact[plus.size]
    return now

def merge2(a,b):
    a2 = copy(a)
    b2 = copy(b)
    ressize = a2.size + b2.size
    rescase = a2.case* b2.case
    return datum(ressize, rescase)
def add_root(d):
    now = copy(d)
    now.case *= cb.fact[now.size]
    now.size += 1
    return now

def dfs(u,p=-1):
    now = datum()
    for v in to[u]:
        if v == p:
            continue
        dp[u][v] = dfs(v,u)
        now = merge(now,dp[u][v])
    now = add_root(now)
    return now

ans = [mint(0) for _ in range(n)]
def bfs(u,p=-1,pdp=datum()):
    for v in to[u]:
        if v == p:
            dp[u][v] = pdp
    deg = len(to[u])
    l = [datum() for _ in range(deg+1)]
    r = [datum() for _ in range(deg+1)]
    for i in range(deg):
        l[i+1] = merge(l[i], dp[u][to[u][i]])
    for i in reversed(range(1,deg+1)):
        r[i-1] = merge(r[i], dp[u][to[u][i-1]])

    ans[u] = add_root(l[-1]).case

    for i in range(deg):
        v = to[u][i]
        if v == p:
            continue
        lm = copy(l[i])
        rm = copy(r[i+1])
        bfs(v,u,add_root(merge2(lm,rm)))







to = [list() for _ in range(n)]
dp = [dict() for _ in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    a -=  1
    b -= 1
    to[a].append(b)
    to[b].append(a)

dfs(0)
bfs(0)

print(*ans)