Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import permutations as perm
from bisect import bisect_left as bisect
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    # greatest common divisor
    while b > 0:
        a, b = b, a % b

    return a


def solve():
    def get_ans(c1, c2, c3):
        ret = 0
        lenc3 = len(RGB[c3])
        for c1i in range(len(RGB[c1])):
            tmp = bisect(RGB[c2], RGB[c1][c1i])
            for c2i in range(tmp, len(RGB[c2])):
                c3i = bisect(RGB[c3], RGB[c2][c2i])
                if lenc3 == c3i:
                    continue
                # print(c1i, c2i, c3i)
                c21d = RGB[c2][c2i] - RGB[c1][c1i]
                t = bisect(RGB[c3], RGB[c2][c2i] + c21d, c3i)

                if lenc3 != t and RGB[c3][t] - RGB[c2][c2i] == c21d:
                    ret += lenc3 - c3i - 1
                else:
                    ret += lenc3 - c3i

        return ret
    int(input())
    S = list(input().strip())
    RGB = {k: [] for k in "RGB"}
    for i, s in enumerate(S):
        RGB[s].append(i)

    ans = 0
    # print(RGB)
    for c1, c2, c3 in perm(list("RGB")):
        # print(c1, c2, c3)
        ans += get_ans(c1, c2, c3)
        # print(ans)
    print(ans)


solve()


Fixed submission:
import sys
from itertools import permutations as perm
from bisect import bisect_left as bisect
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    # greatest common divisor
    while b > 0:
        a, b = b, a % b

    return a


def solve():
    def get_ans(c1, c2, c3):
        ret = 0
        lenc3 = len(RGB[c3])
        for c1i in range(len(RGB[c1])):
            tmp = bisect(RGB[c2], RGB[c1][c1i])
            for c2i in range(tmp, len(RGB[c2])):
                c3i = bisect(RGB[c3], RGB[c2][c2i])
                if lenc3 == c3i:
                    continue
                # print(c1i, c2i, c3i)
                c21d = RGB[c2][c2i] - RGB[c1][c1i]
                t = bisect(RGB[c3], RGB[c2][c2i] + c21d, c3i)

                if lenc3 != t and RGB[c3][t] - RGB[c2][c2i] == c21d:
                    ret += lenc3 - c3i - 1
                else:
                    ret += lenc3 - c3i

        return ret
    int(input())
    S = list(input().strip())
    RGB = {k: [] for k in "RGB"}
    for i, s in enumerate(S):
        RGB[s].append(i)

    ans = 0
    # print(RGB)
    for c1, c2, c3 in perm(list("RGB")):
        # print(c1, c2, c3)
        ans += get_ans(c1, c2, c3)
        # print(ans)
    print(ans)


solve()
Buggy python submission (verdict: Runtime Error):
def inpl():
    return list(map(int, input().split()))


HW = inpl()
HW.sort()
if HW[0] == 1:
    print(1)
    exit()
print(HW[0] * HW[1] // 2 + int(H[0] % 2 == 1 and H[1] % 2 == 1))


Fixed submission:
def inpl():
    return list(map(int, input().split()))


HW = inpl()
HW.sort()
if HW[0] == 1:
    print(1)
    exit()
print(HW[0] * HW[1] // 2 + int(HW[0] % 2 == 1 and HW[1] % 2 == 1))
Buggy python submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


def dfs(n, d):
    if depth[n] != -1:
        return
    depth[n] = d
    for nn in eAB[n]:
        dfs(nn, d + 1)
    return


N = int(input())
AB = [inpl() for i in range(N - 1)]

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

depth = [-1 for i in range(N)]

dfs(0, 0)

eo = [[], []]
for i in range(N):
    eo[depth[i] % 2].append(i)

ans = [-1 for i in range(N)]
if len(eo[0]) > N // 3 and len(eo[1]) > N // 3:
    for i in range(N // 3):
        ans[3 * i] = eo[0][i]
        ans[3 * i + 1] = eo[1][i]
    t_0 = i + 1
    t_1 = i + 1
elif len(eo[0]) <= N // 3:
    for i in range(len(eo[0])):
        ans[3 * i + 2] = eo[0][i]
    t_0 = len(eo[0])
    t_1 = 0
elif len(eo[1]) <= N // 3:
    for i in range(len(eo[1])):
        ans[3 * i + 2] = eo[1][i]
    t_0 = 0
    t_1 = len(eo[1])

for i in range(N):
    if ans[i] != -1:
        continue
    if t_0 < len(eo[0]):
        ans[i] = eo[0][t_0]
        t_0 += 1
    else:
        ans[i] = eo[1][t_1]
        t_1 += 1

print(" ".join(list(map(str, [a + 1 for a in ans]))))


Fixed submission:
import sys

sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


def dfs(n, d):
    if depth[n] != -1:
        return
    depth[n] = d
    for nn in eAB[n]:
        dfs(nn, d + 1)
    return


N = int(input())
AB = [inpl() for i in range(N - 1)]

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

depth = [-1 for i in range(N)]

dfs(0, 0)

eo = [[], []]
for i in range(N):
    eo[depth[i] % 2].append(i)

# print(eo)
ans = [-1 for i in range(N)]
if len(eo[0]) > N // 3 and len(eo[1]) > N // 3:
    for i in range(N // 3):
        ans[eo[0][i]] = 3 * i + 1
        ans[eo[1][i]] = 3 * i + 2
    t_0 = N // 3
    t_1 = N // 3
    if N % 3 > 0:
        ans[eo[0][N // 3]] = 3 * (N // 3) + 1
        t_0 += 1
    if N % 3 > 1:
        ans[eo[1][N // 3]] = 3 * (N // 3) + 2
        t_1 += 1
    # print(ans)
    for i, v in enumerate(eo[0][t_0:] + eo[1][t_1:]):
        ans[v] = 3 * (i + 1)
else:
    if len(eo[0]) <= N // 3:
        ni = 0
    else:
        ni = 1
    tmp = [True for i in range(N + 1)]
    for i in range(len(eo[ni])):
        ans[eo[ni][i]] = 3 * (i + 1)
        tmp[3 * (i + 1)] = False
    tmp = [i for i, t in enumerate(tmp) if t is True]
    j = 1
    for i in range(N):
        if ans[i] != -1:
            continue
        ans[i] = tmp[j]
        j += 1

print(" ".join(list(map(str, ans))))
Buggy python submission (verdict: Time Limit Exceeded):
def inpl():
    return list(map(int, input().split()))


H, N = inpl()
AB = [inpl() for i in range(N)]

# print(AB)
ans = [float("inf") for _ in range(H + 1)]
ans[0] = 0

for a, b in AB:
    # print(a, b)
    for i in range(H):
        ni = min(i + a, H)
        # print(i)
        ans[ni] = min(ans[ni], ans[i] + b)

print(ans[H])


Fixed submission:
import sys
import functools


def inpl():
    return list(map(int, input().split()))


@functools.lru_cache(maxsize=None)
def solve(h):
    if h <= 0:
        return 0

    ret_val = float("inf")
    for a, b in AB:
        val = b + solve(h - a)
        if val < ret_val:
            ret_val = val
        else:
            break
    return ret_val


sys.setrecursionlimit(100000)

H, N = inpl()
AB = [inpl() for i in range(N)]
AB.sort(key=lambda ab: (ab[0] / ab[1], -ab[0]), reverse=True)
# print(AB)
print(solve(H))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import functools


def inpl():
    return list(map(int, input().split()))


@functools.lru_cache(maxsize=None)
def solve(h):
    if h <= 0:
        return 0

    ret_val = float("inf")
    for a, b in AB:
        ret_val = min(ret_val, b + solve(h - a))
    return ret_val


sys.setrecursionlimit(100000)

H, N = inpl()
AB = [inpl() for i in range(N)]
AB.sort(key=lambda ab: (ab[0] / ab[1], -ab[0]), reverse=True)
# print(AB)
print(solve(H))


Fixed submission:
import sys
import functools


def inpl():
    return list(map(int, input().split()))


@functools.lru_cache(maxsize=None)
def solve(h):
    if h <= 0:
        return 0

    ret_val = float("inf")
    for a, b in AB:
        val = b + solve(h - a)
        if val < ret_val:
            ret_val = val
        else:
            break
    return ret_val


sys.setrecursionlimit(100000)

H, N = inpl()
AB = [inpl() for i in range(N)]
AB.sort(key=lambda ab: (ab[0] / ab[1], -ab[0]), reverse=True)
# print(AB)
print(solve(H))
Buggy python submission (verdict: Runtime Error):
print("Yes" if len(set(input.split()))==1 else "No")

Fixed submission:
print("Yes" if len(set(input().split()))==1 else "No")
Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
from functools import reduce


def inpl():
    return list(map(int, input().split()))


def lcm_base(x, y):
    return (x * y) // gcd(x, y)


def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def count2(i, t=0):
    if i % 2 == 0:
        return count2(i // 2, t + 1)
    else:
        return t


N, M = inpl()
A = inpl()
A = [a // 2 for a in A]

all_lcm = lcm_list(A)
c_2 = count2(A[0])
for a in A:
    if c_2 != count2(a):
        print(0)
        exit()

print(((M // all_lcm) + 1) // 2)


Fixed submission:
from fractions import gcd
from functools import reduce


def inpl():
    return list(map(int, input().split()))


def lcm_base(x, y):
    return (x * y) // gcd(x, y)


def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def count2(i):
    t = 2
    ret = 1
    while i % t == 0:
        t *= 2
        ret += 1
    return ret - 1


N, M = inpl()
A = inpl()
A = [a // 2 for a in A]

all_lcm = 1
c2 = count2(A[0])
for a in A:
    all_lcm = lcm(all_lcm, a)
    if count2(a) != c2:
        print(0)
        exit()

print(((M // all_lcm) + 1) // 2)
Buggy python submission (verdict: Runtime Error):
def inpl():
    return list(map(int, input().split()))


S = input()

p = re.compile(r'([^L][^R])*[^L]?$')

if p.match(S):
    print('Yes')
else:
    print('No')

Fixed submission:
def inpl():
    return list(map(int, input().split()))


S = input()

flag = True
for i in range(len(S)):
    if i % 2 == 0 and S[i] in 'RUD':
        continue
    elif i % 2 == 1 and S[i] in 'LUD':
        continue
    flag = False

print('Yes' if flag is True else 'No')
Buggy python submission (verdict: Runtime Error):
def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    # greatest common divisor
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


def lcm(a, b):
    # least common multiple
    return a * b // gcd(a, b)


R, G, B, N = inpl()
ans = 0
for r in range(N // R + 1):
    rest = N - r * R
    for i in range(B):
        if (G * i) % B == rest % B:
            x = i
            break
    else:
        # print(r)
        continue
    if rest - G * x < 0:
        continue
    ans += (rest - G * x) // lcm(B, G) + 1
    # print(r, rest, x, ans)

print(ans)


Fixed submission:
def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    # greatest common divisor
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


def lcm(a, b):
    # least common multiple
    return a * b // gcd(a, b)

import sys

sys.setrecursionlimit(5000)


R, G, B, N = inpl()
ans = 0
for r in range(N // R + 1):
    rest = N - r * R
    for i in range(B):
        if (G * i) % B == rest % B:
            x = i
            break
    else:
        # print(r)
        continue
    if rest - G * x < 0:
        continue
    ans += (rest - G * x) // lcm(B, G) + 1
    # print(r, rest, x, ans)

print(ans)

Buggy python submission (verdict: Runtime Error):
def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    # greatest common divisor
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


def lcm(a, b):
    # least common multiple
    return a * b // gcd(a, b)


R, G, B, N = inpl()
ans = 0
for r in range(N // R + 1):
    rest = N - r * R
    x = 0
    while (G * x) % B != rest % B:
        x += 1
    if rest - G * x < 0:
        continue
    ans += (rest - G * x) // lcm(B, G) + 1
    # print(r, rest, x, ans)

print(ans)


Fixed submission:
def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    # greatest common divisor
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


def lcm(a, b):
    # least common multiple
    return a * b // gcd(a, b)

import sys

sys.setrecursionlimit(5000)


R, G, B, N = inpl()
ans = 0
for r in range(N // R + 1):
    rest = N - r * R
    for i in range(B):
        if (G * i) % B == rest % B:
            x = i
            break
    else:
        # print(r)
        continue
    if rest - G * x < 0:
        continue
    ans += (rest - G * x) // lcm(B, G) + 1
    # print(r, rest, x, ans)

print(ans)
Buggy python submission (verdict: Runtime Error):
def inpl():
    return list(map(int, input().split()))


S = inpu()
print(min(S.count('0'), S.count('1')))

Fixed submission:
def inpl():
    return list(map(int, input().split()))


S = input()
print(min(S.count('0'), S.count('1')) * 2)
Buggy python submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(20000)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


n = int(input())
A = inpl()

ans = gcd(A[0], A[1])
for a in A:
    ans = gcd(ans, a)
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(20000)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


n = int(input())
A = inpl()
A.sort()
ans = gcd(A[0], A[1])
for a in A:
    ans = gcd(ans, a)
print(ans)

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

sys.setrecursionlimit(2000)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


n = int(input())
A = inpl()

ans = gcd(A[0], A[1])
for a in A:
    ans = gcd(ans, a)
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(20000)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


n = int(input())
A = inpl()
A.sort()
ans = gcd(A[0], A[1])
for a in A:
    ans = gcd(ans, a)
print(ans)

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

sys.setrecursionlimit(2000)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


n = int(input())
A = inpl()

ans = gcd(A[0], A[1])
for a in A[2:]:
    ans = gcd(ans, a)
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(20000)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


n = int(input())
A = inpl()
A.sort()
ans = gcd(A[0], A[1])
for a in A:
    ans = gcd(ans, a)
print(ans)

Buggy python submission (verdict: Runtime Error):
def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


n = int(input())
A = inpl()

ans = gcd(A[0], A[1])
for a in A[2:]:
    ans = gcd(ans, a)
print(ans)

Fixed submission:
import sys

sys.setrecursionlimit(20000)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    la = max(a, b)
    sm = min(a, b)
    if la % sm == 0:
        return sm
    else:
        return gcd(sm, la - sm)


n = int(input())
A = inpl()
A.sort()
ans = gcd(A[0], A[1])
for a in A:
    ans = gcd(ans, a)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def inpl():
    return list(map(int, input().split()))


N, K = inpl()
A = inpl()

if K == 0:
    print(sum(A))
    exit()

t = len(bin(K)) - 2
oneCounter = [0 for _ in range(t)]
# print(t)
sumA = 0
for a in A:
    sumA += a
    tmp = bin(a)[2:]
    # print(tmp)
    for i, x in enumerate(reversed(tmp)):
        # print(i, x)
        if i >= t:
            break
        elif x == "0":
            continue
        oneCounter[i] += 1
    # print(a, zeroCounter)

# print(oneCounter)
nibeki = [2 ** i for i in range(t)]
ans = sumA
for i in range(t - 1):
    if 2 * oneCounter[i] < N:
        # print(i, oneCounter[i])
        ans += nibeki[i] * (N - 2*oneCounter[i])

# print(ans, "koo")
for i in range(nibeki[t-1], K+1):
    tmp = 0
    # print(bin(i))
    for j in range(t):
        # print(j)
        if bin(i)[-1 - j] == "0":
            # print(j, "i")
            continue
        tmp += nibeki[j] * (N-2*oneCounter[j])
    # print(i, tmp)
    ans = max(ans, sumA + tmp)
print(ans)


Fixed submission:
def inpl():
    return list(map(int, input().split()))


N, K = inpl()
A = inpl()

if K == 0:
    print(sum(A))
    exit()

t = len(bin(K)) - 2
oneCounter = [0 for _ in range(t)]
# print(t)
sumA = 0
for a in A:
    sumA += a
    tmp = bin(a)[2:]
    # print(tmp)
    for i, x in enumerate(reversed(tmp)):
        # print(i, x)
        if i >= t:
            break
        elif x == "0":
            continue
        oneCounter[i] += 1
    # print(a, zeroCounter)

# print(oneCounter)
nibeki = [2**i for i in range(t)]
ans = sumA
orK = bin(K)[2:]
# print(orK)
tmp = 0
for j in range(len(orK)):
    if orK[j] == "0":
        continue
    tmp += nibeki[t - 1 - j] * (N - 2 * oneCounter[t - 1 - j])
ans = max(ans, sumA + tmp)

for i in range(len(orK)):
    if orK[i] == "0":
        continue

    tmp = 0
    for j in range(i):
        if orK[j] == "0":
            continue
        tmp += nibeki[t - 1 - j] * (N - 2 * oneCounter[t - 1 - j])
    # print(tmp)

    for j in range(i + 1, t):
        if 2 * oneCounter[t - 1 - j] < N:
            # print(i, oneCounter[i])
            tmp += nibeki[t - 1 - j] * (N - 2 * oneCounter[t - 1 - j])
    # print(orK, i, ans, tmp)
    ans = max(ans, sumA + tmp)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

A = [0 for _ in range(n)]
for i in range(n):
    a = int(input())
    for k in range(i):
        if a <= A[k]:
            # print(A[k + 1: i + 1], A[k:i])
            A[k + 1: i + 1] = A[k: i]
            A[k] = a
            break
    else:
        A[i] = a
    # print(A)
# A.sort()
# print(A)

ans = 0
end = [A[0], A[-1]]
ans += abs(A[-1] - A[0])
A = A[1:-1]
while len(A) != 0:
    # print(ans, A, end)
    sl = [A[0], A[-1]]

    tmp_l = []
    tmp_l.append(abs(end[0] - sl[0]))
    tmp_l.append(abs(end[1] - sl[0]))
    tmp_l.append(abs(end[0] - sl[1]))
    tmp_l.append(abs(end[1] - sl[1]))

    tmp = max(tmp_l)
    index = tmp_l.index(tmp)
    ans += tmp

    # print(index, tmp_l[index])
    if index % 2 == 0:
        end[0] = sl[index == 2]

    else:
        end[1] = sl[index == 3]

    if index < 2:
        A = A[1:]
    else:
        A = A[:-1]

print(ans)

Fixed submission:
n = int(input())

A = []
for i in range(n):
    A.append(int(input()))
A.sort()
# print(A)

ans = 0
end = [A[0], A[-1]]
ans += abs(A[-1] - A[0])
A = A[1:-1]
s = 0
L = -1
for _ in range(n - 2):
    # print(ans, A, end)
    sl = [A[s], A[L]]
    # print(sl)

    tmp_l = []
    tmp_l.append(abs(end[0] - sl[0]))
    tmp_l.append(abs(end[1] - sl[0]))
    tmp_l.append(abs(end[0] - sl[1]))
    tmp_l.append(abs(end[1] - sl[1]))

    tmp = max(tmp_l)
    index = tmp_l.index(tmp)
    ans += tmp

    # print(index, tmp_l[index])
    if index % 2 == 0:
        end[0] = sl[index == 2]

    else:
        end[1] = sl[index == 3]

    if index < 2:
        s += 1
    else:
        L -= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

A = []
for _ in range(n):
    A.append(int(input()))
A.sort()

ans = 0
end = [A[0], A[-1]]
ans += abs(A[-1] - A[0])
A = A[1:-1]
while len(A) != 0:
    # print(ans, A, end)
    sl = [A[0], A[-1]]

    tmp_l = []
    tmp_l.append(abs(end[0] - sl[0]))
    tmp_l.append(abs(end[1] - sl[0]))
    tmp_l.append(abs(end[0] - sl[1]))
    tmp_l.append(abs(end[1] - sl[1]))

    tmp = max(tmp_l)
    index = tmp_l.index(tmp)
    ans += tmp

    # print(index, tmp_l[index])
    if index % 2 == 0:
        end[0] = sl[index == 2]

    else:
        end[1] = sl[index == 3]

    if index < 2:
        A = A[1:]
    else:
        A = A[:-1]

print(ans)

Fixed submission:
n = int(input())

A = []
for i in range(n):
    A.append(int(input()))
A.sort()
# print(A)

ans = 0
end = [A[0], A[-1]]
ans += abs(A[-1] - A[0])
A = A[1:-1]
s = 0
L = -1
for _ in range(n - 2):
    # print(ans, A, end)
    sl = [A[s], A[L]]
    # print(sl)

    tmp_l = []
    tmp_l.append(abs(end[0] - sl[0]))
    tmp_l.append(abs(end[1] - sl[0]))
    tmp_l.append(abs(end[0] - sl[1]))
    tmp_l.append(abs(end[1] - sl[1]))

    tmp = max(tmp_l)
    index = tmp_l.index(tmp)
    ans += tmp

    # print(index, tmp_l[index])
    if index % 2 == 0:
        end[0] = sl[index == 2]

    else:
        end[1] = sl[index == 3]

    if index < 2:
        s += 1
    else:
        L -= 1

print(ans)
Buggy python submission (verdict: Runtime Error):
ef multiply(x, y):
    return (x * y) % mod


def power(x, y):
    if y == 0:
        return 1
    elif y == 1:
        return x
    elif x == 1:
        return 1
    elif x == 0:
        return 0
    else:
        # print(mod)
        tmp = power(x, y // 2)
        return (multiply(tmp, tmp) * [1, x][y % 2]) % mod


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

mod = 10 ** 9 + 7

plist = [0]
nfact = 1
N
tmp = 0
for i in range(1, N):
    tmp = (tmp + power(i + 1, mod - 2)) % mod
    nfact = (nfact * (i + 1)) % mod
    plist.append(tmp)

ans = 0
# print(plist)
# print(nfact)
for i, a in enumerate(A):
    # print(i, N - i - 1, N)
    tmp = (1 + plist[i] + plist[N - i - 1]) % mod
    ans = (ans + multiply(multiply(a, tmp), nfact)) % mod

print(ans)

Fixed submission:
def multiply(x, y):
    return (x * y) % mod


def power(x, y):
    if y == 0:
        return 1
    elif y == 1:
        return x
    elif x == 1:
        return 1
    elif x == 0:
        return 0
    else:
        # print(mod)
        tmp = power(x, y // 2)
        return (multiply(tmp, tmp) * [1, x][y % 2]) % mod


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

mod = 10 ** 9 + 7

plist = [0]
nfact = 1
N
tmp = 0
for i in range(1, N):
    tmp = (tmp + power(i + 1, mod - 2)) % mod
    nfact = (nfact * (i + 1)) % mod
    plist.append(tmp)

ans = 0
# print(plist)
# print(nfact)
for i, a in enumerate(A):
    # print(i, N - i - 1, N)
    tmp = (1 + plist[i] + plist[N - i - 1]) % mod
    ans = (ans + multiply(multiply(a, tmp), nfact)) % mod

print(ans)