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

import math
n,m,k=map(int,input().split())
mod=10**9+7
s=math.factorial(n*m-2)//(math.factorial(n*m-k)*math.factorial(k-2))
cnt=0
s%=mod
a=(m*m*s)%mod
for d in range(1,n):
        cnt+=d*(n-d)*a
        cnt%=mod
a=(n*n*s)%mod
for d in range(1,m):
        cnt+=d*(m-d)*a
        cnt%=mod
print(cnt)

### FIXED CODE ###

import math
n,m,k=map(int,input().split())
mod=10**9+7
s=math.factorial(n*m-2)//(math.factorial(n*m-k)*math.factorial(k-2))
cnt=0
s%=mod
a=(m*m*s)%mod
for d in range(1,n):
        cnt+=d*(n-d)*a
        cnt%=mod
a=(n*n*s)%mod
for d in range(1,m):
        cnt+=d*(m-d)*a
        cnt%=mod
print(cnt)
### BUGGY CODE ###

from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

import numpy as np

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

D = 20

N = read()
A = reads()

freq = [None] * (N+1)
freq[0] = np.zeros(D, dtype=int)
for i in range(N):
  freq[i+1] = freq[i] + np.asarray([A[i] >> d & 1 for d in range(D)])

r = 0
count = 0
for l in range(N):
  while r < N and np.all(freq[r+1] - freq[l] <= np.ones(D)):
    r += 1
  count += r - l
print(count)


### FIXED CODE ###

from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

D = 20

N = read()
A = reads()

psum = [[0]*(N+1) for _ in range(D)]
for d in range(D):
  for i in range(N):
    psum[d][i+1] = psum[d][i] + (A[i] >> d & 1)

r = 0
count = 0
for l in range(N):
  while r < N and all(psum[d][r+1] - psum[d][l] <= 1 for d in range(D)):
    r += 1
  count += r - l
print(count)
### BUGGY CODE ###

def main():
    from itertools import product
    import os

    INF = 10 ** 9 + 10

    D = int(input())
    *C, = map(int, input().split())
    # C[type]
    # sum(ci*(d-last(d,i) for ci in C)だけ下がる
    S = [[int(x) for x in input().split()] for _ in range(D)]
    # S[day][type]:=happiness

    last = [-1] * 26

    def select(d):
        s = -1
        ret = -1
        for prd in product(range(26), repeat=min(D - d, 3)):
            score = 0
            old = []
            for k, t in enumerate(prd):
                score += S[d + k][t]
                score -= sum(ci * (d + k - last[i]) for i, ci in enumerate(C) if i != t)
                old.append((t, last[t]))
                last[t] = d + k
            old.reverse()
            for t_, d_ in old:
                last[t_] = d_
            if s < score:
                s = score
                ret = prd[0]
        return ret

    T = []
    for d in range(D):
        t = select(d)
        last[t] = d
        T.append(t + 1)
    print(*T, sep='\n')
    # print(os.path.join(os.path.dirname(__file__), 'output_file.txt'))
    # with open(file=os.path.join(os.path.dirname(__file__), 'output_file.txt'), mode='w', encoding='utf-8') as f:
    #     for t in map(str, T):
    #         f.write(t)
    #         f.write('\n')


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    INF = 10 ** 9 + 10

    D = int(input())
    *C, = map(int, input().split())
    # C[type]
    # sum(ci*(d-last(d,i) for ci in C)だけ下がる
    S = [[int(x) for x in input().split()] for _ in range(D)]
    # S[day][type]:=happiness

    last = [-1] * 26

    def diff(d, t):
        ret = S[d][t]
        ret -= sum(ci * (d - last[i]) for i, ci in enumerate(C) if i != t)
        return ret

    T = []
    score = 0
    for d in range(D):
        delta = -INF
        take_t = -1
        for t_ in range(26):
            buf = diff(d, t_)
            if delta < buf:
                delta = buf
                take_t = t_

        last[take_t] = d
        score += buf
        T.append(take_t + 1)
    print(*T, sep='\n')
    # print(score)


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

from collections import defaultdict
N,M,*A = map(int, open(0).read().split())
ls = [0]*10
ls[1] = 2
ls[2] = 5
ls[3] = 5
ls[4] = 4
ls[5] = 5
ls[6] = 6
ls[7] = 3
ls[8] = 7
ls[9] = 6
dic = defaultdict(int)
for a in A:
  k = ls[a]
  dic[k] = max(dic[k],a)
dp = [-1]*(N+1)
for k in dic.keys():
  if k>N:
    continue
  dp[k] = dic[k]
for i in range(N+1):
  for k in dic.keys():
    if i-k>0 and dp[i-k]!=-1:
      s = str(dp[i-k])
      m = int(s+str(dic[k]))
      dp[i] = max(dp[i], m)
print(dp[N])

### FIXED CODE ###

n,m=map(int,input().split())
A=list(map(int,input().split()))
L=[0,2,5,5,4,5,6,3,7,6]
dp=[0]+[-1]*n
for i in range(n+1):
  for a in A:
    if i+L[a]<=n:
      dp[i+L[a]]=max(dp[i+L[a]],dp[i]*10+a)
print(dp[n])
### BUGGY CODE ###

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

if k > n:
    k = n-1

'''
n番目の場所に到達する時
n-1番目から、1つ飛んでくる
n-2番目から、２つ飛んでくる
...
n-K番目から、kつ飛んでくる

可能性がある　その時の最小値を考える

初期条件
dp[0] = 0
dp[1~Kまで]を計算

*** n-j
dp[n] = dp[n-j] + abs(h[n] - h[n-j])

漸化式
n >= 2
dp[n] = min(jump_list)

1~K
jump_list = [1 ~ j で dp[n] = dp[n-j] + abs(h[n] - h[n-j])]

k~N
jump_list = [1 ~ k で dp[n] = dp[n-j] + abs(h[n] - h[n-j])]

'''

#初期値
dp = [0]

def create_jump_list(j,i):
    alist = []
    for x in range(j, i):
        alist.append(
            dp[x] + abs(h[i] - h[x])
        )
    return alist

# kまで
for i in range(1, k+1):
    jump_list = create_jump_list(0,i)
    dp.append(
        min(jump_list)
    )

for i in range(k+1,n):
    jump_list = create_jump_list(i-k, i)
    dp.append(
        min(jump_list)
    )

#print(dp)
print(dp[n-1])


### FIXED CODE ###

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

if k > n:
    k = n-1

'''
n番目の場所に到達する時
n-1番目から、1つ飛んでくる
n-2番目から、２つ飛んでくる
...
n-K番目から、kつ飛んでくる

可能性がある　その時の最小値を考える

初期条件
dp[0] = 0
dp[1~Kまで]を計算

*** n-j
dp[n] = dp[n-j] + abs(h[n] - h[n-j])

漸化式
n >= 2
dp[n] = min(jump_list)

1~K
jump_list = [1 ~ j で dp[n] = dp[n-j] + abs(h[n] - h[n-j])]

k~N
jump_list = [j-k ~ j で dp[n] = dp[n-j] + abs(h[n] - h[n-j])]

'''

#初期値
dp = [0]

def create_jump_list(j,i):
    alist = []
    for x in range(j, i):
        alist.append(
            dp[x] + abs(h[i] - h[x])
        )
    return alist

# kまで
for i in range(1, k+1):
    jump_list = create_jump_list(0,i)
    dp.append(
        min(jump_list)
    )

for i in range(k+1,n):
    jump_list = create_jump_list(i-k, i)
    dp.append(
        min(jump_list)
    )
print(dp[n-1])
### BUGGY CODE ###

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

[n,m] = [int(i) for i in input().split()]
if n == 1:
    print(m)
else:
    ans = -1
    #a1は高々この値
    a1_max = m // n
    for i in range(a1_max,0,-1):
        ls = make_divisors(i)
        if (m - i) % ls[-1] == 0 and (m - i)//ls[-1] >= (n-1):
            ans = max(ans,ls[-1])
    print(ans)


### FIXED CODE ###

[n,m] = [int(i) for i in input().split()]
if n == 1:
    print(m)
else:
    ans = -1
    #a1は高々この値
    a1_max = m // n
    for i in range(a1_max,0,-1):
        if (m - i) % i == 0 and (m - i)//i >= (n-1):
            ans = max(ans,i)
    print(ans)
### BUGGY CODE ###

S = input()

N = len(S)
l = [int(x) for x in list(str(S))]

count = 0
idx = 0
len_S = 0

while True:
    if len(l) <= 1:
        break
    if (l[idx] == 0 and l[idx+1] == 1) or (l[idx] == 1 and l[idx+1] == 0):
        l.pop(idx)
        l.pop(idx)
        count += 2
        idx = 0
        if len(set(l)) == 1:
            break
        continue
    if idx+1 == len(l)-1:
        break
    idx += 1

print(count)

### FIXED CODE ###

S = input()

l0 = 0
l1 = 0

for i in S:
    if i == '0':
        l0 += 1
    elif i == '1':
        l1 += 1

ans = min(l0, l1) * 2

print(ans)
### BUGGY CODE ###

N, P = map(int, input().split())
S = input()

if P==2:
    counter = 0
    for i in range(len(S)):
        if S[i] in ['2','4','6','8','0']:
            counter+=i+1
    print(counter)
elif P==5:
    counter = 0
    for i in range(len(S)):
        if S[i] in ['5','0']:
            counter+=i+1
    print(counter)
else:
    list_a=[]
    for i in range(len(S)):
        list_a.append(int(S[i-1:])%P)
    
    list_a.append(0)
    counter=0
    for i in range(len(list_a)-1):
        counter+=list_a[i+1:].count(list_a[i])
    
    print(counter)

### FIXED CODE ###

N, P = map(int, input().split())
S = input()

counter=0
def power_func(a,b,p):
    if b==0:
        return 1
    elif b%2==0:
        d=power_func(a,b//2,p)
        return d*d %p
    elif b%2==1:
        return (a*power_func(a,b-1,p ))%p
if P==2:
    counter = 0
    for i in range(len(S)):
        if S[i] in ['2','4','6','8','0']:
            counter+=i+1
elif P==5:
    counter = 0
    for i in range(len(S)):
        if S[i] in ['5','0']:
            counter+=i+1
else:
    list_a=[0]*P
    list_a[0]+=1
    temp=0
    for i in range(N):
        M = power_func(10,N-i,P)
        temp=(int(S[i])*M+temp)%P
        counter+=list_a[temp]
        list_a[temp]+=1
print(counter)
### BUGGY CODE ###


N,K = map(int,input().split())

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

dic = {}

for i in A:

    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

lis = []

for i in dic:

    lis.append(dic[i])

lis.sort()

ans = 0

"""
num = len(lis) - K
if num < 0:
    num = 0

for i in range(num):
    ans += lis[i]
"""
lo = len(lis)

while lo > K:
 
    ans += lis[0]
    del lis[0]
    lo = len(lis)

print (ans)


### FIXED CODE ###


N,K = map(int,input().split())

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

dic = {}

for i in A:

    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

lis = []

for i in dic:

    lis.append(dic[i])

lis.sort()

ans = 0

num = len(lis) - K
if num < 0:
    num = 0

for i in range(num):
    ans += lis[i]

print (ans)
### BUGGY CODE ###

from itertools import accumulate
n,m = [int(a) for a in input().strip().split(' ')]
As = accumulate([int(a)%m for a in input().strip().split(' ')])
As = [0] + [i%m for i in As]

def isok(i,j):
    di = As[j] - As[i-1]
    if di % m == 0:
        return True
    else:
        return False


counter = 0
for i in range(n):
    for j in range(i, n):
        if isok(i+1, j+1):
            counter += 1
print(counter)


### FIXED CODE ###

from itertools import accumulate
n,m = [int(a) for a in input().strip().split(' ')]
As = accumulate([int(a)%m for a in input().strip().split(' ')])
As = [0] + [i%m for i in As]
As = sorted(As)
count = 0
ba = None
Bs = []
for a in As:
    if a == ba:
        count += 1
    elif ba != None:
        Bs.append(count*(count+1))
        count = 0
    ba = a
if count != 0:
    Bs.append(count*(count+1))
print(int(sum(Bs)/2))
### BUGGY CODE ###

import sys

mod = 10**9 + 7


def extgcd(a, b):
    r = [1, 0, a]
    w = [0, 1, b]
    while w[2] != 1:
        q = r[2] // w[2]
        r2 = w
        w2 = [r[0] - q * w[0], r[1] - q * w[1], r[2] - q * w[2]]
        r = r2
        w = w2
    return [w[0], w[1]]


def mod_inv(a, m):
    x = extgcd(a, m)[0]
    return (m + x % m) % m


def Cmb(n, k):
    res = 1
    for i in range(1, n + 1):
        res = res * i % mod
    for i in range(1, k + 1):
        res = res * mod_inv(i, mod) % mod
    for i in range(1, n - k + 1):
        res = res * mod_inv(i, mod) % mod
    return res


n, m = list(map(int, input().split()))
a = [int(input()) for _ in range(m)]
l = len(a)

if 1 in [a[i + 1] - a[i] for i in range(l - 1)]:
    print(0)
    sys.exit()

b = [-1] + a + [n + 1]
c = []
for bi, bi2 in zip(b[:-1], b[1:]):
    c.append((bi2 - 1) - (bi + 1))  # width 2 を取れる数


def count(n):
    ret = 0
    for i in range(n // 2 + 1):
        ret += Cmb(n - i, i) % mod
    return ret


ans = 1
for ci in c:
    ans *= count(ci) % mod
    ans %= mod

print(ans)


### FIXED CODE ###

mod = 10**9 + 7

n, m = list(map(int, input().split()))
a = set([int(input()) for _ in range(m)])

dp = {}
for i in range(n + 1):
    dp[i] = 1
dp[1] = 0 if 1 in a else 1
for i in range(2, n + 1):
    if i in a:
        dp[i] = 0
    else:
        dp[i] = dp[i - 1] % mod + dp[i - 2] % mod
        dp[i] %= mod

print(dp[n] % mod)
### BUGGY CODE ###

import string
import collections

n = int(input())

li = []
ls = []
cnt = 0
res = [[list(i) for i in string.ascii_lowercase] for _ in range(n)]
# print(res)
r = [[] for _ in range(n)]
rs = ["" for _ in range(n)]

for i in range(n):
    s = str(input())
    sl = list(s)
    #  print(s)
    #  print(sl)
    li.append(sl)
    # for j in range(26):
    #     res[i][j].append(0)

# print(res)

for i in range(n):
    coll = collections.Counter(li[i])
    r[i].extend(coll.most_common())
    r[i].sort()
    rs[i] = ''.join(str(s) for s in r[i])

collr = collections.Counter(rs)

# print(collr)
# print(collr.values())
# print(sum(collr.values()))

for i in collr.values():
    cnt += i * (i-1) / 2

print(int(cnt))
        # print(res[i])
#     if not res[i] in dic:
#         dicdic[res[i]] = 0
#     else:
#         pass

# for m in range(n):
#   for l in range(n):
#     if l <= m:
#       continue
#     else:
#       if res[m] == res[l]:
#         cnt += 1

# print(cnt)

### FIXED CODE ###

n = int(input())

l = []
cnt = 0
t = 0

for i in range(n):
    s = str(input())
    sl = ''.join(sorted(s))
    l.append(sl)

l.sort()

tmp = l[0]

for i in range(1, n):
  if tmp == l[i]:
    cnt += 1
  else:
    tmp = l[i]
    t += int(cnt * (cnt + 1) / 2)
    cnt = 0
else:
    t += int(cnt * (cnt + 1) / 2)

print(t)
### BUGGY CODE ###

import collections
N=int(input())
list1 = list(map(int, input().split()))
c = collections.Counter(list1)
for i in range(N):
    a=0
    for j in range(1,N+1):
        if j==list1[i]:
            a+=int((c[j]-1)*(c[j]-2)/2)
        else:
            a+=int(c[j]*(c[j]-1)/2)
    print(a)

### FIXED CODE ###

import collections
N=int(input())
a=0
list1 = list(map(int, input().split()))
c = collections.Counter(list1)
for i in range(N):
    a+=int(c[i+1]*(c[i+1]-1)/2)
for i in range(N):
    if c[list1[i]]!=0:
        print(a-c[list1[i]]+1)
    else:
        print(a)
### BUGGY CODE ###

#bit全探索する
INF = 5000

def dp(n, medichine, Ma, Mb):
    INF =  5000
    if n == 0: #終端
        List = [[0, 0, 0], medichine[0]]
        if medichine[0][0] * Mb == medichine[0][1] * Ma:
            cost = medichine[0][2]
        else:
            cost = INF
    else: #n-1をもとにnについて計算
        previous = dp(n-1, medichine, Ma, Mb)
        List = previous[0].copy()
        cost = previous[1]
        for x in previous[0]:
            C = x[2] + medichine[n][2]
            if C < cost:
                A = x[0] + medichine[n][0]
                B = x[1] + medichine[n][1]
                List.append([A, B, C])
                if A * Mb == B * Ma:
                    cost = C         
    return List, cost



N, Ma, Mb = map(int, input().split())
medichine = []
for i in range(N):
    Lst = [int(x) for x in input().split()]
    medichine.append(Lst)
ans = dp(N-1, medichine, Ma, Mb)
cost = ans[1]
if cost != INF:
    print(cost)
else:
    print(-1)

### FIXED CODE ###

import collections
import copy
INF = 10000

#入力
N, Ma, Mb = map(int, input().split())
medichine = [tuple(map(int, input().split())) for _ in range(N)]

dp = {}
#dp初期化
dp = collections.defaultdict(lambda:INF)
dp[0,0] = 0

for a, b, c in medichine:
    dp_ = copy.copy(dp)
    for ca,cb in dp.keys():
        dp_[ca+a, cb+b] = min(dp_[ca+a, cb+b], dp[ca,cb]+c)
    dp = dp_    

ans = INF
for ca,cb in dp.keys():
    if ca == cb == 0: continue
    if ca*Mb == cb*Ma:
        ans = min(ans, dp[ca,cb])
        
print(-1 if ans == INF else ans)
### BUGGY CODE ###

import copy
import math
 
d,g = [int(i) for i in input().split()]
P = []
C = []
for i in range(d):
    p,c = [int(i) for i in input().split()]
    P.append(p)
    C.append(c)
 
m = 10**9
def score(point = 0, n = 0, now=0, done=set()):
    global m
    if point >= g:
        if m > n: m = n
    elif point+100*(now+1)*P[now] >= g:
        n += math.ceil((g-point)/(100*(now+1)))
        if m > n: 
            m = n
    else:
        done.add(now)
        for i in range(d):
            if not i in done:
                score(point+P[now]*100*(now+1) + C[now], n + P[now], i, copy.copy(done))
 
for i in range(d):
    done = set()
    score(0,0,i,done)
print(m)

### FIXED CODE ###

import math
 
d,g = [int(i) for i in input().split()]
P = []
C = []
for i in range(d):
    p,c = [int(i) for i in input().split()]
    P.append(p)
    C.append(c)

# cnt = 0
# def count():
#     global cnt
#     cnt+=1
#     print(cnt)

def flag(point,P,now):
    if point+100*(now+1)*(P-1) >= g: return 1
    elif point+100*(now+1)*P + C[now] >= g: return 2
    else: return 0

m = 10**9
def score(point = 0, n = 0, now=-1, remain = list(range(d))):
    global m
    if m <= n: return
    elif now == -1:
        for i in remain:
            score(point, n, i, remain)
    elif point >= g: m = n
    elif flag(point,P[now],now) == 1:
        n += math.ceil((g-point)/(100*(now+1)))
        if m > n: m = n
    elif flag(point,P[now],now) == 2:
        n += P[now]
        if m > n: m = n
    else:
        if m <= n+P[now]: return
        else:
            remain = remain[:]
            remain.remove(now)
            nxtp = point + P[now]*100*(now+1) + C[now]
            for i in remain:
                score(nxtp, n + P[now], i, remain)

score()
print(m)
### BUGGY CODE ###

from sys import stdin
from collections import defaultdict as dd
from collections import deque as dq
import itertools as it
from math import sqrt, log, log2
from fractions import Fraction

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

count = 0
while count < k:
    totsum = 0
    prefs = [0]*n
    zeros = []
    for i in range(n):
        if intensity[i]!=0:
            l = i - intensity[i]
            r = i + intensity[i] + 1

            prefs[max(l, 0)] += 1
            if r <= n-1: 
                prefs[r] += -1
        else:
            zeros.append(i)
            
    for i in range(1, n):
        prefs[i] += prefs[i-1]
        totsum += prefs[i]
    
    for i in zeros:
        if intensity[i] == 0:
            prefs[i] += 1
            totsum += 1
    
    if totsum == n*k:
        print(*prefs)
        exit()
    
    intensity = prefs
    count += 1

print(*prefs)

### FIXED CODE ###

from sys import stdin
# from collections import defaultdict as dd
# from collections import deque as dq
# import itertools as it
# from math import sqrt, log, log2
# from fractions import Fraction

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

count = 0
while count < k:
    totsum = 0
    prefs = [0]*n
    zeros = []
    for i in range(n):
        l = i - intensity[i]
        r = i + intensity[i] + 1

        prefs[max(l, 0)] += 1
        if r <= n-1: 
            prefs[r] += -1
            
    for i in range(1, n):
        prefs[i] += prefs[i-1]
        totsum += prefs[i]
    
    if sum(prefs) == n*n:
        print(*prefs)
        exit()
    
    intensity = prefs
    count += 1

print(*prefs)
### BUGGY CODE ###

import math

K =int(input())
sum = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            d = math.gcd(a,b)
            sum += math.gcd(c,d)
print(sum)



### FIXED CODE ###

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd,numbers)

K =int(input())
sum = 0

for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            sum += gcd(a,b,c)
print(sum)
### BUGGY CODE ###

K=int(input())

if K%2==0:
    print(-1)
    exit()


now=0
count=0
N=0
flag=False
while True:
    now+=(7*pow(10,count, K))%K
    now%=K
    #print(N)
    if not flag:
        N+=7*10**(count)
    if N>=K:
        flag=True
    #print(now, count)
    if now==0 and flag:
        print(count+1)
        exit()
    count+=1


print(-1)

### FIXED CODE ###

K=int(input())

if K%2==0:
    print(-1)
    exit()


now=0
count=0
N=0
flag=False
while True:
    now+=(7*pow(10,count, K))%K
    now%=K
    #print(N)
    if not flag:
        N+=7*10**(count)
    if N>=K:
        flag=True
    #print(now, count)
    if now==0 and flag:
        print(count+1)
        exit()
    count+=1
    if count>10**6:
        break


print(-1)
### BUGGY CODE ###

import sys
import numpy as np
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

B = 0
s = 1
for i, a in enumerate(A):
    if i < K:
        s *= a
        B = s
    else:
        s /= A[i-K]
        s *= a
        if B != 0 and B < s:
            print('Yes')
        elif B != 0 and B >= s:
            print('No')
        B = s


### FIXED CODE ###

import sys
import numpy as np
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

B = 0
s = 1
for i, a in enumerate(A):
    if i >= K:
        B = A[i-K]
        s = a
        if B != 0 and B < s:
            print('Yes')
        elif B != 0 and B >= s:
            print('No')
### BUGGY CODE ###

#def gcd(a, b):
#    while b:
#        a, b = b, a % b
#    return a
#
#def lcm(a, b):
#    return a * b // gcd (a, b)

import fractions
from functools import reduce
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

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


N,M = map(int, input().split())
xs = list(map(int, input().split()))
b = None
for x in xs:
    for i in range(30):
        if x%(2**i):
            break
    if b!=None:
        if b!=i:
            b = -1
            break
    b = i
if b==-1:
    print(0)
else:
    r = lcm_list(xs)
    if r//2 > M:
        print(0)
    else:
        print((M-r//2)//r+1)


### FIXED CODE ###

#def gcd(a, b):
#    while b:
#        a, b = b, a % b
#    return a
#
#def lcm(a, b):
#    return a * b // gcd (a, b)

import fractions
from functools import reduce
def lcm_base(x, y):
    r = (x * y) // fractions.gcd(x, y)
    if r > 2*M:
        return 0
    return r

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


N,M = map(int, input().split())
xs = list(map(int, input().split()))
b = None
for x in xs:
    for i in range(30):
        if x%(2**i):
            break
    if b!=None:
        if b!=i:
            b = -1
            break
    b = i
if b==-1:
    print(0)
else:
    r = lcm_list(xs)
    if r//2 > M:
        print(0)
    elif r == 0:
        print(0)
    else:
        print((M-r//2)//r+1)
### BUGGY CODE ###

def resolve():
    N=int(input())
    A=[int(input()) for i in range(N)]
    temp=[0]*N

    for i in range(N):
        if A[i]<max(A):
            temp[i]=max(A)
        elif A.count(max(A))!=1:
            temp[i]=max(A)
        else:
            temp[i]=max(A[:i]+A[i+1:])
    for i in temp:
        print(i)



resolve()

### FIXED CODE ###

# import bisect
# import copy
# import fractions
# import math
# import numpy as np
#from collections import Counter, deque
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product

def resolve():
    N=int(input())
    A=[int(input()) for i in range(N)]
    B=sorted(A,reverse=True)

    for i in range(N):
        if A[i]<B[0]:
            print(B[0])
        else:
            print(B[1])



resolve()
### BUGGY CODE ###

from collections import deque
s=input()
q=int(input())
ans=deque(s)
for _ in range(q):
  que=input()
  if que[0]=='1':
    ans.reverse()
  else:
    t,f,c=map(str,que.split())
    if f=='1':
      ans.appendleft(c)
    else:
      ans.append(c)
      
print(''.join(ans))




### FIXED CODE ###

def main():
  from collections import deque
  s=input()
  q=int(input())
  turn = 2
  d=deque(s)

  for _ in [0]*q:
      query = input()
      if query[0] == "1":
          t = int(query)
          turn = 3 - turn
      else:
          t,f,c = map(str,query.split())
          if (int(f) + turn)% 2  == 0:
              d.append(c)
          else:
              d.appendleft(c)
  if turn == 2:
      print("".join(d))
  else:
      print("".join(list(d)[::-1]))
main()