Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03998 (python)

Fixed submission:
sa = list(input())
sb = list(input())
sc = list(input())

d = 'a'
cnta = 0
cntb = 0
cntc = 0
A = len(sa)
B = len(sb)
C = len(sc)

while 1:
    if d == 'a':
        cnta += 1
        if cnta == A+1:
            print('A')
            break
        d = sa.pop(0)
    elif d == 'b':
        cntb += 1
        if cntb == B+1:
            print('B')
            break
        d = sb.pop(0)
    else:
        cntc += 1
        if cntc == C+1:
            print('C')
            break
        d = sc.pop(0)

Buggy submission (verdict: Runtime Error):
sb = list(input())
sc = list(input())

d = sa[0]

while 1:
    if d == 'a':
        sa = sa.pop(0)
        if sa == []:
            print('A')
            break
        d = sa[0]
    elif d == 'b':
        sb = sb.pop(0)
        if sb == []:
            print('B')
            break
        d = sb[0]
    else:
        sc = sc.pop(0)
        if sc == []:
            print('C')
            break
        d = sc[0]

Buggy submission (verdict: Runtime Error):
sa = list(input())
sb = list(input())
sc = list(input())

d = sa[0]

while (len(sa) == 0 or len(sb) == 0 or len(sc) == 0) == 0:
    if d == 'a':
        sa = sa[1::]
        d = sa[0]
    elif d == 'b':
        sb = sb[1::]
        d = sb[0]
    else:
        sc = sc[1::]
        d = sc[0]

if len(sa) == 0:
    print('A')
elif len(sb) == 0:
    print('B')
else:
    print('C')

Buggy submission (verdict: Runtime Error):
sa = list(input())
sb = list(input())
sc = list(input())

d = sa[0]

while d !=[]:
    if d == 'a':
        sa = sa[1::]
        d = sa[0]
    elif d == 'b':
        sb = sb[1::]
        d = sb[0]
    else:
        sc = sc[1::]
        d = sc[0]

if len(sa) == 0:
    print('A')
elif len(sb) == 0:
    print('B')
else:
    print('C')

Buggy submission (verdict: Runtime Error):
sa = list(input())
sb = list(input())
sc = list(input())

d = sa[0]

while (len(sa) == 0 or len(sb) == 0 or len(sc) == 0) == 0:
    if d == 'a':
        sa = sa[1::]
        d = sa[0]
    elif d == 'b':
        sb = sb[1::]
        d = sb[0]
    else:
        sc = sc[1::]
        d = sc[0]

if len(sa) == 0:
    print('A')
elif len(sb) == 0:
    print('B')
else:
    print('C')

Buggy submission (verdict: Runtime Error):
sa = list(input())
sb = list(input())
sc = list(input())

cnta = len(sa)
cntb = len(sb)
cntc = len(sc)

d = sa[0]

while not(cnta == 0 or cntb == 0 or cntc == 0):
    if d == 'a':
        sa = sa[1::]
        d = sa[0]
    elif d == 'b':
        sb = sb[1::]
        d = sb[0]
    else:
        sc = sc[1::]
        d = sc[0]
if cnta == 0:
    print('A')
elif cntb == 0:
    print('B')
else:
    print('C')
Problem p03101 (python)

Fixed submission:
h,w = list(map(int,input().split()))
h1,w1 = list(map(int,input().split()))

print((h-h1)*(w-w1))

Buggy submission (verdict: Runtime Error):
h,w = list(map(int,input().split())
h1,w1 = list(map(int,input().split())

print((h-h1)*(w-w1))

Buggy submission (verdict: Runtime Error):
h,w = list(map(int,input().split())
h1,w1 = list(map(intinput().split())

print((h-h1)*(w-w1))
Problem p03102 (python)

Fixed submission:
n,m,c = list(map(int,input().split()))
b = list(map(int,input().split()))

def solve(a,b,c):
    val = 0
    for ia,ib in zip(a,b):
        val += ia*ib
    val += c
    return val > 0

cnt = 0
for i in range(n):
    a = list(map(int,input().split()))
    if solve(a,b,c):
        cnt += 1

print(cnt)

Buggy submission (verdict: Runtime Error):
n,m,c = list(map(int,input().split()))
b = list(map(int,input().split()))

def solve(a,b,c):
    val = 0
    for ia,ib in zip(a,b):
        val += ia*ib
    val += c
    return val > 0

cnt = 0
for i in range(n):
    a = list(map(int,input().split())
    if solve(a,b,c):
        cnt += 1

print(cnt)
Problem p03200 (python)

Fixed submission:
b = input()
white = 0
bl = []
for k in b:
    if k == 'B':
        bl.append(1)
    else:
        white += 1
        bl.append(0)
k = 0
wp = white
ans = 0
for p in bl:
    if p==1:
        ans += wp - k
        wp += 1
    k += 1

print(ans)

Buggy submission (verdict: Runtime Error):
st = input()

def find_bw(s):
  k = 0
  temp = False
  while True:
    if s[k] == "B":
      while True:
        if s[k] == "W":
          temp = True
          break
        k+= 1
    if temp:
      break
    k+=1
    if k>len(s):
      break
  return k

print(find_bw(st))
Problem p03673 (python)

Fixed submission:
n = int(input())
List = list(map(int,input().split()))
ans = List[-1-n%2::-2]+List[::2]
print(*ans[::(-1)**n],end=" ")

Buggy submission (verdict: Runtime Error):
List = list(map(int,input().split()))
print(*List[::-2],*List[n%2::2])
Problem p03846 (python)

Fixed submission:
n = int(input()) 
a = list(map(int,input().split())) 
sa = sorted(a) 

if n % 2 == 0:
  k = 1
  l = 0
else:
  k = 0
  l = 1 

a=1000000007
flag = True 
for i,s in enumerate(sa):
  if ((i+l)//2)*2+k != s:
    flag = False
if flag:
  print(2**(n//2) % a)
else:
  print(0)

Buggy submission (verdict: Runtime Error):

n = int(input())
a = list(map(int,input().split()))

if n%2==0:
    if sorted(a) != [(k//2)*2+1 for k in range(n)]:
        print(0)
    else:
        print(int(2**(n//2) % (10e9+7)))
else:
    if sorted(a)!= [0]+[(k//2+1)*2 for k in range(n-1)]:
        print(0)
    else:
        print(int(2**(n//2) % (10e9+7)))

Buggy submission (verdict: Runtime Error):
n = int(input()) 
a = list(map(int,input().split())) 
sa = sorted(a) 

if n % 2 == 0:
  k = 1
  l = 0
else:
  k = 0
  l = 1 

a=1000000007
lag = True 
for i,s in enumerate(sa):
  if ((i+l)//2)*2+k != s:
    flag = False
if flag:
  print(2**(n//2) % a)
else:
  print(0)

Buggy submission (verdict: Runtime Error):
n = int(intput())

a = list(map(int,input().split()))

sa = sorted(a)

if n % 2 == 0:
  k = 0
else:
  k = 1

flag = True

for i,s in enumerate(sa):
  if (i/2)*2+k != s:
    flag = False

if flag:
  print(2**n/2)
else:
  print(0)

Buggy submission (verdict: Runtime Error):
n = int(intput())

a = list(map(int,input().split()))

sa = sorted(a)

if n % 2 == 0:
  k = 0
else:
  k = 1

flag = True

for i,s in enumerate(sa):
  if (i/2)*2+k != s:
    flag = false

if flag:
  print(2**n/2)
else:
  print(0)

Buggy submission (verdict: Runtime Error):
n = int(intput())

a = list(map(int,input().split()))

sa = sorted(a)

if n % 2 == 0:
  k = 0
else:
  k = 1

flag = True

for i,s in enumerate(sa):
  if (i//2)*2+k != s:
    flag = false

if flag:
  print(2**n//2)
else:
  print(0)

Buggy submission (verdict: Runtime Error):
n = int(intput())

a = list(map(int,input().split()))

sa = sorted(a)

if n % 2 == 0;
  k = 0
else;
  k = 1

flag = True

for i,s in enumerate(sa);
  if (i//2)*2+k != s;
    flag = false

if flag;
  print(2**n//2)
else;
  print(0)
Problem p02882 (python)

Fixed submission:
import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions
from operator import itemgetter


import sys
def input():
	return sys.stdin.readline().strip()

a, b, x = list(map(int, input().split()))

if x <= (a**2)*b / 2:
	shita = math.degrees(math.atan(a*(b**2)/(2*x)))
else:
	shita = math.degrees(math.atan( 2*((a**2)*b - x)/(a**3) ))

print(shita)

Buggy submission (verdict: Runtime Error):

import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions
from operator import itemgetter
from scipy.sparse import csr_matrix #pypyだとエラーになるので、pythonを使うこと
from scipy.sparse.csgraph import shortest_path, floyd_warshall
import numpy as np

import sys
def input():
	return sys.stdin.readline().strip()

a, b, x = list(map(int, input().split()))

if x <= (a**2)*b / 2:
	shita = math.degrees(math.atan(a*(b**2)/(2*x)))
else:
	shita = math.degrees(math.atan( 2*((a**2)*b - x)/(a**3) ))

print(shita)
Problem p02885 (python)

Fixed submission:
A, B = list(map(int, input().split()))

print(max(0, A - 2*B))

Buggy submission (verdict: Runtime Error):

from scipy.sparse import csr_matrix #pypyだとエラーになるので、pythonを使うこと
from scipy.sparse.csgraph import shortest_path, floyd_warshall
import numpy as np

import sys
def input():
	return sys.stdin.readline().strip()

N, M, L = list(map(int, input().split()))

graph = np.full((N, N), np.inf)
for i in range(M):
	A, B, C = list(map(int, input().split()))
	graph[A - 1, B - 1] = C
	graph[B - 1, A - 1] = C

graph = csr_matrix(graph)
dist = floyd_warshall(graph, directed=False)
graph2 = np.full((N, N), np.inf)
for i in range(N):
	for j in range(N):
		if dist[i, j] <= L:
			graph2[i, j] = 1
graph2 = csr_matrix(graph2)
num = shortest_path(graph2, directed=False)


Q = int(input())
for q in range(Q):
	s, t = list(map(int, input().split()))
	if num[s - 1][t - 1] < 10000000:
		print(int(num[s - 1][t - 1] - 1))
	else:
		print(-1)
Problem p02902 (python)

Fixed submission:

import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions

import sys
def input():
	return sys.stdin.readline().strip()

N, M = list(map(int, input().split()))

graph = [[] for _ in range(N)]

for i in range(M):
	A, B = list(map(int, input().split()))
	A -= 1
	B -= 1
	graph[A].append(B)


for start in range(N):
	parent = [-1] * N
	depth = [0] * N
	que = deque([])
	que.append(start)
	depth[start] = 1
	while len(que) > 0:
		node = que.popleft()
		judge = 0
		if len(graph[node]) > 0:
			for node_next in graph[node]:
				if depth[node_next] == 1:
					judge = 1
				if depth[node_next] == 0:
					depth[node_next] = depth[node] + 1
					que.append(node_next)
					parent[node_next] = node
		if judge == 1:
			last = node
			break
	if judge == 1:
		break


if judge != 1:
	print(-1)
	exit()

node_list = deque([last])
while True:
	loc = parent[node_list[0]]
	if loc == -1:
		break
	else:
		node_list.appendleft(loc)

node_set = set([node_list[0]])


for i in range(1, len(node_list)):
	max_depth = -1
	for edge in graph[node_list[i]]:
		if edge in node_set:
			for j in range(i):
				if edge == node_list[j]:
					max_depth = max(max_depth, j)
	if max_depth >= 0:
		start = max_depth
		end = i
		break
	node_set.add(node_list[i])

#print(node_set, node_list, depth)
print(end + 1 - start)
for node in range(start, end + 1):
	print(node_list[node] + 1)

Buggy submission (verdict: Runtime Error):

import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions

import sys
def input():
	return sys.stdin.readline().strip()

N, M = list(map(int, input().split()))

graph = [[] for _ in range(N)]

for i in range(M):
	A, B = list(map(int, input().split()))
	A -= 1
	B -= 1
	graph[A].append(B)


for start in range(N):
	parent = [-1] * N
	depth = [0] * N
	que = deque([])
	que.append(start)
	depth[start] = 1
	while len(que) > 0:
		node = que.popleft()
		if len(graph[node]) > 0:
			judge = 0
			for node_next in graph[node]:
				if depth[node_next] == 1:
					judge = 1
				if depth[node_next] == 0:
					depth[node_next] = depth[node] + 1
					que.append(node_next)
					parent[node_next] = node
			if judge == 1:
				last = node
				break
		if judge == 1:
			break


if judge != 1:
	print(-1)
	exit()

node_list = deque([last])
while True:
	loc = parent[node_list[0]]
	if loc == -1:
		break
	else:
		node_list.appendleft(loc)

node_set = set([node_list[0]])

for i in range(1, len(node_list)):
	max_depth = -1
	for edge in graph[node_list[i]]:
		if edge in node_set:
			for j in range(i):
				if edge == node_list[j]:
					max_depth = max(max_depth, j)
	if max_depth >= 0:
		start = max_depth
		end = i
	node_set.add(node_list[i])

print(end + 1 - start)
for node in range(start, end + 1):
	print(node_list[node] + 1)
Problem p02936 (python)

Fixed submission:

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq


N, Q = list(map(int, input().split()))
eda = [[] for i in range(N)]
ope = [0]*N
score = [0]*N
parent = [-1]*N


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

for i in range(Q):
	p, x = list(map(int, input().split()))
	ope[p - 1] += x


que = []
que.append(0)
score[0] = ope[0]
for ss in range(N - 1):
	loc = que.pop()
	for edge in eda[loc]:
		if parent[loc] != edge:
			score[edge] += score[loc] + ope[edge]
			que.append(edge)
			parent[edge] = loc

for i in range(N - 1):
	print(score[i], end = "")
	print(" ", end = "")
print(score[N - 1])

Buggy submission (verdict: Runtime Error):

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq


N, Q = list(map(int, input().split()))

for i in range(N - 1):
	a, b = list(map(int, input().split())

for i in range(Q):
	p, x = list(map(int, input().split()))

Buggy submission (verdict: Runtime Error):
input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq


N, Q = list(map(int, input().split()))
eda = [[] for i in range(N)]
ope = [0]*N
score = [0]*N
parent = [-1]*N


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

for i in range(Q):
	p, x = list(map(int, input().split()))
	ope[p - 1] += x


que = []
que.append(0)
score[0] = ope[0]
for ss in range(N - 1):
	loc = que.pop()
	for edge in eda[loc]:
		if parent[loc] != edge:
			score[edge] += score[loc] + ope[edge]
			que.append(edge)
			parent[edge] = loc

for i in range(N - 1):
	print(score[i], end = "")
	print(" ", end = "")
print(score[N - 1])
Problem p02939 (python)

Fixed submission:
S = input()

now = S[0]
i = 1
mozi_list = [now]
num = 1
while i < len(S):
	#print(i, mozi_list)
	if i != len(S) - 1:
		if len(now) == 1:
			if now != S[i]:
				now = S[i]
				mozi_list.append(now)
				i += 1
				num += 1
			else:
				now = S[i:i + 2]
				mozi_list.append(now)
				i += 2
				num += 1
		elif len(now) == 2:
			now = S[i]
			mozi_list.append(now)
			i += 1
			num += 1
	else:
		if len(now) == 2:
			now = S[i]
			mozi_list.append(now)
			i += 1
			num += 1
		elif now != S[i]:
			now = S[i]
			mozi_list.append(now)
			i += 1
			num += 1
		else:
			i += 1

print(num)

Buggy submission (verdict: Runtime Error):

S = input()

now = S[0]
i = 1
mozi_list = [now]
num = 1
while i < len(S):
	#print(i, mozi_list)
	if i != len(S) - 1:
		if len(now) == 1:
			if now != S[i]:
				now = S[i]
				mozi_list.append(now)
				i += 1
				num += 1
			else:
				now = S[i:i + 2]
				mozi_list.append(now)
				i += 2
				num += 1
		if len(now) == 2:
			now = S[i]
			mozi_list.append(now)
			i += 1
			num += 1
	else:
		if len(now) == 2:
			now = S[i]
			mozi_list.append(now)
			i += 1
			num += 1
		elif now != S[i]:
			now = S[i]
			mozi_list.append(now)
			i += 1
			num += 1
		else:
			i += 1

print(num)
Problem p02960 (python)

Fixed submission:

import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math

S = input()
amari = [1, 10, 9, 12, 3, 4]
kurai = 0
hatena = []
base = 0
for i in range(len(S)):
	#print(S[len(S) - 1 - i])
	if S[len(S) - 1 - i] == '?':
		hatena.append(kurai)
	else:
		base += (int(S[len(S) - 1 - i])*amari[i % 6])
		base = base % 13
	kurai += 1

#print(base, hatena)
count13 = [0]*13
count13[base]+=1
for kurai in hatena:
	countnow = [0]*13
	for num in range(10):
		x = num * amari[kurai % 6]
		for i in range(13):
			y = (i + x) % 13
			countnow[y] += count13[i]
	for i in range(13):
		countnow[i] = countnow[i] % 1000000007
	count13 = copy.deepcopy(countnow)

print(count13[5])

Buggy submission (verdict: Runtime Error):

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
import math

S = input()
amari = [1, 10, 9, 12, 3, 4]
kurai = 0
hatena = []
base = 0
for i in range(len(S)):
	#print(S[len(S) - 1 - i])
	if S[len(S) - 1 - i] == '?':
		hatena.append(kurai)
	else:
		base += (int(S[len(S) - 1 - i])*amari[i % 6])
		base = base % 13
	kurai += 1

#print(base, hatena)
count13 = [0]*13
count13[base]+=1
for kurai in hatena:
	countnow = [0]*13
	for num in range(10):
		x = num * amari[kurai % 6]
		for i in range(13):
			y = (i + x) % 13
			countnow[y] += count13[i]
	for i in range(13):
		countnow[i] = countnow[i] % 1000000007
	count13 = copy.deepcopy(countnow)

print(count13[5])

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
import math

S = input()
amari = [1, 10, 9, 12, 3, 4]
kurai = 0
hatena = []
base = 0
for i in range(len(S)):
	#print(S[len(S) - 1 - i])
	if S[len(S) - 1 - i] == '?':
		hatena.append(kurai)
	else:
		base += (int(S[len(S) - 1 - i])*amari[i % 6])
		base = base % 13
	kurai += 1

#print(base, hatena)
count13 = [0]*13
count13[base]+=1
for kurai in hatena:
	countnow = [0]*13
	for num in range(10):
		x = num * amari[kurai % 6]
		for i in range(13):
			y = (i + x) % 13
			countnow[y] += count13[i]
	for i in range(13):
		countnow[i] = countnow[i] % 1000000007
	count13 = copy.deepcopy(countnow)

print(count13[5])
Problem p02963 (python)

Fixed submission:
import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
import math

S = int(input())

a = math.ceil(S**0.5)

XY = []
XY.append(a)
XY.append(0)
rest = -S + a**2
if rest > a:
	XY.append(a - 1)
	XY.append(a - 1)
	rest = rest - a
else:
	XY.append(a - 1)
	XY.append(a)

XY.append(0)
XY.append(rest)

for i in range(6):
	print(XY[i], end="")
	print(" ", end="")
print("\n")

Buggy submission (verdict: Runtime Error):

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
import math

N, K = list(map(int, input().split()))

A = list(map(int, input().split()))

def minus():
	return -1

D = defaultdict(minus)
D2 = defaultdict(minus)
jump = []
first = deque()
for i in range(N):
	if D[A[i]] != -1:
		bisect.insort(jump, (D[A[i]], i))
	D[A[i]] = i
	if D2[A[i]] == -1:
		D2[A[i]] = i
	first.append(D2[A[i]])

x = 0
roop = [0]
syugo = set()
syugo.add(x)
while True:
	while True:
		loc = bisect.bisect_left(jump, (x, x))
		#print(x,loc,jump)
		if loc < len(jump):
			if x == jump[loc][0]:
				x = jump[loc][1] + 1
			else:
				break
		else:
			break
	if x == N:
		x = 0
	else:
		x = first[x % N] + 1
	size1 = len(syugo)
	syugo.add(x)
	size2 = len(syugo)
	if size1 == size2:
		break
	else:
		roop.append(x)
mod = (K - 1) % len(roop)
x = roop[mod] %N
#print(roop, first, x)
output = []
while x < N:
	loc = bisect.bisect_left(jump, (x, x))
	if loc < len(jump):
		if x == jump[loc][0]:
			x = jump[loc][1]
			x = x + 1
		else:
			output.extend(A[x:jump[loc][0]])
			x = jump[loc][0]
			break
	else:
		output.extend(A[x:N])
		break

for i in range(len(output)):
	print(output[i], end='')
	print(" ", end='')
print('\n')
Problem p02973 (python)

Fixed submission:

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

N = int(input())
A = []
for i in range(N):
	A.append(int(input()))

B = []
min = -10000000000
for i in range(N):
	loc = bisect.bisect_right(B, -A[i])
	if loc == len(B):
		B.append(-A[i])
	else:
		B[loc] = -A[i]

print(len(B))

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

N = int(input())
A = []
for i in range(N):
	A.append(int(input()))

B = []
min = -10000000000
for i in range(N):
	if min <= -A[i]:
		min = -A[i]
		B.append(-A[i])
	else:
		loc = bisect.bisect_right(B, -A[i])
		B[loc] = -A[i]

print(len(B))
Problem p02981 (python)

Fixed submission:
N, A, B = list(map(int,input().split()))

print(min(A*N, B))

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 9 + 7

def P(m, n):
    num = 1
    for i in range(n):
        num = (num*(m-i)) % INF
    return num


n, k = list(map(int, sys.stdin.readline().split()))
graph = [dict() for i in range(n)]
for i in range(n):
    graph[i]["eda"]=[]
for i in range(n - 1):
    a, b = list(map(int, sys.stdin.readline().split()))
    graph[a - 1]["eda"].append(b - 1)
    graph[b - 1]["eda"].append(a - 1)
ans = 1
check = [False] * n
stack = [0]
ans = 1
while len(stack) > 0:
    x = stack.pop()
    check[x] = True
    count = 0
    for i in graph[x]["eda"]:
        if not check[i]:
            count += 1
            stack.append(i)
    if x == 0:
        temp = P(k - 1, count)
        ans = (ans * temp) % INF
    else:
        temp = P(k - 2, count)
        ans = (ans * temp) % INF

print((ans * k) % INF)

Buggy submission (verdict: Runtime Error):
import sys
import math

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 9 + 7

def P(n, r):
    num = math.factorial(n) // math.factorial(n - r)
    return num



n, k = list(map(int, sys.stdin.readline().split()))
graph = [dict() for i in range(n)]
for i in range(n):
    graph[i]["eda"]=[]
for i in range(n - 1):
    a, b = list(map(int, sys.stdin.readline().split()))
    graph[a - 1]["eda"].append(b - 1)
    graph[b - 1]["eda"].append(a - 1)
ans = 1
check = [False] * n
stack = [0]
ans = 1
while len(stack) > 0:
    x = stack.pop()
    check[x] = True
    count = 0
    for i in graph[x]["eda"]:
        if not check[i]:
            count += 1
            stack.append(i)
    if x == 0:
        temp = P(k - 1, count)
        ans = (ans * temp) % INF
    else:
        temp = P(k - 2, count)
        ans = (ans * temp) % INF

print((ans * k) % INF)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
 
N, K = list(map(int, input().split()))
ab = []
for i in range(N-1):
    ab.append(list(map(int, input().split())))
 
ki = []
 
for i in range(N):
    ki.append(dict())
    ki[-1]["eda"] = []
 
for i in range(N-1):
    ki[ab[i][0] - 1]["eda"].append([ab[i][0] - 1, ab[i][1] - 1])
    ki[ab[i][1] - 1]["eda"].append([ab[i][1] - 1, ab[i][0] - 1])
 
def P(m ,n):
    if m <= 0 or n <= 0:
        return 0
    else:
        num = 1
        for i in range(n):
            num *= m-i
        return num
 
def saiki(i, base, ki):
    num = 1
    eda_num = len(ki[i]["eda"])-1
    if eda_num == 0:
        return num
    num *= P(K-2, eda_num)
 
    for edge in ki[i]["eda"]:
        if edge[1] != base:
            num *= saiki(edge[1], i, ki)
            num = num % 1000000007
            #print(num)
 
    return num
 
total_num = K
if len(ki[0]["eda"]) != 0:
    total_num *= P(K-1, len(ki[0]["eda"]))
    for edge in ki[0]["eda"]:
        total_num *= saiki(edge[1], 0, ki)
        total_num = total_num % 1000000007
        if total_num == 0:
            break
 
print(total_num)