Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

def main():
    s = list(input())
    q = int(input())

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = [query[-1]] + s
                else:
                    s = s + [query[-1]]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + [query[-1]]
                else:
                    s = [query[-1]] + s

    if ctn % 2 == 1:
        print("".join(s[::-1]))
    else:
        print("".join(s))

main()

### FIXED CODE ###

from collections import deque

def main():

    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()
### BUGGY CODE ###

from math import gcd
from functools import reduce

n = int(input())
A = list(map(int, input().split()))
res = ['pairwise coprime', 'setwise coprime', 'not coprime']

# 素因数分解 O(n ** 0.5)
def prime_factor(n):
    factor = []
    for i in range(2, int(n ** 0.5) + 1):
        while (n % i == 0):
            factor.append(i)
            n //= i
    if n != 1:
        factor.append(n)
    return factor

prime_factors = []
for a in A:
    factor = prime_factor(a)
    factor_set = list(set(factor))
    prime_factors.extend(factor_set)

if len(prime_factors) == len(set(prime_factors)):
    print(res[0])
elif reduce(gcd, A) == 1:
    print(res[1])
else:
    print(res[2])

### FIXED CODE ###

from math import gcd
from functools import reduce

n = int(input())
A = list(map(int, input().split()))
res = ['pairwise coprime', 'setwise coprime', 'not coprime']

MAX = 10 ** 6 + 1000
dp = [i for i in range(MAX)]
# dp[x]はxを割り切る最小の素数
for x in range(2, MAX):
    if dp[x] < x: continue
    for y in range(x + x, MAX, x):
        if dp[y] == y:
            dp[y] = x

def get_factor(n):
    factors = []
    while n > 1:
        factors.append(dp[n])
        n //= dp[n]
    return factors

prime_factors = []
for a in A:
    factor = get_factor(a)
    factor_set = list(set(factor))
    prime_factors.extend(factor_set)

if len(prime_factors) == len(set(prime_factors)):
    print(res[0])
elif reduce(gcd, A) == 1:
    print(res[1])
else:
    print(res[2])
### BUGGY CODE ###

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

def gcd(x,y):
  if y > x:
    x, y = y, x
  while x % y != 0:
    x, y = y, x % y
  return y

#print(gcd(660, 420))

def soinsu(n):
  count = 0
  i = 2
  while n != 1:
    if n % i == 0:
      count += 1
    while n % i == 0:
      n = n // i
    i += 1
  return count

#print(soinsu(9))

if __name__ == "__main__":
  gcd = gcd(A, B)
  ans = soinsu(gcd) + 1
  print(ans)

### FIXED CODE ###

import collections
A, B = map(int, input().split())

def gcd(x,y):
  if y > x:
    x, y = y, x
  while x % y != 0:
    x, y = y, x % y
  return y

#print(gcd(660, 420))
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

#print(prime_factorize(9))

if __name__ == "__main__":
  gcd = gcd(A, B)
  c = collections.Counter(prime_factorize(gcd))
  ans = len(c.keys()) + 1
  print(ans)
### BUGGY CODE ###

from collections import defaultdict as dd
from collections import deque
from copy import deepcopy as dc
N, M = map(int, input().split())
e = dd(list)
inf = 10 ** 9
for _ in range(M):
  u, v = map(int, input().split())
  e[u].append(v)
S, T = map(int, input().split())

stopped = [inf] * (N + 1)
Q = deque()
Q.append(S)
stopped[S] = 0
saw2 = {i: 0 for i in range(1, N + 1)}
saw3 = {i: 0 for i in range(1, N + 1)}
while len(Q) != 0:
  s2 = dc(saw2)
  s3 = dc(saw3)
  p = Q.popleft()
  if stopped[p] >= stopped[T]:
    break
  for q in e[p]:
    for r in e[q]:
      if s2[r] == 1:
        continue
      else:
        s2[r] = 1
      for s in e[r]:
        if s3[s] == 1:
          continue
        else:
          s3[s] = 1
        if stopped[s] > stopped[p] + 1:
          Q.append(s)
          stopped[s] = stopped[p] + 1
if stopped[T] == inf:
  print(-1)
else:
  print(stopped[T])


### FIXED CODE ###

from collections import defaultdict as dd
from collections import deque
N, M = map(int, input().split())
e = dd(list)
inf = 10 ** 9
for _ in range(M):
  u, v = map(int, input().split())
  e[u].append(v + N)
  e[u + N].append(v + 2 * N)
  e[u + 2 * N].append(v)
S, T = map(int, input().split())
S += 2 * N
T += 2 * N
stopped = [inf] * (3 * (N + 1))
Q = deque()
Q.append(S)
stopped[S] = 0
while len(Q) != 0:
  p = Q.popleft()
  if stopped[p] >= stopped[T]:
    break
  for q in e[p]:
    if stopped[q] > stopped[p] + 1:
      Q.append(q)
      stopped[q] = stopped[p] + 1
if stopped[T] == inf:
  print(-1)
else:
  print(stopped[T] // 3)
### BUGGY CODE ###

def main():
    import sys
    import time
    input = sys.stdin.buffer.readline
    from math import gcd
    N = int(input())
    A = list(map(int, input().split()))
    g = A[0]

    S = 1
    for x in A:
        S *= x
    start = time.time()

    def factorization(n):
        ret = set()
        temp = n
        for i in range(2, int(-(-n**0.5//1))+1):
            if temp % i == 0:
                cnt = 0
                while temp % i == 0:
                    cnt += 1
                    temp //= i
                ret.add(i)

        if temp != 1:
            ret.add(temp)

        if len(ret) == 0 and n != 1:
            ret.add(n)

        return ret

    flag = True
    s = factorization(A[0])
    for x in A[1:]:
        tmp = 0
        now = time.time()
        if now - start > 1.8:
            exit(1)
            break
        for y in s:
            if x % y == 0:
                flag = False
                break
        if flag:
            s |= factorization(x)
            continue
        else:
            break

    if flag:
        print("pairwise coprime")
    else:
        g = A[0]
        for i, x in enumerate(A[::-1]):
            g = gcd(g, x)
        if g == 1:
            print("setwise coprime")

        else:
            print("not coprime")


main()


### FIXED CODE ###

def main():
    import sys
    input = sys.stdin.buffer.readline
    from math import gcd
    N = int(input())
    A = list(map(int, input().split()))
    g = A[0]
    M = max(A)
    X = [i for i in range(M+1)]
    S = [True for i in range(M+1)]
    S[0] = False
    S[1] = False

    for i in range(1, M+1):
        if S[i]:
            for j in range(i, M+1, i):
                if S[j]:
                    S[j] = False
                    X[j] = i
    flag = True
    s = set()
    ls = 0
    for x in A:
        tmp = set()
        while(X[x] != 1):
            tmp.add(X[x])
            x //= X[x]
        l = len(tmp)
        s |= tmp
        if len(s) == ls + l:
            ls += l
            continue
        else:
            flag = False
            break

    if flag:
        print("pairwise coprime")
    else:
        for x in A[1:]:
            g = gcd(g, x)
        if g == 1:
            print("setwise coprime")

        else:
            print("not coprime")


main()
### BUGGY CODE ###

import sys
import collections
import copy
import math
from collections import Counter
def verify_anagrams(first_word, second_word):
    def get_counter(s):
        return Counter(s.upper().replace(' ', ''))

    return get_counter(first_word) == get_counter(second_word)
input = sys.stdin.readline
N = int(input())
ans_list=["test"]
ans_num=[1]
ans = 0
for i in range(N):
    chk = input().strip()
    for j in range(len(ans_list)):
        if verify_anagrams(ans_list[j],chk):
            ans+= ans_num[j]
            ans_num[j] +=1
            break
        if j == len(ans_list)-1:
            ans_list.append(chk)
            ans_num.append(1)
print(ans)


### FIXED CODE ###

import math
N = int(input())
s_dict = {}
ans = 0
for i in range(N):
    s = input()
    s = "".join(sorted(s))
    if s not in s_dict:
        s_dict[s] = 0
        s_dict[s] += 1
    else:
        ans += s_dict[s]
        s_dict[s] += 1
print(ans)
### BUGGY CODE ###

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

c = 0
for i in range(2, n):
    for j in range(2, 7):
        if (7 * i + j - 7) * 9 % 11 == k:
            c += 1

print(c)

### FIXED CODE ###

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

p = int((n - 2) / 11)
c = p * 5
for i in range(2 + p * 11 , n):
    for j in range(2, 7):
        if (7 * i + j - 7) * 9 % 11 == k:
            c += 1

print(c)
### BUGGY CODE ###

R, G, B, N = map(int, input().split())
ans = 0
for r in range((N//R)+1):
    for g in range((N//G)+1):
        v = R * r + G * g
        if v > N:
            continue
        if (N - v) % B != 0:
            continue
        ans +=1 
print(ans)


### FIXED CODE ###

R, G, B, N = map(int, input().split())
ans = 0
for r in range((N//R)+1):
    for g in range((N//G)+1):
        v = R * r + G * g
        if v > N:
            break
        if (N - v) % B == 0:
            ans +=1 
        
print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

N,Q = map(int,input().split())
to = [[] for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

for _ in range(N-1):
    a,b=map(int,input().split())
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    p,x = map(int,input().split())
    ans[p] += x

def dfs(v, p):
    for u in to[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)
dfs(1, -1)
print(*ans[1:])


### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

N,Q = map(int,input().split())
to = [[] for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

for _ in range(N-1):
    a,b=map(int,input().split())
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    p,x = map(int,input().split())
    ans[p] += x

def dfs(v, p):
    for u in to[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)
dfs(1, -1)
print(*ans[1:])
### BUGGY CODE ###

def calc(n, to_metal, to_acorn):
    dp = list(range(n + 1))
    for i in range(n + 1):
        for m, a in zip(to_metal, to_acorn):
            if i >= m:
                dp[i] = max(dp[i], dp[i - m] + a)
    return dp[n]


def main():
    N = int(input())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

    N = calc(N, a, b)  # 1 周目 : dp[巣から持ってきたドングリ数] = 取引所 B で持てる最大ドングリ数
    N = calc(N, b, a)  # 2 周目 : dp[取引所 B から持ってきたドングリ数] = 巣に持ち帰れる最大ドングリ数
    print(N)


if __name__ == '__main__':
    main()


### FIXED CODE ###

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())

# A : ドングリと金属に変える
# B : 全部ドングリに変える
# B : ドングリと金属に変える（無駄な操作でロスがないので、いったんドングリ化してよい）
# A : ドングリに変える

dp1 = list(range(N + 1))  # dp1[巣から持ってきたドングリ数] = 取引所 B で持てる最大ドングリ数
dp1[0] = 0
for i in range(N + 1):
    if i >= ga:
        dp1[i] = max(dp1[i], dp1[i - ga] + gb)
    if i >= sa:
        dp1[i] = max(dp1[i], dp1[i - sa] + sb)
    if i >= ba:
        dp1[i] = max(dp1[i], dp1[i - ba] + bb)

N = dp1[N]
dp2 = list(range(N + 1))  # dp2[取引所 B から持ってきたドングリ数] = 巣に持ち帰れる最大ドングリ数
for i in range(N + 1):
    if i >= gb:
        dp2[i] = max(dp2[i], dp2[i - gb] + ga)
    if i >= sb:
        dp2[i] = max(dp2[i], dp2[i - sb] + sa)
    if i >= bb:
        dp2[i] = max(dp2[i], dp2[i - bb] + ba)
print(dp2[N])
### BUGGY CODE ###

n = int(input())
a_list = list(map(int,input().split()))
for i in range(n):
    a_list[i] = [a_list[i],str(i+1)]
a_list.sort()
ans = ""
for i in range(n):
    ans += a_list[i][1] + " "
print(ans)

### FIXED CODE ###

n = int(input())
a_list = list(map(int,input().split()))
ans = [0]*n
for i in range(n):
    ans[a_list[i]-1] = i+1
print(" ".join(map(str,ans)))
### BUGGY CODE ###

import sys
from math import factorial
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

MOD = 10 ** 9 + 7
N, K = rl()
A = rl()
A.sort()
min = A[0]
A = [(x - min)%MOD for x in A]
#最大値として何回登場するか
count = 0
fac = factorial
l = [fac(x) * pow(fac(K-1) * fac(max(0, x - (K-1))) % MOD, MOD-2, MOD) % MOD for x in range(N)]
for i in range(K-1, N):
    # A[i] * iC(k-1)
    count += A[i] * l[i]
    count %= MOD
for i in range(N-K+1):
    # A[i] * (N-(i+1)) C (k-1)
    count -= A[i] * l[(N-(i+1))]
    count %= MOD

print(count)


### FIXED CODE ###

import sys
from math import factorial
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 10**6
MOD = 10 ** 9 + 7
fact, fact_inv = make_fact(U, MOD)
N, K = rl()
A = rl()
A.sort()
min = A[0]
A = [(x - min)%MOD for x in A]
#最大値として何回登場するか
count = 0

for i in range(K-1, N):
    # A[i] * iC(k-1)
    count += A[i] * fact[i] % MOD * fact_inv[K-1] % MOD * fact_inv[i-(K-1)] % MOD
    count %= MOD
for i in range(N-K+1):
    # A[i] * (N-(i+1)) C (k-1)
    count -= A[i] * fact[N-(i+1)] % MOD * fact_inv[K-1] % MOD * fact_inv[N-(i+1)-(K-1)] % MOD
    count %= MOD

print(count)
### BUGGY CODE ###

n, m = map(int, input().split())
l, r = map(int, input().split())
card = set(range(l, r+1))
for i in range(1, m):
    l, r = map(int, input().split())
    card = card.intersection(set(range(l, r+1)))

print(len(card))

### FIXED CODE ###

import sys
n, m = map(int, input().split())

min_id = 1
max_id = n
for i in range(m):
    l, r = map(int, input().split())

    if min_id < l <= max_id:
        min_id = l
    elif max_id < l:
        print(0)
        sys.exit()

    if min_id <= r < max_id:
        max_id = r
    elif r < min_id:
        print(0)
        sys.exit()

print(max_id - min_id + 1)
### BUGGY CODE ###

from collections import deque

def solve():
    H, W = map(int, input().split())
    Ass = ['#'*(W+2)] + ['#'+input()+'#' for _ in range(H)] + ['#'*(W+2)]

    distss = [[-1]*(W) for _ in range(H)]
    Q = deque([])
    for i in range(1, H+1):
        for j in range(1, W+1):
            if Ass[i][j] == '#':
                Q.append((i, j))
                distss[i-1][j-1] = 0

    while Q:
        i, j = Q.popleft()
        for di, dj in [(-1,0), (1,0), (0,-1), (0,1)]:
            i2, j2 = i+di, j+dj
            if Ass[i2][j2] == '.' and distss[i2-1][j2-1] == -1:
                Q.append((i2, j2))
                distss[i2-1][j2-1] = distss[i-1][j-1] + 1

    return max(map(max, distss))

print(solve())


### FIXED CODE ###

from collections import deque

def solve():
    H, W = map(int, input().split())
    Ass = ['#'*(W+2)] + ['#'+input()+'#' for _ in range(H)] + ['#'*(W+2)]

    distss = [[-1]*(W) for _ in range(H)]
    Q = deque([])
    for i in range(1, H+1):
        for j in range(1, W+1):
            if Ass[i][j] == '#':
                Q.append((i, j))
                distss[i-1][j-1] = 0

    while Q:
        i, j = Q.popleft()
        for di, dj in [(-1,0), (1,0), (0,-1), (0,1)]:
            i2, j2 = i+di, j+dj
            if Ass[i2][j2] == '.' and distss[i2-1][j2-1] == -1:
                Q.append((i2, j2))
                distss[i2-1][j2-1] = distss[i-1][j-1] + 1

    return max(map(max, distss))

print(solve())
### BUGGY CODE ###

n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
b = [0]
for i in reversed(a):
    b.append(b[-1] + i)


def search(i):
    if a[-1] < i:
        return 0
    mi = 0
    ma = n - 1
    while mi != ma:
        m = (mi + ma) // 2
        if a[m] >= i:
            ma = m
        else:
            mi = m + 1
    return n-mi

def countmax(i):
    ans = 0
    for j in a:
        ans += search(i - j)
    return ans

mi = 0
ma = a[-1] * 2
while mi != ma:
    md = (mi + ma) // 2 + 1
    #print(md, countmax(md))
    if countmax(md) > m:
        mi = md
    else:
        ma = md - 1

ans = 0
for j in a:
    i = search(mi + 1 - j)
    ans+=b[i]+i*j
ans += (m-countmax(mi+1))*mi
print(ans)


### FIXED CODE ###

n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
b = [0]
for i in reversed(a):
    b.append(b[-1] + i)


def search(i):
    if a[-1] < i:
        return 0
    mi = 0
    ma = n - 1
    while mi != ma:
        m = (mi + ma) // 2
        if a[m] >= i:
            ma = m
        else:
            mi = m + 1
    return n-mi

def countmax(i):
    ans = 0
    for j in a:
        ans += search(i - j)
    return ans

mi = 0
ma = a[-1] * 2
while mi != ma:
    md = (mi + ma) // 2 + 1
    #print(md, countmax(md))
    if countmax(md) > m:
        mi = md
    else:
        ma = md - 1

ans = 0
for j in a:
    i = search(mi + 1 - j)
    ans+=b[i]+i*j
ans += (m-countmax(mi+1))*mi
print(ans)
### BUGGY CODE ###

# 標準入力
N = int(input())
H = list(map(int, input().split()))
H.append(10**10)
count = [0 for i in range(N)]
# 左からi番目に降り立って(iは0~N-1)
for i in range(N):
# H[i] >= H[i+1] ならcount[i]を一つ増やしてiに足していけ(H[N] = 10**10としよう)
  while H[i] >= H[i+1]:
    count[i] += 1
    i += 1
#max(count)を出力しよう
print(max(count))


### FIXED CODE ###

# 標準入力
N = int(input())
H = list(map(int, input().split()))
H.append(10**10)
count = [0 for i in range(N)]
i = 0
# 左からi番目に降り立って(iは0~N-1)、H[i]>=H[i+1]が続くまでcountを増やす。count[i]=5なら次はi+6から繰り返す
while i <= N-1:
  j = i
  while H[j] >= H[j+1]:
    count[i] += 1
    if j <= N-3:
      j += 1
    else:
      break
  i += count[i]+1
#max(count)を出力しよう
print(max(count))
### BUGGY CODE ###

a, b, x = map(int, input().split())
cnt = 0
for i in range(a, b+1):
    if i % x == 0:
        cnt += 1
print(cnt)

### FIXED CODE ###

a,b,x = map(int, input().split())
print((b//x) - ((a-1)//x))
### BUGGY CODE ###

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

mem = [-1 for i in range(n+1)]
def countd(y):
  if mem[y] == -1:
    mem[y] = d.count(y)
  return mem[y]

if d[0] != 0:
  print(0)
  exit()

count = 1
for i in range(n-1):
  x = d[i+1]
  if x == 0:
    print(0)
    exit()
  if x == 1:
    continue
  count = count * countd(x-1)

print(count % 998244353)


### FIXED CODE ###

from collections import Counter

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

mem = Counter(d)

if d[0] != 0 or mem[0] != 1:
  print(0)
  exit()

count = 1
for i in range(max(d)):
  count = count * mem[i] ** mem[i+1]
  count = count % 998244353

print(count)
### BUGGY CODE ###

import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    a = [I() - 1 for _ in range(N)]

    c = 0
    ans = 0
    is_ok = True
    while True:
        c = a[c]
        ans += 1
        if c == 0:
            is_ok = False
            break
        if c == 1:
            break

    if is_ok:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    resolve()

### FIXED CODE ###

import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    a = [I() - 1 for _ in range(N)]

    c = 0
    ans = 0
    is_ok = True
    while True:
        c = a[c]
        ans += 1
        if c == 0 or ans > N:
            is_ok = False
            break
        if c == 1:
            break

    if is_ok:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    resolve()
### BUGGY CODE ###

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


def pow_k(x, n):
    res = 1
    while n > 0:
        if n & 1:
            res *= x
        x *= x
        n //= 2
    return res


total = pow_k(2, n) - 1

now = 1
div = 1
for i in range(max(a, b) + 1):
    now *= (n - i)
    div *= i + 1
    if i == a - 1 or i == b - 1:
        total -= now // div
print(total % (10 ** 9 + 7))

### FIXED CODE ###

n, a, b = map(int, input().split())
MOD = 10 ** 9 + 7
# Pythonのpowは繰り返し二乗法で実装されている
ans = pow(2, n, MOD) - 1
product = 1
div = 1
for i in range(1, b + 1):
    product = product * (n - i + 1) % MOD
    div = div * i % MOD
    if i == a or i == b:
        # 逆元を求める
        ans -= product * pow(div, MOD - 2, MOD)
print(ans % MOD)
### BUGGY CODE ###

import math
n=int(input())

for p in range(1, n+1):
  right=math.floor(p**0.5)+1
  cnt=0
  for i in range(1, right):
    for j in range(1, i+1):
      for k in range(1, j+1):
        if i**2+j**2+k**2+i*j+j*k+k*i==p:
          if i==j and j==k:
            cnt+=1
          elif i==j and j!=k:
            cnt+=3
          elif j==k and k!=i:
            cnt+=3
          elif k==i and i!=j:
            cnt+=3
          elif i!=j and j!=k and k!=i:
            cnt+=6
  print(cnt)

### FIXED CODE ###

n=int(input())
lst=[0]*10000000
for i in range(1, 101):
  for j in range(1, i+1):
    for k in range(1, j+1):
      temp=i**2+j**2+k**2+i*j+j*k+k*i
      if i==j and j==k:
        lst[temp+1]+=1
      elif i==j and j!=k:
         lst[temp+1]+=3
      elif j==k and k!=i:
         lst[temp+1]+=3
      elif k==i and i!=j:
         lst[temp+1]+=3
      elif i!=j and j!=k and k!=i:
         lst[temp+1]+=6
for p in range(1, n+1):
  print(lst[p+1])
### BUGGY CODE ###

import numpy as np
 
N,K=map(int, input().split())
P=np.array(list(map(int, input().split())))
 
P=(P+1)/2
ans=0
for i in range(N):
  try:
    ans=max(ans,sum(P[i:i+K]))
  except:
    break
print(ans)

### FIXED CODE ###

import sys

N,K=map(int,input().split())
P=list(map(int,input().split()))
Q=[(1+p)/2 for p in P]
temp=sum(Q[:K])
ans=temp
for i in range(N):
  try:
    temp=temp-Q[i]+Q[i+K]
    ans=max(ans,temp)
  except:
    pass
else:
  print(ans)
### BUGGY CODE ###

# coding: utf-8
S = input().split()
Q = int(input())

def pop(s, rev, f, c):
    ret = s
    if rev == 1:
        if f == '1':
            ret = [c] + s
        else:
            ret = s + [c]
    else:
        if f == '1':
            ret = s + [c]
        else:
            ret = [c] + s
    return ret


left, right = '', ''
rev = 1
for _ in range(Q):
    que = input().split()
    if que[0] == '1':
        rev *= -1
        continue
    if que[0] == '2':
        S = pop(S, rev, *que[1:])
print(''.join(S)[::rev])

### FIXED CODE ###

# coding: utf-8
S = input().split()
Q = int(input())

def pop(left, right, rev, f, c):
    if rev == 1:
        if f == '1':
            left.append(c)
        else:
            right.append(c)
    else:
        if f == '1':
            right.append(c)
        else:
            left.append(c)
    return left, right


left, right = [], []
rev = 1
for _ in range(Q):
    que = input().split()
    if que[0] == '1':
        rev *= -1
        continue
    if que[0] == '2':
        left, right = pop(left, right, rev, *que[1:])
print(''.join(left[::-1]+S+right)[::rev])