Buggy python submission (verdict: Time Limit Exceeded):


n,k = map(int,input().split())
a = [0 for _ in range(n)]
b = [0 for _ in range(n)]
c = [[] for _ in range(n)]

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

pat = k
pat_arr = [0 for _ in range(n)]
pat_arr[0] = k
queue = [0]



while(queue):
	pos = queue.pop(0)
	num = k-2
	if(pos == 0):num += 1
	
	for i in c[pos]:
		if(pat_arr[i] != 0):continue
		queue.append(i)
		pat_arr[i] = num
		pat *= num
		pat %= 1000000007
		num -= 1
		
		
print(pat % 1000000007)


Fixed submission:
n,k = map(int,input().split())
#a = [0 for _ in range(n)]
#b = [0 for _ in range(n)]
c = [[] for _ in range(n)]

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

pat = k
pat_arr = [0 for _ in range(n)]
pat_arr[0] = k
queue = [0]



while(queue):
	pos = queue.pop(0)
	num = k-2
	if(pos == 0):num += 1
	
	for i in c[pos]:
		if(pat_arr[i] != 0):continue
		queue.append(i)
		pat_arr[i] = num
		pat *= num
		pat %= 1000000007
		num -= 1
		
		
print(pat % 1000000007)

Buggy python submission (verdict: Time Limit Exceeded):



n,k = map(int,input().split())
c = [[] for _ in range(n)]

for i in range(n - 1):
	A,B = map(int,input().split())
	
	#c[min(A - 1,B - 1)].append(max(A - 1,B - 1))
	
	c[A - 1].append(B - 1)
	c[B - 1].append(A - 1)

pat = k
pat_arr = [0 for _ in range(n)]
pat_arr[0] = k
queue = [0]

pat = k

for i in range(len(c)):c[i] = set(c[i])

"""
for i in c:print(len(i))


for j in range(len(c[0])):
	pat *= (k-j-1)
	pat %= 1000000007
for i in range(1,len(c)):
	for j in range(len(c[i])):
		pat *= (k-j-2)
		pat %= 1000000007
print(pat)






exit()
"""
while(queue):
	pos = queue.pop(0)
	num = k-2
	if(pos == 0):num += 1
	
	for i in c[pos]:
		if(pat_arr[i] != 0):continue
		queue.append(i)
		pat_arr[i] = num
		pat *= num
		pat %= 1000000007
		num -= 1
		
		
print(pat % 1000000007)

Fixed submission:
n,k = map(int,input().split())
#a = [0 for _ in range(n)]
#b = [0 for _ in range(n)]
c = [[] for _ in range(n)]

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

pat = k
pat_arr = [0 for _ in range(n)]
pat_arr[0] = k
queue = [0]



while(queue):
	pos = queue.pop(0)
	num = k-2
	if(pos == 0):num += 1
	
	for i in c[pos]:
		if(pat_arr[i] != 0):continue
		queue.append(i)
		pat_arr[i] = num
		pat *= num
		pat %= 1000000007
		num -= 1
		
		
print(pat % 1000000007)

Buggy python submission (verdict: Time Limit Exceeded):



n,k = map(int,input().split())
a = [0 for _ in range(n)]
b = [0 for _ in range(n)]
c = [[] for _ in range(n)]

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

pat = k
pat_arr = [0 for _ in range(n)]
pat_arr[0] = k
queue = [0]



while(queue):
	pos = queue.pop(0)
	num = k-2
	if(pos == 0):num += 1
	
	for i in c[pos]:
		if(pat_arr[i] != 0):continue
		queue.append(i)
		pat_arr[i] = num
		pat *= num
		pat %= 1000000007
		num -= 1
		
		
print(pat % 1000000007)

Fixed submission:
n,k = map(int,input().split())
#a = [0 for _ in range(n)]
#b = [0 for _ in range(n)]
c = [[] for _ in range(n)]

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

pat = k
pat_arr = [0 for _ in range(n)]
pat_arr[0] = k
queue = [0]



while(queue):
	pos = queue.pop(0)
	num = k-2
	if(pos == 0):num += 1
	
	for i in c[pos]:
		if(pat_arr[i] != 0):continue
		queue.append(i)
		pat_arr[i] = num
		pat *= num
		pat %= 1000000007
		num -= 1
		
		
print(pat % 1000000007)

Buggy python submission (verdict: Time Limit Exceeded):



n,k = map(int,input().split())
a = [0 for _ in range(n)]
b = [0 for _ in range(n)]
c = [[0 for _ in range(n)] for _ in range(n)]

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

pat = k
pat_arr = [0 for _ in range(n)]
pat_arr[0] = k
queue = [0]

while(queue):
	pos = queue.pop(0)
	num = k-2
	if(pos == 0):num += 1
	
	for i in range(n):
		if(c[pos][i] == 0 or pat_arr[i] != 0):continue
		queue.append(i)
		pat_arr[i] = num
		pat *= num
		pat %= 1000000007
		num -= 1
		
		
print(pat % 1000000007)

Fixed submission:
n,k = map(int,input().split())
#a = [0 for _ in range(n)]
#b = [0 for _ in range(n)]
c = [[] for _ in range(n)]

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

pat = k
pat_arr = [0 for _ in range(n)]
pat_arr[0] = k
queue = [0]



while(queue):
	pos = queue.pop(0)
	num = k-2
	if(pos == 0):num += 1
	
	for i in c[pos]:
		if(pat_arr[i] != 0):continue
		queue.append(i)
		pat_arr[i] = num
		pat *= num
		pat %= 1000000007
		num -= 1
		
		
print(pat % 1000000007)

Buggy python submission (verdict: Time Limit Exceeded):



n,k = map(int,input().split())
a = [0 for _ in range(n)]
b = [0 for _ in range(n)]
c = [[0 for _ in range(n)] for _ in range(n)]

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

pat = k
pat_arr = [0 for _ in range(n)]
pat_arr[0] = k
queue = [0]

while(queue):
	pos = queue.pop(0)
	num = k-2
	if(pos == 0):num += 1
	
	for i in range(n):
		if(c[pos][i] == 0 or pat_arr[i] != 0):continue
		queue.append(i)
		pat_arr[i] = num
		pat *= num
		num -= 1
		
		
print(pat % 1000000007)

Fixed submission:
n,k = map(int,input().split())
#a = [0 for _ in range(n)]
#b = [0 for _ in range(n)]
c = [[] for _ in range(n)]

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

pat = k
pat_arr = [0 for _ in range(n)]
pat_arr[0] = k
queue = [0]



while(queue):
	pos = queue.pop(0)
	num = k-2
	if(pos == 0):num += 1
	
	for i in c[pos]:
		if(pat_arr[i] != 0):continue
		queue.append(i)
		pat_arr[i] = num
		pat *= num
		pat %= 1000000007
		num -= 1
		
		
print(pat % 1000000007)