Buggy python submission (verdict: Runtime Error):
def main():
	N, K = map(int, input().split())
	DIV = 10**9+7
	ab = [[] for _ in range(N)]

	size = 10**5+1
	fact = [0]*size
	inverse = [0]*size
	inv_cal = [0]*size
	fact[:2] = [1, 1]
	inverse[:2] = [1, 1]
	inv_cal[:2] = [0, 1]

	for i in range(2, size):
		fact[i] = (fact[i-1]*i%DIV)
		inv_cal[i] = (-inv_cal[DIV%i]*(DIV//i))%DIV
		inverse[i] = inverse[i-1]*inv_cal[i]%DIV

	def C(n, r):
		ans = fact[n]*inverse[r]%DIV
		return ans*inverse[n-r]%DIV

	def P(n, r):
		if n < 0:
			return 0
		ans = C(n, r)
		return ans*fact[r]%DIV

	for _ in range(N-1):
		u, v = map(int, input().split())
		ab[u-1].append(v-1)
		ab[v-1].append(u-1)

	def dfs(v, ans=1, p=-1):
		for u in ab[v]:
			if u == p:
				continue
			ans = dfs(u, ans, v)
		c = 0
		nk = K
		if p == -1:
			c = len(ab[v])+1
		else:
			c = len(ab[v])-1
			nk = K-2
		ans *= P(nk, c)
		return ans%DIV
	ans = dfs(0)
	print(ans)


if __name__ == '__main__':
	main()

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def main():
	N, K = map(int, input().split())
	DIV = 10**9+7
	ab = [[] for _ in range(N)]

	def P(n, r):
		ans = 1
		for i in range(r):
			ans = (ans*(n-i)) % DIV
		return ans

	for _ in range(N-1):
		u, v = map(int, input().split())
		ab[u-1].append(v-1)
		ab[v-1].append(u-1)

	def dfs(v, p=-1):
		c = 0
		ans = 1
		for u in ab[v]:
			if u == p:
				continue
			c += 1
			ans = ans * dfs(u, v) % DIV
		nk = K-1
		if p != -1:
			nk = K-2
		ans *= P(nk, c)
		return ans%DIV
	ans = dfs(0)*K%DIV
	print(ans)


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Runtime Error):
def main():
	N, K = map(int, input().split())
	DIV = 10**9+7
	ab = [[] for _ in range(N)]

	size = max(N+1, K+1)
	fact = [0]*size
	inverse = [0]*size
	inv_cal = [0]*size
	fact[:2] = [1, 1]
	inverse[:2] = [1, 1]
	inv_cal[:2] = [0, 1]

	for i in range(2, size):
		fact[i] = (fact[i-1]*i%DIV)
		inv_cal[i] = (-inv_cal[DIV%i]*(DIV//i))%DIV
		inverse[i] = inverse[i-1]*inv_cal[i]%DIV

	def C(n, r):
		ans = fact[n]*inverse[r]%DIV
		return ans*inverse[n-r]%DIV

	def P(n, r):
		if n < 0:
			return 0
		ans = C(n, r)
		return ans*fact[r]%DIV

	for _ in range(N-1):
		u, v = map(int, input().split())
		ab[u-1].append(v-1)
		ab[v-1].append(u-1)

	def dfs(v, ans=1, p=-1):
		for u in ab[v]:
			if u == p:
				continue
			ans = dfs(u, ans, v)
		c = 0
		nk = K
		if p == -1:
			c = len(ab[v])+1
		else:
			c = len(ab[v])-1
			nk = K-2
		ans *= P(nk, c)
		return ans%DIV
	ans = dfs(0)
	print(ans)


if __name__ == '__main__':
	main()

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def main():
	N, K = map(int, input().split())
	DIV = 10**9+7
	ab = [[] for _ in range(N)]

	def P(n, r):
		ans = 1
		for i in range(r):
			ans = (ans*(n-i)) % DIV
		return ans

	for _ in range(N-1):
		u, v = map(int, input().split())
		ab[u-1].append(v-1)
		ab[v-1].append(u-1)

	def dfs(v, p=-1):
		c = 0
		ans = 1
		for u in ab[v]:
			if u == p:
				continue
			c += 1
			ans = ans * dfs(u, v) % DIV
		nk = K-1
		if p != -1:
			nk = K-2
		ans *= P(nk, c)
		return ans%DIV
	ans = dfs(0)*K%DIV
	print(ans)


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Runtime Error):
def main():
	N, K = map(int, input().split())
	DIV = 10**9+7
	ab = [[] for _ in range(N)]

	size = max(N+1, K+1)
	fact = [0]*size
	inverse = [0]*size
	inv_cal = [0]*size
	fact[:2] = [1, 1]
	inverse[:2] = [1, 1]
	inv_cal[:2] = [0, 1]

	for i in range(2, size):
		fact[i] = (fact[i-1]*i%DIV)
		inv_cal[i] = (-inv_cal[DIV%i]*(DIV//i))%DIV
		inverse[i] = inverse[i-1]*inv_cal[i]%DIV

	def C(n, r):
		ans = fact[n]*inverse[r]%DIV
		return ans*inverse[n-r]%DIV

	def P(n, r):
		ans = C(n, r)
		return ans*fact[r]%DIV

	for _ in range(N-1):
		u, v = map(int, input().split())
		ab[u-1].append(v-1)
		ab[v-1].append(u-1)

	def dfs(v, ans=1, p=-1):
		for u in ab[v]:
			if u == p:
				continue
			ans = dfs(u, ans, v)
		c = 0
		nk = K
		if p == -1:
			c = len(ab[v])+1
		else:
			c = len(ab[v])-1
			nk = K-2
		ans *= P(nk, c)
		ans %= DIV
		return ans
	ans = dfs(0)
	print(ans)


if __name__ == '__main__':
	main()

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def main():
	N, K = map(int, input().split())
	DIV = 10**9+7
	ab = [[] for _ in range(N)]

	def P(n, r):
		ans = 1
		for i in range(r):
			ans = (ans*(n-i)) % DIV
		return ans

	for _ in range(N-1):
		u, v = map(int, input().split())
		ab[u-1].append(v-1)
		ab[v-1].append(u-1)

	def dfs(v, p=-1):
		c = 0
		ans = 1
		for u in ab[v]:
			if u == p:
				continue
			c += 1
			ans = ans * dfs(u, v) % DIV
		nk = K-1
		if p != -1:
			nk = K-2
		ans *= P(nk, c)
		return ans%DIV
	ans = dfs(0)*K%DIV
	print(ans)


if __name__ == '__main__':
	main()