Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**6)
N,K=map(int, sys.stdin.readline().split())

al=[ [] for _ in range(N+1) ]	#隣接リスト
for _ in range(N-1):
    a,b=map(int, sys.stdin.readline().split())
    al[a].append(b)
    al[b].append(a)
   
mod=10**9+7
ans=1
def dfs(par,cur,num):
    global ans
    ans*=num
    ans%=mod
    if par==-1: #ルートノードの時は
        num=K-1  #K-1色使用できる
    else:       #ルートノードじゃない時は
        num=K-2  #K-2色使用できる
    for to in al[cur]:
        if to!=par: #遷移候補が親でなければ、遷移
            dfs(cur,to,num)
            num-=1
dfs(-1,1,K)

print ans

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from collections import deque
import copy

N,K=map(int, sys.stdin.readline().split())

 #階乗　前計算
mod=10**9+7
fact, inv_fact = [1], [1]
fact_tmp = 1
for i in range(1, K+1):
    fact_tmp *= i
    fact_tmp %= mod
    fact.append(fact_tmp)
    inv_fact.append(pow(fact_tmp, mod-2, mod))

def npr(n,r):
	if n < 0 or r < 0 or n < r:	return 0
	else:	return (fact[n] * inv_fact[n-r]) %mod



nl=[ [] for i in range(N+1) ]	#隣接リスト
C=[ 0 for i in range(N+1) ]	#入次数: 頂点に入ってくる辺数
 
for i in range(N-1):
	a,b=map(int, sys.stdin.readline().split())
	nl[a].append(b)
	C[a]+=1
	nl[b].append(a)
	C[b]+=1


start=1
Visit=[ None for i in range(N+1) ] 
Visit[start]=1
 
p=None		#Parent
q=deque()
q.append( (p,start) )

ans=K

while q:
	p,current=q.popleft()
	flag=0
	for to in nl[current]:
		if Visit[to] is None:
			Visit[to]=1
			q.append( (current,to) )
			flag=1
	if p is None and flag:
		ans*=npr(K-1,C[current])
	elif p is not None and flag:
		ans*=npr(K-2,C[current]-1)
	ans%=mod
		

print ans

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
N,K=map(int, sys.stdin.readline().split())

al=[ [] for _ in range(N+1) ]	#隣接リスト
for _ in range(N-1):
    a,b=map(int, sys.stdin.readline().split())
    al[a].append(b)
    al[b].append(a)
   
mod=10**9+7
ans=1
def dfs(par,cur,num):
    global ans
    ans*=num
    ans%=mod
    if par==-1: #ルートノードの時は
        num=K-1  #K-1色使用できる
    else:       #ルートノードじゃない時は
        num=K-2  #K-2色使用できる
    for to in al[cur]:
        if to!=par: #遷移候補が親でなければ、遷移
            dfs(cur,to,num)
            num-=1
dfs(-1,1,K)

print ans

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from collections import deque
import copy

N,K=map(int, sys.stdin.readline().split())

 #階乗　前計算
mod=10**9+7
fact, inv_fact = [1], [1]
fact_tmp = 1
for i in range(1, K+1):
    fact_tmp *= i
    fact_tmp %= mod
    fact.append(fact_tmp)
    inv_fact.append(pow(fact_tmp, mod-2, mod))

def npr(n,r):
	if n < 0 or r < 0 or n < r:	return 0
	else:	return (fact[n] * inv_fact[n-r]) %mod



nl=[ [] for i in range(N+1) ]	#隣接リスト
C=[ 0 for i in range(N+1) ]	#入次数: 頂点に入ってくる辺数
 
for i in range(N-1):
	a,b=map(int, sys.stdin.readline().split())
	nl[a].append(b)
	C[a]+=1
	nl[b].append(a)
	C[b]+=1


start=1
Visit=[ None for i in range(N+1) ] 
Visit[start]=1
 
p=None		#Parent
q=deque()
q.append( (p,start) )

ans=K

while q:
	p,current=q.popleft()
	flag=0
	for to in nl[current]:
		if Visit[to] is None:
			Visit[to]=1
			q.append( (current,to) )
			flag=1
	if p is None and flag:
		ans*=npr(K-1,C[current])
	elif p is not None and flag:
		ans*=npr(K-2,C[current]-1)
	ans%=mod
		

print ans

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque

N,K=map(int, sys.stdin.readline().split())
nl=[ [] for i in range(N+1) ]	#隣接リスト
C=[ 0 for i in range(N+1) ]	#入次数: 頂点に入ってくる辺数
 
for i in range(N-1):
	a,b=map(int, sys.stdin.readline().split())
	nl[a].append(b)
	C[a]+=1
	nl[b].append(a)
	C[b]+=1


Visit=[ 0 for i in range(N+1) ]


for i,x in enumerate(C):
	if x==1:
		start=i
		break


#階乗　前計算
mod=10**9+7
fact, inv_fact = [1], [1]
fact_tmp = 1
for i in range(1, K+1):
    fact_tmp *= i
    fact_tmp %= mod
    fact.append(fact_tmp)
    inv_fact.append(pow(fact_tmp, mod-2, mod))

def npr(n,r):
	if n < 0 or r < 0 or n < r:	return 0
	else:	return (fact[n] * inv_fact[n-r]) %mod


q=deque()
q.append(start)
Visit[start]=1

ans=1

while q: 
	fro=q.popleft()
	cnt=1	# fro1から距離２までの頂点数。fro自体も含めるので１からスタート
	for to1  in nl[fro]:
		if Visit[to1]==0:
			Visit[to1]=1
			cnt+=1
		for to2 in nl[to1]:
			if Visit[to2]==0:
				Visit[to2]=1
				cnt+=1
				q.append(to2)
	if fro==start:
		#print "npr(",K,",",cnt,") ::::", npr(K,cnt)
		ans*=npr(K,cnt)
	else:
		#print "npr(",K-C[fro],",",cnt-1,") :::: ",npr(K-C[fro],cnt-1)
		ans*=npr(K-C[fro],cnt-1)
	ans%=mod

print ans


Fixed submission:
# -*- coding: utf-8 -*-
import sys
from collections import deque
import copy

N,K=map(int, sys.stdin.readline().split())

 #階乗　前計算
mod=10**9+7
fact, inv_fact = [1], [1]
fact_tmp = 1
for i in range(1, K+1):
    fact_tmp *= i
    fact_tmp %= mod
    fact.append(fact_tmp)
    inv_fact.append(pow(fact_tmp, mod-2, mod))

def npr(n,r):
	if n < 0 or r < 0 or n < r:	return 0
	else:	return (fact[n] * inv_fact[n-r]) %mod



nl=[ [] for i in range(N+1) ]	#隣接リスト
C=[ 0 for i in range(N+1) ]	#入次数: 頂点に入ってくる辺数
 
for i in range(N-1):
	a,b=map(int, sys.stdin.readline().split())
	nl[a].append(b)
	C[a]+=1
	nl[b].append(a)
	C[b]+=1


start=1
Visit=[ None for i in range(N+1) ] 
Visit[start]=1
 
p=None		#Parent
q=deque()
q.append( (p,start) )

ans=K

while q:
	p,current=q.popleft()
	flag=0
	for to in nl[current]:
		if Visit[to] is None:
			Visit[to]=1
			q.append( (current,to) )
			flag=1
	if p is None and flag:
		ans*=npr(K-1,C[current])
	elif p is not None and flag:
		ans*=npr(K-2,C[current]-1)
	ans%=mod
		

print ans