Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n, k = map(int, raw_input().split())
A = [int(raw_input())-k for i in xrange(n)]
su = 0
v = [(0, 0)]
for i in xrange(n):
    su += A[i]
    v.append((su, i+1))
v.sort()

class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0]*n
        self.el = [0]*n
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s
    def add(self, i, x):
        self.el[i] += x
        while i <= self.n:
            self.data[i] += x
            i += i & -i
    def get(self, i):
        return self.el[i]

ans = 0
bit = BIT(n+2)
for e, i in v:
    ans += bit.sum(i+1)
    bit.add(i+1, 1)
print ans

Fixed submission:
n, k = map(int, raw_input().split())
A = [int(raw_input())-k for i in xrange(n)]
su = 0
v = [(0, 0)]
for i in xrange(n):
    su += A[i]
    v.append((su, i+1))
v.sort()

data = [0]*(n+5)
def dsum(i):
    s = 0
    while i > 0:
        s += data[i]
        i -= i & -i
    return s
def add(i, x):
    while i <= n+2:
        data[i] += x
        i += i & -i
ans = 0
for e, i in v:
    ans += dsum(i+1)
    add(i+1, 1)
print ans

Buggy python submission (verdict: Runtime Error):
n, k = map(int, raw_input().split())
A = [int(raw_input())-k for i in xrange(n)]
su = 0
v = [(0, 0)]
for i in xrange(n):
    su += A[i]
    v.append((su, i+1))
v.sort()

data = [0]*(n+2)
def sum(i):
    s = 0
    while i > 0:
        s += data[i]
        i -= i & -i
    return s
def add(i, x):
    while i <= n+2:
        data[i] += x
        i += i & -i
ans = 0
tmp = 0
for e, i in v:
    ans += sum(i+1)
    add(i+1, 1)
print ans


Fixed submission:
n, k = map(int, raw_input().split())
A = [int(raw_input())-k for i in xrange(n)]
su = 0
v = [(0, 0)]
for i in xrange(n):
    su += A[i]
    v.append((su, i+1))
v.sort()

data = [0]*(n+5)
def dsum(i):
    s = 0
    while i > 0:
        s += data[i]
        i -= i & -i
    return s
def add(i, x):
    while i <= n+2:
        data[i] += x
        i += i & -i
ans = 0
for e, i in v:
    ans += dsum(i+1)
    add(i+1, 1)
print ans

Buggy python submission (verdict: Runtime Error):
n, k = map(int, raw_input().split())
A = [int(raw_input())-k for i in xrange(n)]
su = 0
v = [(0, 0)]
for i in xrange(n):
    su += A[i]
    v.append((su, i+1))
v.sort()

data = [0]*(n+2)
def sum(i):
    s = 0
    while i > 0:
        s += data[i]
        i -= i & -i
    return s
def add(i, x):
    while i <= n+2:
        data[i] += x
        i += i & -i
ans = 0
tmp = 0
for e, i in v:
    ans += sum(i+1)
    add(i+1, 1)
print ans


Fixed submission:
n, k = map(int, raw_input().split())
A = [int(raw_input())-k for i in xrange(n)]
su = 0
v = [(0, 0)]
for i in xrange(n):
    su += A[i]
    v.append((su, i+1))
v.sort()

data = [0]*(n+5)
def dsum(i):
    s = 0
    while i > 0:
        s += data[i]
        i -= i & -i
    return s
def add(i, x):
    while i <= n+2:
        data[i] += x
        i += i & -i
ans = 0
for e, i in v:
    ans += dsum(i+1)
    add(i+1, 1)
print ans
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop
n = input()
A = map(int, raw_input().split())
left = [None]*(3*n+1)
right = [None]*(3*n+1)
lsum = rsum = 0
que = []
for i in xrange(3*n):
    lsum += A[i]
    heappush(que, A[i])
    if len(que) > n:
        v = heappop(que)
        lsum -= v
    if len(que) == n:
        left[i] = lsum
que = []
for i in xrange(3*n-1, -1, -1):
    rsum += A[i]
    heappush(que, -A[i])
    if len(que) > n:
        v = heappop(que)
        rsum += v
    if len(que) == n:
        right[i] = rsum
ans = -10**18
for i in xrange(3*n):
    if left[i] is not None and right[i+1] is not None:
        ans = max(ans, left[i] - right[i+1])
print ans


Fixed submission:
from heapq import heappush, heappop
n = input()
A = map(int, raw_input().split())
left = [None]*(3*n+1)
right = [None]*(3*n+1)
lsum = rsum = 0
que = []
for i in xrange(3*n):
    lsum += A[i]
    heappush(que, A[i])
    if len(que) > n:
        v = heappop(que)
        lsum -= v
    if len(que) == n:
        left[i] = lsum
que = []
for i in xrange(3*n-1, -1, -1):
    rsum += A[i]
    heappush(que, -A[i])
    if len(que) > n:
        v = heappop(que)
        rsum += v
    if len(que) == n:
        right[i] = rsum
ans = -10**18
for i in xrange(3*n):
    if left[i] is not None and right[i+1] is not None:
        ans = max(ans, left[i] - right[i+1])
print ans
Buggy python submission (verdict: Runtime Error):
n = input()
P = []
mx = {}
my = {}
for i in xrange(n):
    x, y = map(int, raw_input().split())
    if x > y: x, y = y, x
    mx[x] = i
    my[y] = i
    P.append((x, y))

kx = sorted(mx)
mi_x = min(mx); ma_x = max(mx)
ky = sorted(my)
mi_y = min(my); ma_y = max(my)

if ma_x < mi_y or ma_y < mi_x:
    print (ma_x - mi_x) * (ma_y - mi_y)
    exit(0)

ans = (ma_x - mi_x) * (ma_y - mi_y)
xm = ()
for i in xrange(n):
    i = mx[kx[i]]
    if xm <= kx[i]:
        break
    y = P[i][1]
    if y <= min(ma_x, ma_y):
        xm = min(xm, y)
    else:
        break
ans = min((ma_x - xm) * (ma_y - kx[0]), ans)

ym = 0
for i in xrange(n-1, -1, -1):
    i = my[ky[i]]
    if ky[i] <= ym:
        break
    x = P[i][0]
    if max(mi_x, mi_y) <= x:
        ym = max(ym, x)
    else:
        break
ans = min((ky[-1] - mi_x) * (ym - mi_y), ans)
print ans


Fixed submission:
# seishin.py
from heapq import heappush, heappop, heapify

N = int(input())
P = []
for i in range(N):
    x, y = map(int, input().split())
    P.append((x, y) if x < y else (y, x))

X = [x for x, y in P]
Y = [y for x, y in P]

MIN = min(X)
MAX = max(Y)

# R_min = MIN, B_max = MAX
res = (max(X) - MIN) * (MAX - min(Y))

# R_min = MIN, R_max = MAX
base = MAX - MIN
heapify(P)

# 出来る限りスライドさせながら(B_max - B_min)を最小化
r = max(X)
while P:
    x, y = heappop(P)
    res = min(res, base * (r - x))
    if y != -1:
        # Bに入ってたxをRに入れて代わりにyをBに入れる
        heappush(P, (y, -1))
        r = max(r, y)
    else:
        # (x, y) のyの方がB_minに該当
        # => これ以上B_minを落とせない
        break

print(res)

Buggy python submission (verdict: Runtime Error):
n = input()
P = []
mx = {}
my = {}
for i in xrange(n):
    x, y = map(int, raw_input().split())
    if x > y: x, y = y, x
    mx[x] = i
    my[y] = i
    P.append((x, y))

kx = sorted(mx)
mi_x = min(mx); ma_x = max(mx)
ky = sorted(my)
mi_y = min(my); ma_y = max(my)

if ma_x < mi_y or ma_y < mi_x:
    print (ma_x - mi_x) * (ma_y - mi_y)
    exit(0)

ans = (ma_x - mi_x) * (ma_y - mi_y)
xm = ()
for i in xrange(n):
    i = mx[kx[i]]
    if xm <= kx[i]:
        break
    y = P[i][1]
    if y <= min(ma_x, ma_y):
        xm = min(xm, y)
    else:
        break
ans = min((ma_x - xm) * (ma_y - kx[0]), ans)

ym = 0
for i in xrange(n-1, -1, -1):
    i = my[ky[i]]
    if ky[i] <= ym:
        break
    x = P[i][0]
    if max(mi_x, mi_y) <= x:
        ym = max(ym, x)
    else:
        break
ans = min((ky[-1] - mi_x) * (ym - mi_y), ans)


Fixed submission:
# seishin.py
from heapq import heappush, heappop, heapify

N = int(input())
P = []
for i in range(N):
    x, y = map(int, input().split())
    P.append((x, y) if x < y else (y, x))

X = [x for x, y in P]
Y = [y for x, y in P]

MIN = min(X)
MAX = max(Y)

# R_min = MIN, B_max = MAX
res = (max(X) - MIN) * (MAX - min(Y))

# R_min = MIN, R_max = MAX
base = MAX - MIN
heapify(P)

# 出来る限りスライドさせながら(B_max - B_min)を最小化
r = max(X)
while P:
    x, y = heappop(P)
    res = min(res, base * (r - x))
    if y != -1:
        # Bに入ってたxをRに入れて代わりにyをBに入れる
        heappush(P, (y, -1))
        r = max(r, y)
    else:
        # (x, y) のyの方がB_minに該当
        # => これ以上B_minを落とせない
        break

print(res)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

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

used = [0]*N
Q1 = []
Q2 = []
def dfs(v, Q):
    if used[v]:
        return 0
    used[v] = 1
    Q.append(v+1)
    for w in G[v]:
        if dfs(w, Q):
            return 1
    return 1

if len(G[0]) == 1:
    dfs(0, Q1)
    print(len(Q1))
    print(*Q1)
else:
    used[0] = 1
    for v in G[0]:
        if dfs(v, Q1):
            break
    for v in G[0]:
        if dfs(v, Q2):
            break
    print(len(Q1) + len(Q2) + 1)
    print(*Q1[::-1], 1, *Q2)


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

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

used = [0]*N
Q1 = []
Q2 = []
def dfs(v, Q):
    if used[v]:
        return 0
    used[v] = 1
    Q.append(v+1)
    for w in G[v]:
        if dfs(w, Q):
            return 1
    return 1

if len(G[0]) == 1:
    dfs(0, Q1)
    print(len(Q1))
    print(*Q1)
else:
    used[0] = 1
    for v in G[0]:
        if dfs(v, Q1):
            break
    for v in G[0]:
        if dfs(v, Q2):
            break
    print(len(Q1) + len(Q2) + 1)
    print(*(Q1[::-1]+[1]+Q2))
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

dp = [[-1]*11 for i in range(N)]
mark = {}
Q = int(input())
col = [0]*(Q+1)
col[-1] = 0
C = []
for i in range(Q):
    v, d, c = map(int, input().split())
    col[i] = c
    C.append((v-1, d))

def dfs(v, d, idx):
    if idx < dp[v][d]:
        return
    dp[v][d] = idx
    if d:
        for w in G[v]:
            dfs(w, d-1, idx)

for i in range(Q-1, -1, -1):
    v, d = C[i]
    dfs(v, d, i)
print(*(col[max(e for e in dp[i])] for i in range(N)), sep='\n')


Fixed submission:
N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

dp = [{-1: -1} for i in range(N)]
mark = {}
Q = int(input())
col = [0]*(Q+1)
col[-1] = 0
C = []
for i in range(Q):
    v, d, c = map(int, input().split())
    col[i] = c
    C.append((v-1, d))

def dfs(v, d, idx):
    if d <= max(dp[v]):
        return
    dp[v][d] = idx
    if d:
        for w in G[v]:
            dfs(w, d-1, idx)

for i in range(Q-1, -1, -1):
    v, d = C[i]
    dfs(v, d, i)
print(*(col[max(e.values())] for e in dp), sep='\n')

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

dp = [[-1]*10 for i in range(N)]
mark = {}
Q = int(input())
col = [0]*(Q+1)
col[-1] = 0
C = []
for i in range(Q):
    v, d, c = map(int, input().split())
    col[i] = c
    C.append((v-1, d))

def dfs(v, d, idx):
    if idx < dp[v][d]:
        return
    dp[v][d] = idx
    if d:
        for w in G[v]:
            dfs(w, d-1, idx)

for i in range(Q-1, -1, -1):
    v, d = C[i]
    dfs(v, d, i)
print(*(col[max(e for e in dp[i])] for i in range(N)), sep='\n')

Fixed submission:
N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

dp = [{-1: -1} for i in range(N)]
mark = {}
Q = int(input())
col = [0]*(Q+1)
col[-1] = 0
C = []
for i in range(Q):
    v, d, c = map(int, input().split())
    col[i] = c
    C.append((v-1, d))

def dfs(v, d, idx):
    if d <= max(dp[v]):
        return
    dp[v][d] = idx
    if d:
        for w in G[v]:
            dfs(w, d-1, idx)

for i in range(Q-1, -1, -1):
    v, d = C[i]
    dfs(v, d, i)
print(*(col[max(e.values())] for e in dp), sep='\n')
Buggy python submission (verdict: Runtime Error):
n, m = map(int, raw_input().split())
P = [map(int, raw_input().split()) for i in xrange(n)]
C = [map(int, raw_input().split()) for i in xrange(n)]
for a, b in P:
    idx = -1; mi = ()
    for i, (c, d) in enumerate(C):
        di = abs(a-c) + abs(b-d)
        if di < mi:
            mi = di
            idx = i
    print idx+1

Fixed submission:
n, m = map(int, raw_input().split())
P = [map(int, raw_input().split()) for i in xrange(n)]
C = [map(int, raw_input().split()) for i in xrange(m)]
for a, b in P:
    idx = -1; mi = ()
    for i, (c, d) in enumerate(C):
        di = abs(a-c) + abs(b-d)
        if di < mi:
            mi = di
            idx = i
    print idx+1
Buggy python submission (verdict: Runtime Error):
n = input()
X = map(int, raw_input().split())

if n > 10:
    exit(1)

cur = 1
for i in xrange(n):
    if cur <= X[i]:
        X[i] = cur
        cur += 2
    else:
        cur = X[i]+2

ALL = 2**n-1
memo = {}
def dfs(state):
    if state in memo:
        return memo[state]
    if state == ALL:
        return 1
    Y = X[:]
    cur = 1
    for i in xrange(n):
        if (state >> i) & 1:
            continue
        if cur <= Y[i]:
            Y[i] = cur
            cur += 2
        else:
            cur = Y[i]+2
    res = 0
    prev = -100
    for i in xrange(n):
        if (state >> i) & 1:
            continue
        res = (res + dfs(state | (1 << i))) % (10**9 + 7)
        if prev+1 == Y[i]:
            break
        prev = Y[i]
    memo[state] = res
    return res
print dfs(0)


Fixed submission:
n = input()
X = map(int, raw_input().split())

MOD = 10**9+7

ans = 1
res = 0
for i in xrange(n):
    while 2*res+1 > X[i]:
        ans = (ans * (res+1)) % MOD
        res -= 1
    res += 1
while res:
    ans = (ans * res) % MOD
    res -= 1
print ans
Buggy python submission (verdict: Time Limit Exceeded):
n, ma, mb = map(int, raw_input().split())
N = 403
memo = [[10**18]*(N+20) for i in xrange(N+20)]
memo[0][0] = 0
for t in xrange(n):
    a, b, c = map(int, raw_input().split())
    for i in xrange(N, -1, -1):
        for j in xrange(N, -1, -1):
            memo[i+a][j+b] = min(memo[i+a][j+b], memo[i][j] + c)
ans = 10**18
a = ma; b = mb
while a < N and b < N:
    ans = min(ans, memo[a][b])
    a += ma; b += mb
print -1 if ans > 50000 else ans

Fixed submission:
from heapq import heappush, heappop
n, ma, mb = map(int, raw_input().split())
dp = {(0, 0): 0}
INF = 10**18
for i in xrange(n):
    a, b, c = map(int, raw_input().split())
    for x, y in sorted(dp.keys())[::-1]:
        dp[x+a, y+b] = min(dp.get((x+a, y+b), INF), dp[x, y] + c)
ans = INF
for x, y in dp:
    if x*mb == y*ma > 0:
        ans = min(ans, dp[x, y])
print -1 if ans==INF else ans
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
N = int(input())
*A, = map(int, input().split())
G = [[] for i in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

def calc(S):
    if not S:
        return 0
    S.sort(reverse=1)
    if S[0] > sum(S[1:]):
        return sum(S[1:])
    return sum(S)//2
ok = 1
def dfs(v, p):
    global ok
    c = A[v]
    S = []
    for w in G[v]:
        if w == p:
            continue
        S.append(dfs(w, v))
    if not S:
        return c
    d = calc(S)
    s = sum(S)
    k = s - c
    if not 0 <= k <= d:
        ok = 0
    return s - 2*k
import random
random.seed()
*r, = range(N)
random.shuffle(r)
d = {}
for i in range(min(3, N)):
    dfs(r[i], -1)

if ok:
    print('YES')
else:
    print('NO')

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
N = int(input())
*A, = map(int, input().split())
G = [[] for i in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

def calc(S):
    if not S:
        return 0
    S.sort(reverse=1)
    if S[0] > sum(S[1:]):
        return sum(S[1:])
    return sum(S)//2
ok = 1
def dfs(v, p):
    global ok
    c = A[v]
    S = []
    for w in G[v]:
        if w == p:
            continue
        S.append(dfs(w, v))
    if not S:
        return c
    d = calc(S)
    s = sum(S)
    k = s - c
    if not 0 <= k <= d:
        ok = 0
    return s - 2*k
import random
random.seed()
*r, = range(N)
random.shuffle(r)
d = {}
for i in range(2):
    dfs(r[i], -1)

if ok:
    print('YES')
else:
    print('NO')
Buggy python submission (verdict: Time Limit Exceeded):
# seishin.py
N, M = map(int, input().split())
D = [[] for i in range(M+1)]
for i in range(N):
    l, r = map(int, input().split())
    D[r-l+1].append(l)

data = [0]*(M+2)
def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
def add(k, x):
    while k <= M+1:
        data[k] += x
        k += k & -k

C = N
ans = []
for d in range(1, M+1):
    for l in D[d]:
        add(l, 1)
        add(l+d, -1)
        C -= 1

    cnt = C
    i = d
    while i <= M:
        cnt += get(i)
        i += d
    ans.append(cnt)
print(*ans, sep='\n')


Fixed submission:
# seishin.py
N, M = map(int, input().split())
D = [[] for i in range(M+1)]
for i in range(N):
    l, r = map(int, input().split())
    D[r-l+1].append(l)

data = [0]*(M+2)
def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
def add(k, x):
    while k <= M+1:
        data[k] += x
        k += k & -k

C = N
ans = []
for d in range(1, M+1):
    for l in D[d]:
        add(l, 1)
        add(l+d, -1)
        C -= 1

    cnt = C
    i = d
    while i <= M:
        cnt += get(i)
        i += d
    ans.append(cnt)
print(*ans, sep='\n')

Buggy python submission (verdict: Runtime Error):
from math import sqrt
N, M = map(int, input().split())
D = [0]*(M+2); R = [0]*(M)
for i in range(N):
    l, r = map(int, input().split())
    D[1] += 1
    D[r+1] -= 1
    if l <= r-l+1:
        continue
    D[r-l+2] -= 1
    D[l] += 1

    left = r-l+2; right = l
    for k in range(2, int(sqrt(r))+1):
        l0 = max((l+k-1) // k, left); r0 = min((r // k + 1, right))
        if l0 < r0:
            D[l0] += 1
            D[r0] -= 1
        right = l0

print(*R, *D)
tmp = 0
for i in range(1, M+1):
    tmp += D[i]
    R[i-1] += tmp
print(*R, sep='\n')

Fixed submission:
# seishin.py
N, M = map(int, input().split())
D = [[] for i in range(M+1)]
for i in range(N):
    l, r = map(int, input().split())
    D[r-l+1].append(l)

data = [0]*(M+2)
def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
def add(k, x):
    while k <= M+1:
        data[k] += x
        k += k & -k

C = N
ans = []
for d in range(1, M+1):
    for l in D[d]:
        add(l, 1)
        add(l+d, -1)
        C -= 1

    cnt = C
    i = d
    while i <= M:
        cnt += get(i)
        i += d
    ans.append(cnt)
print(*ans, sep='\n')
Buggy python submission (verdict: Runtime Error):
n = input()
A = {}
for i in xrange(2, n+1):
    v = int(raw_input())
    A.setdefault(v, []).append(i)
f = lambda v: (v in A)/1 and 1+max(e+i for i,e in enumerate(sorted(map(f,A[v]))[::-1]))
print f(1)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n = input()
A = {}
for i in xrange(2, n+1):
    v = int(raw_input())
    A.setdefault(v, []).append(i)
f = lambda v: (v in A)/1 and 1+max(e+i for i,e in enumerate(sorted(map(f,A[v]))[::-1]))
print f(1)
Buggy python submission (verdict: Runtime Error):
print"vowel"*(c in"aiueo")or"consonant"

Fixed submission:
print("vowel"*(input()in"aiueo")or"consonant")
Buggy python submission (verdict: Runtime Error):
n, e, t = map(int, raw_input().split())
X = map(int, raw_input().split())

if n > 2001:
    exit(1)
ans = 0
bef = 0
bef_i = 0

d = [[0 for i in xrange(n)] for j in xrange(n)]
for i in xrange(n):
    el = d[i]
    xi = X[i]
    for j in xrange(i, n):
        el[j] = max(2*(X[j] - xi), t) + X[j] - xi
for i in xrange(n):
    for j in xrange(i):
        d[0][i] = min(d[0][j]+d[j+1][i]+(X[j+1]-X[j]), d[0][i])
print d[0][n-1]+ e - X[-1] + X[0]


Fixed submission:
h, w = map(int, raw_input().split())
A = [list(raw_input()) for i in xrange(h)]

x = y = 0
A[0][0] = '.'
while 1:
    if y+1 < h and A[y+1][x] == '#':
        A[y+1][x] = '.'
        y += 1
    elif x+1 < w and A[y][x+1] == '#':
        A[y][x+1] = '.'
        x += 1
    else: break
if y == h-1 and x == w-1 and all(all(f is '.' for f in e) for e in A):
    print "Possible"
else:
    print "Impossible"
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N = int(input())
C = []
M = {}
for i in range(N):
    *c, = map(int, input().split())
    c = tuple(min(c[j:] + c[:j] for j in range(1, 5)))
    C.append(c)
    if c not in M:
        M[c] = deque([i])
    else:
        M[c].append(i)
def count(p, q, r, s):
    if p == q == r == s:
        return 4
    if p == r and q == s:
        return 2
    return 1
def solve(ci, cj, k):
    R = {}
    for l in range(4):
        #  [l]   [l-1]
        # [l+k] [l+k+1]
        c = ci[l], ci[l-1], cj[k-l], cj[k-l-1]
        c = tuple(min(c[j:] + c[:j] for j in range(1, 5)))
        if c not in M:
            return 0
        R[c] = R.get(c, 0) + 1
    res = 1
    for c in R:
        m = M[c]
        cnt = len(m)
        if c == cj:
            cnt -= 1
        if cnt < R[c]:
            return 0
        k = count(*c)
        for p in range(cnt-R[c]+1, cnt+1):
            res *= p * k
    return res

ans = 0
for i in range(N):
    ci = C[i]
    q = M[ci]; q.popleft()
    if not q:
        del M[ci]
    for j in range(i+1, N):
        cj = C[j]
        for k in range(4):
            ans += solve(ci, cj, k)
print(ans)


Fixed submission:
from collections import deque
N = int(input())
C = []
M = {}
for i in range(N):
    *c, = map(int, input().split())
    c = tuple(min(c[j:] + c[:j] for j in range(1, 5)))
    C.append(c)
    if c not in M:
        M[c] = deque([i])
    else:
        M[c].append(i)
def count(p, q, r, s):
    if p == q == r == s:
        return 4
    if p == r and q == s:
        return 2
    return 1
def solve(ci, cj, k):
    R = {}
    for l in range(4):
        #  [l]   [l-1]
        # [l+k] [l+k+1]
        c = ci[l], ci[l-1], cj[k-l], cj[k-l-1]
        c = tuple(min(c[j:] + c[:j] for j in range(1, 5)))
        if c not in M:
            return 0
        R[c] = R.get(c, 0) + 1
    res = 1
    for c in R:
        m = M[c]
        cnt = len(m)
        if c == cj:
            cnt -= 1
        if cnt < R[c]:
            return 0
        k = count(*c)
        for p in range(cnt-R[c]+1, cnt+1):
            res *= p * k
    return res

ans = 0
for i in range(N):
    ci = C[i]
    q = M[ci]; q.popleft()
    if not q:
        del M[ci]
    for j in range(i+1, N):
        cj = C[j]
        for k in range(4):
            ans += solve(ci, cj, k)
print(ans)
Buggy python submission (verdict: Runtime Error):
S={c:list(input())for c in"abc"}
s="a"
while S[s]:s=S[s].pop(0)
print(s.upper())

Fixed submission:
print(input()+input())*input()/2
Buggy python submission (verdict: Runtime Error):
# seishin.py
from math import sqrt
n, s = map(int, open(0).read().split())

# s = n
if s == n:
    print(n+1)
    exit(0)

def f(b, n):
    res = 0
    while n:
        res += n % b
        n //= b
    return res

# 2 <= b <= sqrt(n)
for b in range(2, int(sqrt(n))+1):
    if f(b, n) == s:
        print(b)
        exit(0)

# sqrt(n) <= b <= n
for p in range(int(sqrt(n)), 0, -1):
    if (n - s) % p > 0:
        continue
    b = (n - s) // p + 1
    if f(b, n) == s:
        print(b)
        exit(0)
print(-1)

Fixed submission:
# seishin.py
from math import sqrt
n, s = map(int, open(0).read().split())

# s = n
if s == n:
    print(n+1)
    exit(0)

def f(b, n):
    res = 0
    while n:
        res += n % b
        n //= b
    return res

# 2 <= b <= sqrt(n)
for b in range(2, int(sqrt(n))+1):
    if f(b, n) == s:
        print(b)
        exit(0)

# sqrt(n) <= b <= n
for p in range(int(sqrt(n)), 0, -1):
    if (n - s) % p > 0:
        continue
    b = (n - s) // p + 1
    if b*b <= n:
        continue
    if f(b, n) == s:
        print(b)
        exit(0)
print(-1)