Buggy python submission (verdict: Runtime Error):
n,k = map(int, input().split())
Tree = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    Tree[a-1].append(b-1)
    Tree[b-1].append(a-1)
#print(Tree)

mod = 10**9+7
def dfs(cur, pre):
    ret = 1
    nodecnt = 0 if pre == -1 else 1
    if cur > n-1 or cur < 0: return 0
    for nd in Tree[cur]:
        #print("現在のnode:",nd)
        if nd == pre: continue
        nodecnt += 1
        ret = ret*(k-nodecnt)%mod
        ret = ret*dfs(nd, cur)%mod
    return ret%mod

print(k*dfs(0,-1)%mod)

Fixed submission:
import sys
sys.setrecursionlimit(100005)
n,k = map(int, input().split())
Tree = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    Tree[a-1].append(b-1)
    Tree[b-1].append(a-1)
#print(Tree)

mod = 10**9+7
def dfs(cur, pre):
    ret = 1
    nodecnt = 0 if pre == -1 else 1
    if cur > n-1 or cur < 0: return 0
    for nd in Tree[cur]:
        #print("現在のnode:",nd)
        if nd == pre: continue
        nodecnt += 1
        ret = ret*(k-nodecnt)%mod
        ret = ret*dfs(nd, cur)%mod
    return ret%mod

print(k*dfs(0,-1)%mod)

Buggy python submission (verdict: Runtime Error):
n,k = map(int, input().split())
Tree = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    Tree[a-1].append(b-1)
    Tree[b-1].append(a-1)
#print(Tree)

mod = 10**9+7
def dfs(cur, pre):
    ret = 1
    nodecnt = 0 if pre == -1 else 1

    for nd in Tree[cur]:
        #print("現在のnode:",nd)
        if nd == pre: continue
        nodecnt += 1
        ret = ret*(k-nodecnt)%mod
        ret = ret*dfs(nd, cur)%mod
    return ret%mod

print(k*dfs(0,-1)%mod)

Fixed submission:
import sys
sys.setrecursionlimit(100005)
n,k = map(int, input().split())
Tree = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    Tree[a-1].append(b-1)
    Tree[b-1].append(a-1)
#print(Tree)

mod = 10**9+7
def dfs(cur, pre):
    ret = 1
    nodecnt = 0 if pre == -1 else 1
    if cur > n-1 or cur < 0: return 0
    for nd in Tree[cur]:
        #print("現在のnode:",nd)
        if nd == pre: continue
        nodecnt += 1
        ret = ret*(k-nodecnt)%mod
        ret = ret*dfs(nd, cur)%mod
    return ret%mod

print(k*dfs(0,-1)%mod)

Buggy python submission (verdict: Runtime Error):
n,k = map(int, input().split())
Tree = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    Tree[a-1].append(b-1)
    Tree[b-1].append(a-1)
#print(Tree)

mod = 10**9+7
def dfs(cur, pre):
    ret = 1
    nodecnt = 0 if pre == -1 else 1

    for nd in Tree[cur]:
        #print("現在のnode:",nd)
        if nd == pre: continue
        nodecnt += 1
        ret = ret*(k-nodecnt)
        ret %= mod
        ret = ret*dfs(nd, cur)
    return ret%mod

print(k*dfs(0,-1)%mod)

Fixed submission:
import sys
sys.setrecursionlimit(100005)
n,k = map(int, input().split())
Tree = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    Tree[a-1].append(b-1)
    Tree[b-1].append(a-1)
#print(Tree)

mod = 10**9+7
def dfs(cur, pre):
    ret = 1
    nodecnt = 0 if pre == -1 else 1
    if cur > n-1 or cur < 0: return 0
    for nd in Tree[cur]:
        #print("現在のnode:",nd)
        if nd == pre: continue
        nodecnt += 1
        ret = ret*(k-nodecnt)%mod
        ret = ret*dfs(nd, cur)%mod
    return ret%mod

print(k*dfs(0,-1)%mod)