Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
#C - Guess The Number
n,m = list(map(int,input().split()))
tmp = []
s = []
c = []
flg = False
counter = 0
ans = 0
tmp = [input() for i in range(m)]
for i in tmp:
    i.split()
    s.append(i[0])
    c.append(i[2])
if(n ==3):
  for j in range(100,1000):
    if(flg == True):
      break
    list_j = list(str(j))
    counter = 0
    for k in s:
        if(list_j[int(k)-1] != c[counter]):
          break
        if(list_j[int(k)-1] == c[counter]):
          counter += 1
          if(counter == len(c)):
            flg = True
            break
    if(flg == True):
      ans = j
      break
if(n ==2):
  for j in range(10,100):
    if(flg == True):
      break
    list_j = list(str(j))
    counter = 0
    for k in s:
        if(list_j[int(k)-1] != c[counter]):
          break
        if(list_j[int(k)-1] == c[counter]):
          counter += 1
          if(counter == len(c)):
            flg = True
            break
    if(flg == True):
      ans = j
      break
if(n ==1):
  for j in range(0,10):
    if(flg == True):
      break
    list_j = list(str(j))
    counter = 0
    for k in s:
        if(list_j[int(k)-1] != c[counter]):
          break
        if(list_j[int(k)-1] == c[counter]):
          counter += 1
          if(counter == len(c)):
            flg = True
            break
    if(flg == True):
      ans = j
      break

if(flg == True):
    print(ans)
else:
    print('-1')


Fixed submission:
#A - Duplex Printing
n = int(input())
ans = n // 2
if(n % 2 == 1):
    ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def choose(n,r):
    res = 1
    fac = 1
    for i in range(r):
        res *= n-i
        res %= MOD
        fac *= i+1
        fac %= MOD
    return res*pow(fac,MOD-2,MOD)%MOD        

n,a,b = list(map(int,input().split()))
t=0
ans = pow(2,n)-1
MOD = 10**9+7

ans -= choose(n,a)
ans -= choose(n,b)

ans = ans % MOD
print(int(ans))

Fixed submission:
def choose(n,r):
    res = 1
    fac = 1
    for i in range(r):
        res *= n-i
        res %= MOD
        fac *= i+1
        fac %= MOD
    return res*pow(fac,MOD-2,MOD)%MOD        

n,a,b = list(map(int,input().split()))
MOD = 10**9+7
t=0
ans = pow(2,n,MOD)-1


ans -= choose(n,a)
ans -= choose(n,b)

ans = ans % MOD
print(int(ans))

Buggy python submission (verdict: Runtime Error):
n,a,b = list(map(int,input().split()))
kumiawase_bunshi = 1
kumiawase_bunbo = 1
t=0
ans = 0
for i in range (1,n+1):
    if(i != a and i != b):
        t=n
        for j in range (n-i+1,n+1):
            kumiawase_bunshi = kumiawase_bunshi * j
        for k in range (1,i+1):
            kumiawase_bunbo = kumiawase_bunbo * k
        ans = ans + (kumiawase_bunshi / kumiawase_bunbo)
        kumiawase_bunshi = 1
        kumiawase_bunbo = 1
ans = ans % 1000000007
print(ans)

Fixed submission:
def choose(n,r):
    res = 1
    fac = 1
    for i in range(r):
        res *= n-i
        res %= MOD
        fac *= i+1
        fac %= MOD
    return res*pow(fac,MOD-2,MOD)%MOD        

n,a,b = list(map(int,input().split()))
MOD = 10**9+7
t=0
ans = pow(2,n,MOD)-1


ans -= choose(n,a)
ans -= choose(n,b)

ans = ans % MOD
print(int(ans))
Buggy python submission (verdict: Runtime Error):
#D - Dice in Line
#一つのサイコロふったときに出る目の期待値は(面の数+1)/2
n,k=list(map(int,input().split()))
dices = list(map(float,input().split()))
expected = []
cumulative_sum = []
answer = 0
i = 0
j = 0
sumOfValue = 0
#サイコロの目を期待値に変換して累積和のリスト作る
for eyes in dices:
    i = (eyes+1)/2
    j = j + i
    expected.append(j)
#最大値を求める
for maxvalue in range(len(expected)-k):
    sumOfValue = expected[maxvalue+3] - expected[maxvalue]
    if(answer < sumOfValue):
        answer = sumOfValue
print(answer)

Fixed submission:
#D - Dice in Line
#一つのサイコロふったときに出る目の期待値は(面の数+1)/2
n,k=list(map(int,input().split()))
dices = list(map(float,input().split()))
expected = []
answer = 0
i = 0
j = 0
sumOfValue = 0
expected.append(i)
#サイコロの目を期待値に変換して累積和のリスト作る
for eyes in dices:
    i = (eyes+1)/2
    j = j + i
    expected.append(j)
#最大値を求める
for t in range(n-k+1):
    sumOfValue = expected[t+k] - expected[t]
    if(answer <= sumOfValue):
        answer = sumOfValue
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#D - Dice in Line
#一つのサイコロふったときに出る目の期待値は(面の数+1)/2
#面の数から期待値に変換してからforぶん回して無理やり最大値を求める
n,k=list(map(int,input().split()))
dices = list(map(float,input().split()))
expected = []
answer = 0
sumOfValue = 0
#サイコロの目を期待値に変換
for eyes in dices:
    expected.append((eyes+1)/2)
#最大値を求める
for maxvalue in range(len(expected)-k+1):
    sumOfValue = sum(expected[maxvalue:maxvalue+k])
    if(answer < sumOfValue):
        answer = sumOfValue
print(answer)

Fixed submission:
#D - Dice in Line
#一つのサイコロふったときに出る目の期待値は(面の数+1)/2
n,k=list(map(int,input().split()))
dices = list(map(float,input().split()))
expected = []
answer = 0
i = 0
j = 0
sumOfValue = 0
expected.append(i)
#サイコロの目を期待値に変換して累積和のリスト作る
for eyes in dices:
    i = (eyes+1)/2
    j = j + i
    expected.append(j)
#最大値を求める
for t in range(n-k+1):
    sumOfValue = expected[t+k] - expected[t]
    if(answer <= sumOfValue):
        answer = sumOfValue
print(answer)
Buggy python submission (verdict: Runtime Error):
h = int(input())
a = int(input())
ans = 0
ans = h // a
ans += 1
print(ans)

Fixed submission:
h,a = list(map(int,input().split()))
ans = 0
syou = h // a
mod = h % a
ans = syou
if(mod != 0):
    ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
#D - Lucky PIN
n=int(input())
s=list(str(input()))
password_list=[]
password_unique_list = []
for i in range(0,n-2):
    for j in range (i+1, n-1):
        for k in range  (j+1, n):
            password_i = s[i]
            password_j = s[j]
            password_k = s[k]
            password = password_i + password_j + password_k
            password_list.append(password)
password_unique_list = set(password_list)
print(len(password_unique_list))

Fixed submission:
#D - Lucky PIN
#000～999までそれぞれ作成できるかをチェックする
n=int(input())
s=str(input())
ans = 0
for i in range (10):
    for j in range (10):
        for k in range (10):
            check_i = (s.find(str(i)))
            check_j = (s.find(str(j),check_i+1))
            check_k = (s.find(str(k),check_j+1))
            if(check_i >= 0 and check_j >= 0 and check_k >= 0):
                ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
import math

H = int(input())

print(int(math.pow(2, int(math.log2(H)) +1) - 1))

Fixed submission:
import math

H = int(input())

print(int(math.pow(2, int(math.log2(H)) +1) - 1))

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

H = int(input())

print(int(math.pow(2, int(math.log2(H)+1))-1))

Fixed submission:
import math

H = int(input())

print(int(math.pow(2, int(math.log2(H)) +1) - 1))
Buggy python submission (verdict: Runtime Error):
N,X = map(int,input().split())
l = list(map(int,input().split()))

pos = 0

for i in range(N+1):
    pos = pos + l[i]

    if pos > X:
        print(i+1)
        exit()

print(i+1)



Fixed submission:
N,X = map(int,input().split())
l = list(map(int,input().split()))

pos = 0
cnt = 1

for i in range(N):
    pos += l[i]

    if pos <= X:
        cnt += 1
    else:
        break

print(cnt)

Buggy python submission (verdict: Runtime Error):
N,X = map(int,input().split())
l = list(map(int,input().split()))

pos = 0

for i in range(N+1):
    pos = pos + l[i]

    if pos > X:
        print(i+1)
        exit()

print(N+1)



Fixed submission:
N,X = map(int,input().split())
l = list(map(int,input().split()))

pos = 0
cnt = 1

for i in range(N):
    pos += l[i]

    if pos <= X:
        cnt += 1
    else:
        break

print(cnt)
Buggy python submission (verdict: Runtime Error):
import math

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

pointID = -1
min_diff = math.inf

for i in range (N):
    #地点i の温度 と 平均気温の差(絶対値)
    diff = abs(T - 1000 * H[i] - A)

    if diff < min_diff:
        pointID = i+1
        min_diff = diff

print(pointID)

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

pointID = -1
min_diff = 9999999999

for i in range (N):
    #地点i の温度 と 平均気温の差(絶対値)
    diff = abs(T - H[i] * 0.006 - A)

    if diff < min_diff:
        pointID = i+1
        min_diff = diff

print(pointID)

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

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

pointID = -1
min_diff = math.inf

for i in range (N):
    #地点i の温度 と 平均気温の差(絶対値)
    diff = abs(T - 1000 * H[i] - A)

    if diff < min_diff:
        pointID = i+1
        min_diff = diff

print(pointID)

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

pointID = -1
min_diff = 9999999999

for i in range (N):
    #地点i の温度 と 平均気温の差(絶対値)
    diff = abs(T - H[i] * 0.006 - A)

    if diff < min_diff:
        pointID = i+1
        min_diff = diff

print(pointID)
Buggy python submission (verdict: Runtime Error):
import math

dp = [float('inf') for _ in range(110000)]
withdraw = []

N = int(input())

#引き出し可能な金額の配列にする
withdraw.append(1)

for i in range( math.floor(math.log(N,6)) + 1):
    withdraw.append(6**i)

for i in range( math.floor(math.log(N,9)) + 1):
    withdraw.append(9**i)

withdraw = sorted(list(set(withdraw)))

dp[0] = 0

for i in range(N+1):
    for j in withdraw:
        dp[i+j] = min(dp[i+j],dp[i]+1)
        
print(dp[N])


Fixed submission:
import math

dp = [float('inf') for _ in range(200000)]
withdraw = []

N = int(input())

#引き出し可能な金額の配列にする
withdraw.append(1)

for i in range(20):
    if 100000 >= 6**i:
        withdraw.append(6**i)
    if 100000 >= 9**i:
        withdraw.append(9**i)

withdraw = sorted(list(set(withdraw)))

dp[0] = 0

for i in range(N+1):
    for j in withdraw:
        dp[i+j] = min(dp[i+j],dp[i]+1)
        
print(dp[N])
Buggy python submission (verdict: Time Limit Exceeded):
K,S = map(int,input().split())

count = 0
for i in range(K+1):
    for j in range(K+1):
        for k in range(K+1):
            if i + j + k == S:
                count +=1

print(count)

Fixed submission:
K,S = map(int,input().split())

count = 0
for i in range(K+1):
    for j in range(K+1):
        if 0 <= S-(i+j) <= K:
            count +=1

print(count)
Buggy python submission (verdict: Runtime Error):
H,N=map(int, input().split())
A = list(map(int, input().split()))

if(sum(A) > H):
  print("Yes")
else:
  print("No")


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

count = 0;

while(H > 0):
  H -= A
  count += 1
  
print(count)
Buggy python submission (verdict: Runtime Error):
a = int(input())
s = str(input())
if a >= 3200:
  print(s)
else:
  print 'red'

Fixed submission:
a = int(input())
s = str(input())
if a >= 3200:
  print(s)
else:
  print('red')

Buggy python submission (verdict: Runtime Error):
a = input()
s = input()
if a >= 3200:
  print s
else:
  print 'red'

Fixed submission:
a = int(input())
s = str(input())
if a >= 3200:
  print(s)
else:
  print('red')
Buggy python submission (verdict: Runtime Error):
a = int(input())
b = int(input())

if(a >= -100 and b <= 100):
	l = [a+b,a-b,a*b]
	print(max(l))

Fixed submission:
a,b = map(int, input().split())
print(max(a+b, a-b, a*b))

Buggy python submission (verdict: Runtime Error):
a = int(input())
b = int(input())

l = [a+b,a-b,a*b]

print(max(l))

Fixed submission:
a,b = map(int, input().split())
print(max(a+b, a-b, a*b))
Buggy python submission (verdict: Time Limit Exceeded):
import math

N = int(input())

r = 0
for u in range(1, 10):
    for l in range(1, u + 1):
        r1 = 0
        r2 = 0
        for i in range(1, N + 1):
            x = int(i / (10 ** int(math.log10(i))))
            if i % 10 == l and x == u:
                r1 += 1
            if i % 10 == u and x == l:
                r2 += 1
        r += r1 * r2
        if u != l:
            r += r1 * r2
print(r)



Fixed submission:
import math
from collections import defaultdict

N = int(input())

rr = defaultdict(lambda: 0)
for i in range(1, N + 1):
    x = int(i / (10 ** int(math.log10(i))))
    y = i % 10
    rr[(x, y)] += 1

r = 0
for u in range(1, 10):
    for l in range(1, 10):
        r += rr[(u, l)] * rr[(l, u)]
print(r)

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

r = 0
for u in range(1, 10):
    for l in range(1, 10):
        r1 = 0
        r2 = 0
        for i in range(1, N + 1):
            x = int(str(i)[0])
            if i % 10 == l and x == u:
                r1 += 1
            if i % 10 == u and x == l:
                r2 += 1
        r += r1 * r2
print(r)



Fixed submission:
import math
from collections import defaultdict

N = int(input())

rr = defaultdict(lambda: 0)
for i in range(1, N + 1):
    x = int(i / (10 ** int(math.log10(i))))
    y = i % 10
    rr[(x, y)] += 1

r = 0
for u in range(1, 10):
    for l in range(1, 10):
        r += rr[(u, l)] * rr[(l, u)]
print(r)

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

r = 0
for u in range(1, 10):
    for l in range(1, 10):
        r1 = 0
        r2 = 0
        for i in range(1, N + 1):
            if i % 10 == l and int(str(i)[0]) == u:
                r1 += 1
            if i % 10 == u and int(str(i)[0]) == l:
                r2 += 1
        r += r1 * r2
print(r)



Fixed submission:
import math
from collections import defaultdict

N = int(input())

rr = defaultdict(lambda: 0)
for i in range(1, N + 1):
    x = int(i / (10 ** int(math.log10(i))))
    y = i % 10
    rr[(x, y)] += 1

r = 0
for u in range(1, 10):
    for l in range(1, 10):
        r += rr[(u, l)] * rr[(l, u)]
print(r)
Buggy python submission (verdict: Runtime Error):
import math

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

lcm = 1
for a in A:
    lcm *= (a // math.gcd(lcm, a)) % (10**9 + 7)

for i in range(len(A)):
    A[i] = lcm // A[i]

print(sum(A) % (10**9 + 7))

Fixed submission:
import fractions

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

lcm = 1
for a in A:
    lcm *= (a // fractions.gcd(lcm, a)) % (10**9 + 7)

for i in range(len(A)):
    A[i] = lcm // A[i]

print(sum(A) % (10**9 + 7))

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

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

lcm = 1
for a in A:
    lcm *= (a // math.gcd(lcm, a)) % (10**9 + 7)

for i in range(len(A)):
    A[i] = int(lcm / A[i])

print(sum(A) % (10**9 + 7))



Fixed submission:
import fractions

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

lcm = 1
for a in A:
    lcm *= (a // fractions.gcd(lcm, a)) % (10**9 + 7)

for i in range(len(A)):
    A[i] = lcm // A[i]

print(sum(A) % (10**9 + 7))
Buggy python submission (verdict: Runtime Error):
import numpy as np
def comb_mod(n, a, m):
  if a > n:
    return 0
  a = min(a, n-a+1)
  # tbl = [[0]*(a+1) for _ in range(2)]
  tbl = np.zeros((2, a+1), dtype=np.int64)
  tbl[0][0] = 1
  tbl[1][0] = 1
  # for i in tqdm(range(n)):
  for i in range(n):
    # tbl[i%2][0] = 1
    tbl[i%2][1:] = (tbl[(i+1)%2][1:] + tbl[(i+1)%2][:-1]) % m
    # for k in range(min(i+1, len(tbl[i%2]))):
    #   if i == k:
    #     tbl[i%2][k] = 1
    #   elif i < k:
    #     tbl[i%2][k] = 0
    #   elif k == 0:
    #     tbl[i%2][k] = 1
    #   else:
    #     tbl[i%2][k] = (tbl[(i-1)%2][k-1] + tbl[(i-1)%2][k]) % m
  return tbl[(n-1)%2][a]

n, a, b = map(int, input().split(' '))
c = 0
for i in range(1, n+1):
  if i == a or i == b:
    continue
  c += comb_mod(n, i, 1000000007)
print(c)

Fixed submission:
N, R = input().split(' ')
N = int(N)
R = int(R)
if N >= 10:
  I = R
else:
  I = R + 100 * (10 - N)
print(I)
Buggy python submission (verdict: Time Limit Exceeded):
N, K, Q = list(map(int, input().split()))
P_list = [K for _ in range(N)]

for _ in range(Q):
    win = int(input()) -1
    P_list = list(map(lambda x:x-1, P_list))
    P_list[win] += 1


for i in range(N):
    if P_list[i] > 0:
        print("Yes")
    else:
        print("No")


Fixed submission:
N, K, Q = list(map(int, input().split()))
P_list = [K for _ in range(N)]

P_list = list(map(lambda x:x-Q, P_list))


for _ in range(Q):
    a = int(input())-1
    P_list[a] += 1

for i in range(N):
    if P_list[i] > 0:
        print("Yes")
    else:
        print("No")
Buggy python submission (verdict: Runtime Error):
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a


N = int(input())
A = list(map(int, input().split()))
L = [0 for i in range(N)]
R = [0 for i in range(N)]
T = [0 for i in range(N)]

for i in range(N):
    if i == 0:
        L[i] = 0
    else:
        L[i] = gcd(L[i-1], A[i-1])
for i in range(N-1, 0, -1):
    if i == N-1:
        R[i] = 0
    else:
        R[i] = gcd(R[i+1], A[i+1])
for i in range(N):
    T[i] = gcd(L[i], R[i])

print(max(T))

Fixed submission:
I = list(map(int, input().split()))
A = I[0]
B = I[1]
T = I[2]

ans = (T // A) * B

print(ans)
Buggy python submission (verdict: Runtime Error):
N, T = list(map(int, input().split(" ")))
c = []
t = []

for i in range(N):
    ci, ti = input().split(" ")
    c.append(int(ci))
    t.append(int(ti))
if min(t) > T:
    print("TLE\n")

else:
    cost = 10000
    for i in range(N):
        if cost > c[i] and T >= t[i]:
            cost = c[i]
    print(

Fixed submission:
N, T = list(map(int, input().split(" ")))
c = []
t = []

for i in range(N):
    ci, ti = input().split(" ")
    c.append(int(ci))
    t.append(int(ti))
if min(t) > T:
    print("TLE\n")

else:
    cost = 10000
    for i in range(N):
        if cost > c[i] and T >= t[i]:
            cost = c[i]
    print(cost)
Buggy python submission (verdict: Runtime Error):
import collections


n = int(input())
v = list(map(int, input().split()))
odd = []
even = []
for i in range(int(n/2)):
    even.append(v[2*i])
    odd.append(v[2*i + 1])
evencount = collections.Counter(even)
oddcount = collections.Counter(odd)


def hoge(i):
    global ans
    try:
        if evencount.most_common()[i][0] == oddcount.most_common()[i][0]:

            if evencount.most_common()[i][1] > oddcount.most_common()[i][1]:
                ans = n - evencount.most_common()[i][1] - oddcount.most_common()[i][1]
                print(ans)
            elif evencount.most_common()[i][1] < oddcount.most_common()[i][1]:
                ans = n - evencount.most_common()[i][1] - oddcount.most_common()[i][1]
                print(ans)
            else:
                if len(evencount) == 1:
                    ans = int(n/2)
                    print(ans)
                else:
                    hoge(i+1)

        else: 
            ans = n - evencount.most_common()[i][1] - oddcount.most_common()[i][1]
            print(ans)
    except IndexError:   
        ans = n - evencount.most_common()[0][1] - oddcount.most_common()[1][1]
        print(ans) 


hoge(0)

Fixed submission:
import numpy as np

n = int(input())

ans = 0

if n % 10 == 9:
    ans += 1
else:
    ans += 9
n = int(n/10)


if n % 10 == 9:
    ans += 10
else:
    ans += 90
n = int(n/10)
if n % 10 == 9:
    ans += 100
else:
    ans += 900


print(ans)
Buggy python submission (verdict: Runtime Error):
a = list(map(int, input().split()))
a = sorted(a)
print(abs(a[3] - a[1]))

Fixed submission:
a = list(map(int, input().split()))
a = sorted(a)
print(abs(a[2] - a[0]))
Buggy python submission (verdict: Runtime Error):
s = input()
a = 0
for i in range(3):
	if s[i:i+1] != 'x':
      a += 100
print(700 + a)

Fixed submission:
s = input()
a = 0
for i in range(3):
	if s[i:i+1] != 'x':
		a += 100
print(700 + a)

Buggy python submission (verdict: Runtime Error):
s = input()
a = 0
for char in s:
	if char != 'x':
    	a++
print(700+a*100)

Fixed submission:
s = input()
a = 0
for i in range(3):
	if s[i:i+1] != 'x':
		a += 100
print(700 + a)
Buggy python submission (verdict: Runtime Error):
s = input()
s = sorted(s)
if s = ['a','b','c']:
    print('Yes')
else:
    print('No')

Fixed submission:
s = input()
c = ''.join(sorted(s))
print('Yes') if c == 'abc' else print('No')
Buggy python submission (verdict: Runtime Error):
for i in range(4):
    a[i] = int(input())
print(min(a[0],a[1]) + min(a[2], a[3]))    

Fixed submission:
a = [0, 0, 0, 0]
for i in range(4):
    a[i] = int(input())
print(min(a[0],a[1]) + min(a[2], a[3]))
Buggy python submission (verdict: Runtime Error):
x,y = input().split()
print('<') if x < y elif x > y print('>') else print('=')

Fixed submission:
x,y = input().split()
print('<') if x < y else print('>') if x > y else print('=')
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

S = input()
Q = int(input())

S = [s for s in S]

is_filp = False
for i in range(Q):
    q = input()

    if q[0] == '1':
        if is_filp:
            is_filp = False
        else:
            is_filp = True
    elif q[0] == '2':
        _, f, c = q.split(' ')
        if f == '1':
            if is_filp:
                S.insert(len(S), c)
            else:
                S.insert(0, c)
        else:
            if is_filp:
                S.insert(0, c)
            else:
                S.insert(len(S), c)

if is_filp:
    S.reverse()
    print(''.join(S))
else:
    print(''.join(S))

Fixed submission:
import bisect

S = input()
Q = int(input())

S = {i: s for i, s in enumerate(S)}


head = -1
tail = len(S)

is_filp = False
for i in range(Q):
    q = input()

    if q[0] == '1':
        if is_filp:
            is_filp = False
        else:
            is_filp = True
    elif q[0] == '2':
        _, f, c = q.split(' ')
        if f == '1':
            if is_filp:
                S[tail] = c
                tail += 1
            else:
                S[head] = c
                head -= 1
        else:
            if is_filp:
                S[head] = c
                head -= 1
            else:
                S[tail] = c
                tail += 1

if is_filp:
    print(''.join([S[i] for i in range(tail - 1, head, -1)]))
else:
    print(''.join([S[i] for i in range(head + 1, tail)]))

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

is_filp = False
for i in range(Q):
    q = input()

    if q[0] == '1':
        if is_filp:
            is_filp = False
        else:
            is_filp = True
    elif q[0] == '2':
        _, f, c = q.split(' ')
        if f == '1':
            if is_filp:
                S = S + c
            else:
                S = c + S 
        else:
            if is_filp:
                S = c + S
            else:
                S = S + c
if is_filp:
    print(S[::-1])
else:
    print(S)

Fixed submission:
import bisect

S = input()
Q = int(input())

S = {i: s for i, s in enumerate(S)}


head = -1
tail = len(S)

is_filp = False
for i in range(Q):
    q = input()

    if q[0] == '1':
        if is_filp:
            is_filp = False
        else:
            is_filp = True
    elif q[0] == '2':
        _, f, c = q.split(' ')
        if f == '1':
            if is_filp:
                S[tail] = c
                tail += 1
            else:
                S[head] = c
                head -= 1
        else:
            if is_filp:
                S[head] = c
                head -= 1
            else:
                S[tail] = c
                tail += 1

if is_filp:
    print(''.join([S[i] for i in range(tail - 1, head, -1)]))
else:
    print(''.join([S[i] for i in range(head + 1, tail)]))