Problem p02985 (python)

Fixed submission:
import numpy as np
from collections import deque

N,K = (int(i) for i in input().split())  
AB = [[int(i) for i in input().split()] for i in range(N-1)] 

Tree = [[] for _ in range(N+1)]
d = deque()

for i in range(N-1):
    a, b = AB[i]
    Tree[a].append(b)
    Tree[b].append(a)

answer = K

for i in range(len(Tree[1])):
    answer = (answer * (K - 1 - i))%(int(10**9)+7)
    d.append([1, Tree[1][i]])

Tree[1] = []

while d != deque():
    for i in range(len(d)):
        parent, child = d[0]
        Tree[child].pop(Tree[child].index(parent))
        for j in range(len(Tree[child])):
            d.append([child, Tree[child][j]])
            answer = (answer * (K - 2 - j))%(int(10**9)+7)
        d.popleft()
print(answer)

Buggy submission (verdict: Runtime Error):
import numpy as np
N,K = (int(i) for i in input().split())  
AB = [[int(i) for i in input().split()] for i in range(N-1)] 

Tree = [[] for _ in range(N+1)]
d = []

for i in range(N-1):
    a, b = AB[i]
    Tree[a].append(b)
    Tree[b].append(a)

answer = K

for i in range(len(Tree[1])):
    answer = (answer * (K - 1 - i))%(int(10**9)+7)
    d.append([1, Tree[1][i]])

Tree[1] = []

while d != []:
    for i in range(len(d)):
        parent, child = d[0]
        Tree[child].pop(Tree[child].index(parent))
        for j in range(len(Tree[child])):
            d.append([child, Tree[child][j]])
            answer = (answer * (K - 2 - j))%(int(10**9)+7)
        d.pop(0)
        Tree[child] = []
print(answer)

Buggy submission (verdict: Runtime Error):
import numpy as np
N,K = (int(i) for i in input().split())  
AB = [[int(i) for i in input().split()] for i in range(N-1)] 

j = 0
point = AB[0][0]
answer = K

d = []

count = 1
for i in range(len(AB)):
    if point in AB[j]:
        AB[j].pop(AB[j].index(point))
        d.append([AB[j][0], count])
        AB.pop(j)
        count += 1
    else:
        j += 1
point = d[0][0]
answer = answer * (K-d[0][1]) % (int(10**9)+7)
d.pop(0)

while len(AB) > 0:
    j = 0
    count = 2
    for i in range(len(AB)):
        if point in AB[j]:
            AB[j].pop(AB[j].index(point))
            d.append([AB[j][0], count])
            AB.pop(j)
            count += 1
        else:
            j += 1
    point = d[0][0]
    answer = answer * (K-d[0][1]) % (int(10**9)+7)
    d.pop(0)
print(answer)

Buggy submission (verdict: Runtime Error):
import numpy as np
N,K = (int(i) for i in input().split())  
AB = [[int(i) for i in input().split()] for i in range(N-1)] 

j = 0
point = AB[0][0]
answer = K

d = []

count = 1
for i in range(len(AB)):
    if point in AB[j]:
        AB[j].pop(AB[j].index(point))
        d.append([AB[j][0], count])
        AB.pop(j)
        count += 1
    else:
        j += 1
point = d[0][0]
answer = answer * (K-d[0][1]) % (int(10**9)+7)
d.pop(0)

while len(AB) > 0:
    j = 0
    count = 2
    for i in range(len(AB)):
        if point in AB[j]:
            AB[j].pop(AB[j].index(point))
            d.append([AB[j][0], count])
            AB.pop(j)
            count += 1
        else:
            j += 1
        stop += 1
    point = d[0][0]
    answer = answer * (K-d[0][1]) % (int(10**9)+7)
    d.pop(0)
print(answer)

Buggy submission (verdict: Runtime Error):
import numpy as np
N,K = (int(i) for i in input().split())  
AB = [[int(i) for i in input().split()] for i in range(N-1)] 

j = 0
point = AB[0][0]
answer = K

d = []

count = 1
for i in range(len(AB)):
    if point in AB[j]:
        AB[j].pop(AB[j].index(point))
        d.append([AB[j][0], count])
        AB.pop(j)
        count += 1
    else:
        j += 1
point = d[0][0]
answer = answer * (K-d[0][1]) % (int(10**9)+7)
d.pop(0)

while len(AB) > 0:
    j = 0
    count = 2
    for i in range(len(AB)):
        if point in AB[j]:
            AB[j].pop(AB[j].index(point))
            d.append([AB[j][0], count])
            AB.pop(j)
            count += 1
        else:
            j += 1
        stop += 1
    point = d[0][0]
    answer = answer * (K-d[0][1]) % (int(10**9)+7)
    d.pop(0)
print(answer)

Buggy submission (verdict: Runtime Error):
import numpy as np
N,K = (int(i) for i in input().split())  
AB = [[int(i) for i in input().split()] for i in range(N-1)] 

j = 0
point = AB[0][0]
answer = K

d = []

count = 1
for i in range(len(AB)):
    if point in AB[j]:
        AB[j].pop(AB[j].index(point))
        d.append([AB[j][0], count])
        AB.pop(j)
        count += 1
    else:
        j += 1
point = d[0][0]
answer = answer * (K-d[0][1]) % (int(10**9)+7)
d.pop(0)

while len(AB) > 0 and stop:
    j = 0
    count = 2
    for i in range(len(AB)):
        if point in AB[j]:
            AB[j].pop(AB[j].index(point))
            d.append([AB[j][0], count])
            AB.pop(j)
            count += 1
        else:
            j += 1
        stop += 1
    point = d[0][0]
    answer = answer * (K-d[0][1]) % (int(10**9)+7)
    d.pop(0)
print(answer)