Problem:


Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.
For each k=1, ..., N, solve the problem below:

Consider writing a number on each vertex in the tree in the following manner:

First, write 1 on Vertex k.

Then, for each of the numbers 2, ..., N in this order, write the number on the vertex chosen as follows:

Choose a vertex that still does not have a number written on it and is adjacent to a vertex with a number already written on it. If there are multiple such vertices, choose one of them at random.

Find the number of ways in which we can write the numbers on the vertices, modulo (10^9+7).

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i,b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

For each k=1, 2, ..., N in this order, print a line containing the answer to the problem.

Sample Input 1

3
1 2
1 3

Sample Output 1

2
1
1

The graph in this input is as follows:

For k=1, there are two ways in which we can write the numbers on the vertices, as follows:

Writing 1, 2, 3 on Vertex 1, 2, 3, respectively

Writing 1, 3, 2 on Vertex 1, 2, 3, respectively

Sample Input 2

2
1 2

Sample Output 2

1
1

The graph in this input is as follows:

Sample Input 3

5
1 2
2 3
3 4
3 5

Sample Output 3

2
8
12
3
3

The graph in this input is as follows:

Sample Input 4

8
1 2
2 3
3 4
3 5
3 6
6 7
6 8

Sample Output 4

40
280
840
120
120
504
72
72

The graph in this input is as follows:



Sample input:
3
1 2
1 3


Sample output:
2
1
1

Submission status: Wrong Answer

Solution code:
def examA():
    S = SI()
    if S[2]==S[3] and S[4]==S[5]:
        print("Yes")
    else:
        print("No")
    return

def examB():
    X = I()
    ans = 0
    ans += (X//500) * 1000
    X %= 500
    ans += (X//5) * 5
    print(ans)
    return

def examC():
    K, N = LI()
    A = LI()
    L = [0]*N
    L[0] = K-A[-1] + A[0]
    for i in range(1,N):
        L[i] = A[i]-A[i-1]
    ans = sum(L) - max(L)
    #print(L)
    print(ans)
    return

def examD():
    def bfs(n, e, fordfs):
        # 点の数、スタートの点、有向グラフ
        W = [-1] * n
        # 各点の状態量、最短距離とか,見たかどうかとか
        W[e] = 0
        que = deque()
        que.append(e)
        while que:
            now = que.popleft()
            nowW = W[now]
            for ne in fordfs[now]:
                if W[ne] == -1:
                    W[ne] = nowW + 1
                    que.append(ne)
        return W
    N, X, Y = LI()
    V = [[]for _ in range(N)]
    for i in range(N-1):
        V[i].append(i+1)
        V[i+1].append(i)
    V[X-1].append(Y-1)
    V[Y-1].append(X-1)
    #print(V)
    ans = [0]*(N-1)
    for i in range(N):
        L = bfs(N,i,V)
        for l in L:
            if l==0:
                continue
            ans[l-1] += 1
    for v in ans:
        print(v//2)
    return

def examE():
    X, Y, A, B, C = LI()
    P = LI()
    Q = LI()
    R = LI()
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)
    cnt = sum(P[:X]) + sum(Q[:Y])
    #print(cnt)
    X -= 1; Y -= 1
    for c in range(C):
        if X>=0 and Y>=0:
            if P[X] <= Q[Y]:
                if P[X] >= R[c]:
                    break
                cnt += (R[c] - P[X])
                X -= 1
            else:
                if Q[Y] >= R[c]:
                    break
                cnt += (R[c] - Q[Y])
                Y -= 1
        elif X>=0:
            if P[X] >= R[c]:
                break
            cnt += (R[c] - P[X])
            X -= 1
        elif Y>=0:
            if Q[Y] >= R[c]:
                break
            cnt += (R[c] - Q[Y])
            Y -= 1
    ans = cnt
    print(ans)
    return

def examF():
    class combination():
        # 素数のmod取るときのみ　速い
        def __init__(self, n, mod):
            self.n = n
            self.fac = [1] * (n + 1)
            self.inv = [1] * (n + 1)
            for j in range(1, n + 1):
                self.fac[j] = self.fac[j - 1] * j % mod

            self.inv[n] = pow(self.fac[n], mod - 2, mod)
            for j in range(n - 1, -1, -1):
                self.inv[j] = self.inv[j + 1] * (j + 1) % mod

        def comb(self, n, r, mod):
            if r > n or n < 0 or r < 0:
                return 0
            return self.fac[n] * self.inv[n - r] * self.inv[r] % mod

    N = I()
    V = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = LI()
        a -= 1; b -= 1
        V[a].append(b)
        V[b].append(a)
    C = combination(N, mod)

    A = [0]*N
    children = [-1] * N
    ans = [0]*N

    # 子の数のカウント
    def dfs(n, s, edges):
        size = 1
        nowchild = []
        cur = 1
        children[s] = 0
        for i in edges[s]:
            if children[i] != -1:
                continue
            ns, nc = dfs(n, i, edges)
            nowchild.append(ns)
            size += ns
            cur *= nc
            cur %= mod
        children[s] = size
        rest = size - 1
        for k in nowchild:
            cur *= C.comb(rest, k, mod)
            rest -= k
            cur %= mod
        A[s] = cur
        return size, cur

    def dfs2(s,p):
        #print(children, A)
        rep = 1
        rest = N - 1
        for to in V[s]:
            rep *= A[to]
            rep %= mod
        for to in V[s]:
            rep *= C.comb(rest, children[to], mod)
            rest -= children[to]
            rep %= mod
        ans[s] = rep

        n = len(V[s])
        L = [0]*n; R = [0]*n
        L2 = [0]*n; R2 = [0]*n

        for i,ne in enumerate(V[s]):
            L[i] = R[i] = A[ne] * C.inv[children[ne]]
            L2[i] = R2[i] = children[ne]
        for i in range(1,n):
            L[i] *= L[i-1]
            L[i] %= mod
            L2[i] += L2[i-1]
        for i in range(1,n-1)[::-1]:
            R[i] *= R[i+1]
            R[i] %= mod
            R2[i] += R2[i+1]
        for i,ne in enumerate(V[s]):
            if ne==p:
                continue
            A[s] = 1
            children[s] = 1
            if i > 0:
                A[s] *= L[i - 1]
                A[s] %= mod
                children[s] += L2[i - 1]
            if i + 1 < n:
                A[s] *= R[i + 1]
                A[s] %= mod
                children[s] += R2[i + 1]
            A[s] *= C.fac[N-children[ne]-1]
            A[s] %= mod
            #print(A[s],s,L,R)
            dfs2(ne, s)
        return

    dfs(N, 0, V)
    return
    dfs2(0,-1)
    for v in ans:
        print(v)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examF()

"""

"""

Submission status: Wrong Answer

Solution code:
class treedp_rerooting():
    def __init__(self, N, V, MOD):
        self.N = N
        self.V = V
        self.mod = MOD
        self.rep = [0] * N
        self.children = [-1] * N
        self.A = [0] * N
        self.C = combination(N, MOD)
        return

    # 子の数のカウント
    def dfs(self, s):
        size = 1
        nowchild = []
        cur = 1
        self.children[s] = 0
        for i in self.V[s]:
            if self.children[i] != -1:
                continue
            ns, nc = self.dfs(i)
            nowchild.append(ns)
            size += ns
            cur *= nc
            cur %= self.mod
        self.children[s] = size
        rest = size - 1
        for k in nowchild:
            cur *= self.C.comb(rest, k)
            rest -= k
            cur %= self.mod
        self.A[s] = cur
        return size, cur

    def dfs2(self, s, p=-1):
        cur = 1
        rest = self.N - 1
        for to in self.V[s]:
            cur *= self.A[to]
            cur %= mod
        for to in self.V[s]:
            cur *= self.C.comb(rest, children[to])
            rest -= self.children[to]
            cur %= self.mod
        self.rep[s] = rep

        n = len(V[s])
        L = [0] * self.N; R = [0] * self.N
        L2 = [0] * self.N; R2 = [0] * self.N

        for i, ne in enumerate(self.V[s]):
            L[i] = R[i] = self.A[ne] * self.C.inv[self.children[ne]] % self.mod
            L2[i] = R2[i] = self.children[ne]
        for i in range(1, n):
            L[i] *= L[i - 1]
            L[i] %= self.mod
            L2[i] += L2[i - 1]
        for i in range(1, n - 1)[::-1]:
            R[i] *= R[i + 1]
            R[i] %= self.mod
            R2[i] += R2[i + 1]
        for i, ne in enumerate(self.V[s]):
            if ne == p:
                continue
            self.A[s] = 1
            self.children[s] = 1
            if i > 0:
                self.A[s] *= L[i - 1]
                self.A[s] %= self.mod
                self.children[s] += L2[i - 1]
            if i + 1 < n:
                self.A[s] *= R[i + 1]
                self.A[s] %= self.mod
                self.children[s] += R2[i + 1]
            self.A[s] *= self.C.fac[self.N - self.children[ne] - 1]
            self.A[s] %= self.mod
            self.dfs2(ne, s)
        return

class combination():
    # 素数のmod取るときのみ　速い
    def __init__(self, n, mod):
        self.n = n
        self.mod = mod
        self.fac = [1] * (n + 1)
        self.inv = [1] * (n + 1)
        for j in range(1, n + 1):
            self.fac[j] = self.fac[j - 1] * j % mod

        self.inv[n] = pow(self.fac[n], mod - 2, mod)
        for j in range(n - 1, -1, -1):
            self.inv[j] = self.inv[j + 1] * (j + 1) % mod

    def comb(self, n, r):
        if r > n or n < 0 or r < 0:
            return 0
        return self.fac[n] * self.inv[n - r] * self.inv[r] % self.mod

def ABC160():
    N = I()
    V = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = LI()
        a -= 1; b -= 1
        V[a].append(b)
        V[b].append(a)
    dp_r = treedp_rerooting(N,V,mod)
    dp_r.dfs(0)
    return
    dp_r.dfs2(0)

    ans = dp_r.rep

    for v in ans:
        print(v)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque

def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    ABC160()

"""
8
1 2
2 3
3 4
3 5
3 6
6 7
6 8
"""

# 40
# 280
# 840
# 120
# 120
# 504
# 72
# 72

Submission status: Wrong Answer

Solution code:
class treedp_rerooting():
    def __init__(self, N, V, MOD):
        self.N = N
        self.V = V
        self.mod = MOD
        self.rep = [0] * N
        self.children = [-1] * N
        self.A = [0] * N
        self.C = combination(N, MOD)
        return

    # 子の数のカウント
    def dfs(self, s):
        size = 1
        nowchild = []
        cur = 1
        self.children[s] = 0
        for i in self.V[s]:
            if self.children[i] != -1:
                continue
            ns, nc = dfs(i)
            nowchild.append(ns)
            size += ns
            cur *= nc
            cur %= self.mod
        self.children[s] = size
        rest = size - 1
        for k in nowchild:
            cur *= self.C.comb(rest, k)
            rest -= k
            cur %= self.mod
        self.A[s] = cur
        return size, cur

    def dfs2(self, s, p=-1):
        cur = 1
        rest = self.N - 1
        for to in self.V[s]:
            cur *= self.A[to]
            cur %= mod
        for to in self.V[s]:
            cur *= self.C.comb(rest, children[to])
            rest -= self.children[to]
            cur %= self.mod
        self.rep[s] = rep

        n = len(V[s])
        L = [0] * self.N; R = [0] * self.N
        L2 = [0] * self.N; R2 = [0] * self.N

        for i, ne in enumerate(self.V[s]):
            L[i] = R[i] = self.A[ne] * self.C.inv[self.children[ne]] % self.mod
            L2[i] = R2[i] = self.children[ne]
        for i in range(1, n):
            L[i] *= L[i - 1]
            L[i] %= self.mod
            L2[i] += L2[i - 1]
        for i in range(1, n - 1)[::-1]:
            R[i] *= R[i + 1]
            R[i] %= self.mod
            R2[i] += R2[i + 1]
        for i, ne in enumerate(self.V[s]):
            if ne == p:
                continue
            self.A[s] = 1
            self.children[s] = 1
            if i > 0:
                self.A[s] *= L[i - 1]
                self.A[s] %= self.mod
                self.children[s] += L2[i - 1]
            if i + 1 < n:
                self.A[s] *= R[i + 1]
                self.A[s] %= self.mod
                self.children[s] += R2[i + 1]
            self.A[s] *= self.C.fac[self.N - self.children[ne] - 1]
            self.A[s] %= self.mod
            dfs2(ne, s)
        return

class combination():
    # 素数のmod取るときのみ　速い
    def __init__(self, n, mod):
        self.n = n
        self.mod = mod
        self.fac = [1] * (n + 1)
        self.inv = [1] * (n + 1)
        for j in range(1, n + 1):
            self.fac[j] = self.fac[j - 1] * j % mod

        self.inv[n] = pow(self.fac[n], mod - 2, mod)
        for j in range(n - 1, -1, -1):
            self.inv[j] = self.inv[j + 1] * (j + 1) % mod

    def comb(self, n, r):
        if r > n or n < 0 or r < 0:
            return 0
        return self.fac[n] * self.inv[n - r] * self.inv[r] % self.mod

def ABC160():
    N = I()
    V = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = LI()
        a -= 1; b -= 1
        V[a].append(b)
        V[b].append(a)
    dp_r = treedp_rerooting(N,V,mod)
    return
    dp_r.dfs(0)
    dp_r.dfs2(0)

    ans = dp_r.rep

    for v in ans:
        print(v)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque

def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    ABC160()

"""
8
1 2
2 3
3 4
3 5
3 6
6 7
6 8
"""

# 40
# 280
# 840
# 120
# 120
# 504
# 72
# 72

Submission status: Wrong Answer

Solution code:
def examA():
    S = SI()
    if S[2]==S[3] and S[4]==S[5]:
        print("Yes")
    else:
        print("No")
    return

def examB():
    X = I()
    ans = 0
    ans += (X//500) * 1000
    X %= 500
    ans += (X//5) * 5
    print(ans)
    return

def examC():
    K, N = LI()
    A = LI()
    L = [0]*N
    L[0] = K-A[-1] + A[0]
    for i in range(1,N):
        L[i] = A[i]-A[i-1]
    ans = sum(L) - max(L)
    #print(L)
    print(ans)
    return

def examD():
    def bfs(n, e, fordfs):
        # 点の数、スタートの点、有向グラフ
        W = [-1] * n
        # 各点の状態量、最短距離とか,見たかどうかとか
        W[e] = 0
        que = deque()
        que.append(e)
        while que:
            now = que.popleft()
            nowW = W[now]
            for ne in fordfs[now]:
                if W[ne] == -1:
                    W[ne] = nowW + 1
                    que.append(ne)
        return W
    N, X, Y = LI()
    V = [[]for _ in range(N)]
    for i in range(N-1):
        V[i].append(i+1)
        V[i+1].append(i)
    V[X-1].append(Y-1)
    V[Y-1].append(X-1)
    #print(V)
    ans = [0]*(N-1)
    for i in range(N):
        L = bfs(N,i,V)
        for l in L:
            if l==0:
                continue
            ans[l-1] += 1
    for v in ans:
        print(v//2)
    return

def examE():
    X, Y, A, B, C = LI()
    P = LI()
    Q = LI()
    R = LI()
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)
    cnt = sum(P[:X]) + sum(Q[:Y])
    #print(cnt)
    X -= 1; Y -= 1
    for c in range(C):
        if X>=0 and Y>=0:
            if P[X] <= Q[Y]:
                if P[X] >= R[c]:
                    break
                cnt += (R[c] - P[X])
                X -= 1
            else:
                if Q[Y] >= R[c]:
                    break
                cnt += (R[c] - Q[Y])
                Y -= 1
        elif X>=0:
            if P[X] >= R[c]:
                break
            cnt += (R[c] - P[X])
            X -= 1
        elif Y>=0:
            if Q[Y] >= R[c]:
                break
            cnt += (R[c] - Q[Y])
            Y -= 1
    ans = cnt
    print(ans)
    return

def examF():
    class combination():
        # 素数のmod取るときのみ　速い
        def __init__(self, n, mod):
            self.n = n
            self.fac = [1] * (n + 1)
            self.inv = [1] * (n + 1)
            for j in range(1, n + 1):
                self.fac[j] = self.fac[j - 1] * j % mod

            self.inv[n] = pow(self.fac[n], mod - 2, mod)
            for j in range(n - 1, -1, -1):
                self.inv[j] = self.inv[j + 1] * (j + 1) % mod

        def comb(self, n, r, mod):
            if r > n or n < 0 or r < 0:
                return 0
            return self.fac[n] * self.inv[n - r] * self.inv[r] % mod

    N = I()
    V = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = LI()
        a -= 1; b -= 1
        V[a].append(b)
        V[b].append(a)
    C = combination(N, mod)

    A = [0]*N
    children = [-1] * N
    ans = [0]*N

    # 子の数のカウント
    def dfs(n, s, edges):
        size = 1
        nowchild = []
        cur = 1
        children[s] = 0
        for i in edges[s]:
            if children[i] != -1:
                continue
            ns, nc = dfs(n, i, edges)
            nowchild.append(ns)
            size += ns
            cur *= nc
            cur %= mod
        children[s] = size
        rest = size - 1
        for k in nowchild:
            cur *= C.comb(rest, k, mod)
            rest -= k
            cur %= mod
        A[s] = cur
        return size, cur

    def dfs2(s,p):
        #print(children, A)
        rep = 1
        rest = N - 1
        for to in V[s]:
            rep *= A[to]
            rep %= mod
        for to in V[s]:
            rep *= C.comb(rest, children[to], mod)
            rest -= children[to]
        ans[s] = rep

        n = len(V[s])
        L = [0]*n; R = [0]*n
        L2 = [0]*n; R2 = [0]*n

        for i,ne in enumerate(V[s]):
            L[i] = R[i] = A[ne] * C.inv[children[ne]]
            L2[i] = R2[i] = children[ne]
        for i in range(1,n):
            L[i] *= L[i-1]
            L[i] %= mod
            L2[i] += L2[i-1]
        for i in range(1,n-1)[::-1]:
            R[i] *= R[i+1]
            R[i] %= mod
            R2[i] += R2[i+1]
        for i,ne in enumerate(V[s]):
            if ne==p:
                continue
            A[s] = 1
            children[s] = 1
            if i > 0:
                A[s] *= L[i - 1]
                A[s] %= mod
                children[s] += L2[i - 1]
            if i + 1 < n:
                A[s] *= R[i + 1]
                A[s] %= mod
                children[s] += R2[i + 1]
            A[s] *= C.fac[N-children[ne]-1]
            A[s] %= mod
            children[ne] += children[s]
            #print(A[s],s,L,R)
            dfs2(ne, s)
            children[ne] -= children[s]
        return

    dfs(N, 0, V)
    dfs2(0,-1)
    for v in ans:
        print(v)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examF()

"""

"""

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import itertools

N = int(readline())
m = map(int,read().split())
AB = zip(m,m)

MOD = 10 ** 9 + 7

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

root = 1
parent = [0] * (N+1)
order = []
stack = [root]
while stack:
    x = stack.pop()
    order.append(x)
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)

fact = [1] * (N+10)
for n in range(1,N+10):
    fact[n] = n * fact[n-1] % MOD
fact_inv = [1] * (N+10)
fact_inv[-1] = pow(fact[-1], MOD-2, MOD)
for n in range(N+9, 0, -1):
    fact_inv[n-1] = fact_inv[n] * n % MOD

size_d = [0] * (N+1)
dp_d = [1] * (N+1)

for v in order[::-1]:
    dp_d[v] *= fact[size_d[v]]; dp_d[v] %= MOD
    p = parent[v]
    s = size_d[v] + 1
    size_d[p] += s
    dp_d[p] *= fact_inv[s] * dp_d[v]; dp_d[p] %= MOD

size_u = [N-2-x for x in size_d]
dp_u = [1] * (N+1)

for v in order[1:]:
    p = parent[v]
    x = dp_d[p]
    x *= dp_u[p]
    x *= fact_inv[size_d[p]]
    x *= fact[size_d[v]+1]
    x *= pow(dp_d[v], MOD-2, MOD)
    x *= fact[size_u[v]]
    x *= fact_inv[size_u[p]+1]
    dp_u[v] = x % MOD

for sd,su,xd,xu in itertools.islice(zip(size_d, size_u, dp_d, dp_u), 2, None):
    comb = fact[sd+su] * fact_inv[sd] * fact_inv[su] % MOD
    print(comb * xd * xu % MOD)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    from collections import deque

    input = sys.stdin.readline
    sys.setrecursionlimit(1000000)

    n = int(input())

    E = [[] for i in range(n)]
    for i in range(n-1):
        a, b = map(int, input().split())
        E[a-1].append(b-1)
        E[b-1].append(a-1)

    mod = 10 ** 9 + 7
    N = n + 1

    #逆元テーブル
    inv_t = [0]+[1]
    for i in range(2, N):
        inv_t += [inv_t[mod % i] * (mod - int(mod / i)) % mod]

    #階乗計算
    kai = [1, 1]
    rev_kai = [1, inv_t[1]]
    for i in range(2, N):
        kai.append(kai[-1] * i % mod)
        rev_kai.append(rev_kai[-1] * inv_t[i] % mod)

    # コンビネーション計算
    def cmb(n, r):
        return kai[n] * rev_kai[r] * rev_kai[n-r] % mod

    down = [{} for i in range(n+1)]
    up = [0 for i in range(n+1)]

    def dfs_down(cur, pre):
        if E[cur] == [pre]:
            down[pre][cur] = [1, 1]
        ns = []
        cnt = 1
        ns_append = ns.append
        for e in E[cur]:
            if e != pre:
                if e not in down[cur]:
                    dfs_down(e, cur)
                ni, cnti = down[cur][e]
                ns_append(ni)
                cnt = cnt * cnti % mod
        s = sum(ns)
        S = s
        for ni in ns:
            cnt = cnt * cmb(s, ni) % mod
            s -= ni
        down[pre][cur] = [S+1, cnt]

    def dfs_up(cur, pre):
        stack = deque([[cur, pre]])
        while stack:
            cur, pre = stack.pop() 
            if cur != 0:
                ns = []
                cnt = 1
                ns_append = ns.append
                if up[pre]:
                    ni, cnti = up[pre]
                    ns_append(ni)
                    cnt = cnt * cnti % mod
                bros = down[pre]
                for bro in bros:
                    if bro != cur:
                        ni, cnti = bros[bro]
                        ns_append(ni)
                        cnt = cnt * cnti % mod
                s = sum(ns)
                S = s
                for ni in ns:
                    cnt = cnt * cmb(s, ni) % mod
                    s -= ni
                up[cur] = [S+1, cnt]
            for e in E[cur]:
                if e != pre:
                    stack.append([e, cur])


    dfs_down(0, n)
    # dfs_up(0, n)


    # for i in range(n):
    #     ns = []
    #     cnt = 1
    #     ns_append = ns.append
    #     if up[i]:
    #         ni, cnti = up[i]
    #         ns_append(ni)
    #         cnt = cnt * cnti % mod
    #     for bro in down[i]:
    #         ni, cnti = down[i][bro]
    #         ns_append(ni)
    #         cnt = cnt * cnti % mod
    #     s = sum(ns)
    #     for ni in ns:
    #         cnt = cnt * cmb(s, ni) % mod
    #         s -= ni
    #     print(cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)

# 入力処理
N = int(input())
E = [[] for _ in range(N)]

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

dp = [1] * N
size = [1] * N

# 組み合わせ
mod = 10 ** 9 + 7
N_max = 2 * 10 ** 5  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N_max + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    # r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

# 深さ優先探索
def dfs(v, p=-1):

    dp_l = 1  # 自分自身しかない場合
    size_l = 1 # 自分自身のサイズを含める

    # 子の情報をあわせて数え上げる
    for u in E[v]:
        
        # 親へのルートは辿らない
        if u == p:
            continue

        d, s = dfs(u, v)
        size_l += s
        dp_l = dp_l * d * cmb(size_l-1, s) % mod

    dp[v] = dp_l % mod
    size[v] = size_l
    
    return (int(dp_l) % mod, size_l)

def bfs(v, p=-1):
    
    for u in E[v]:
        if u == p:
            continue
        
        # u がなくなった場合の dp[v] 値、 size を求める

        partial_v = int(dp[v] / dp[u] / cmb(N - 1, size[u]))
        
        # u に v 側の部分木が接続された場合の値を求める
        # u に v を根とする部分木をつける

        dp[u] = dp[u] * partial_v * cmb(N-1, size[u]-1) % mod
            
        bfs(u, v)
    
dfs(0)
bfs(0)

for i in range(N):
    print(dp[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)

# 入力処理
N = int(input())
E = [[] for _ in range(N)]

dp = [1] * N
size = [1] * N

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

# 組み合わせ
mod = 10 ** 9 + 7
N_max = 2 * 10 ** 5  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N_max + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    # r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

# 深さ優先探索
def dfs(v, p=-1):

    dp_l = 1  # 自分自身しかない場合
    size_l = 1 # 自分自身のサイズを含める

    # 子の情報をあわせて数え上げる
    for u in E[v]:
        
        # 親へのルートは辿らない
        if u == p:
            continue

        d, s = dfs(u, v)
        d %= mod
        size_l += s
        dp_l = dp_l * d * cmb(size_l-1, s) % mod

    dp[v] = dp_l % mod
    size[v] = size_l
        
    
    return (int(dp_l), size_l)


def bfs(v, p=-1):
    
    for u in E[v]:
        if u == p:
            continue
        
        # u がなくなった場合の dp[v] 値、 size を求める

        partial_v = int(dp[v] / dp[u] / cmb(N - 1, size[u]))
        
        # print(partial_v, dp[u])
        
        # u に v 側の部分木が接続された場合の値を求める
        # u に v を根とする部分木をつける

        dp_l = dp[u] * partial_v * cmb(N-1, size[u]-1) % mod
        
        dp[u] = dp_l % mod
    
        bfs(u, v)
    
dfs(0)

bfs(0)

for i in range(N):
    print(dp[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)

# 組み合わせ
mod = 10 ** 9 + 7
N_max = 2 * 10 ** 5  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N_max + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    # r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

# 深さ優先探索
def dfs(v, p=-1):

    dp_l = 1  # 自分自身しかない場合
    size_l = 1 # 自分自身のサイズを含める

    # 子の情報をあわせて数え上げる
    for u in E[v]:
        
        # 親へのルートは辿らない
        if u == p:
            continue

        d, s = dfs(u, v)
        d %= mod
        size_l += s
        dp_l = dp_l * d * cmb(size_l-1, s) % mod

    dp[v] = dp_l % mod
    size[v] = size_l
        
    return (int(dp_l), size_l)
  
def bfs(v, p=-1):
    
    for u in E[v]:
        if u == p:
            continue
        
        partial_v = int(dp[v] / dp[u] / cmb(N - 1, size[u]))
        
        dp[u] = dp[u] * partial_v * cmb(N-1, size[u]-1) % mod
    
        bfs(u,v)

# 入力処理
N = int(input())
E = [[] for _ in range(N)]

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

dfs(0)
bfs(0)

for i in range(N):
    print(dp[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)

# 組み合わせ
mod = 10 ** 9 + 7
N = 2* 10 ** 5  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod
  
# 深さ優先探索
def dfs(v, p=-1):

    # print(v, p, [x + 1 for x in E[v]])
    
    dp_l = 1  # 自分自身しかない場合
    size_l = 1 # 自分自身のサイズを含める

    # 子の情報をあわせて数え上げる
    for u in E[v]:
        
        # 親へのルートは辿らない
        if u == p:
            continue

        d, s = dfs(u, v)
        d %= mod
        size_l += s
        dp_l = dp_l * d * cmb(size_l-1, s) % mod

    dp[v] = dp_l
    size[v] = size_l
        
    return (int(dp_l), size_l)
  
def bfs(v, p=-1):
    
    for u in E[v]:
        if u == p:
            continue
        
        partial_v = int(dp[v] / dp[u] / cmb(N - 1, size[u]))
        
        dp[u] = dp[u] * partial_v * cmb(N-1, size[u]-1) % mod
    
        bfs(u,v)

# 入力処理
N = int(input())
E = [[] for _ in range(N)]

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

dfs(0)
bfs(0)

for i in range(N):
    print(dp[i])