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


def main():
    N = int(input())

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

    # print(A,B,C)

    A.sort()
    B.sort()
    C.sort()

    cnt = 0
    for ai in A:
        for bi in B:
            if bi <= ai:
                continue
            for ci in C:
                if ci <= bi:
                    continue
                # print(ai, bi, ci)
                cnt += 1

    print(cnt)


if __name__ == "__main__":
    main()

### FIXED CODE ###

import bisect

def main():
    N = int(input())

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

    # print(A,B,C)

    A.sort()
    B.sort()
    C.sort()

    def get_a(b):
        p = bisect.bisect_left(A, b)
        return p

    def get_c(b):
        """ bより大きい数がいくつあるか
        """
        p = bisect.bisect_right(C, b)
        return N - p

    cnt = 0
    for bi in B:
        x = get_a(bi) * get_c(bi)
        cnt += x

    print(cnt)


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

# D - Not Divisible
N = int(input()) # 【1行】1数字
num_list = list(map(int, input().split())) 
ind_list = [1 for _ in range(N)]
num_list.sort()
for i in range(N-1):
    if num_list[i+1] == num_list[i]:
        ind_list[i] = -1
        continue
    for j in range(i):
        if num_list[i] % num_list[j] == 0 :
            ind_list[i] = -1
            break
for i in range(N-1):
    if num_list[N-1] % num_list[i] == 0 :
        ind_list[N-1] = -1
        break
print(ind_list.count(1))

### FIXED CODE ###

# D - Not Divisible
N = int(input())
a = list(map(int, input().split()))
a.sort()
MAX = a[-1]
l = [0]*(MAX+1)
for i in range(N):
    l[a[i]] += 1
    
answer = 0
for i in range(N):
    if l[a[i]] == 1:
        answer += 1
    for g in range(a[i], MAX+1, a[i]):
        l[g] = 10
print(answer)
### BUGGY CODE ###

N,K = map(int,input().split())
H = [int(x) for x in input().split()]
dp = [0]*N
for i in range(1,N):
	L = [dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)]
	dp[i] = min(L)
print(dp[-1])

### FIXED CODE ###

# -*- coding: utf-8 -*- 
def cost(h,N,K):
    dp=[0]*N
    dp[1]=abs(h[0]-h[1])
    for i in range(2,N):
        tmp=[abs(h[i]-h[j])+dp[j] for j in range(max(0,i-K),i)]
        dp[i]=min(tmp)
    return dp[-1]

N,K=map(int,input().split())
h=[int(num) for num in input().split()]

print(cost(h,N,K))
### BUGGY CODE ###

from functools import lru_cache


CHECK = False

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

MOD = 10 ** 9 + 7


@lru_cache(maxsize=None)
def modInverse(a, p):
    # Fermat's little theorem, a**(p-1) = 1 mod p
    return pow(a, p - 2, p)


fact = [1]
for i in range(1, 2 * N + 1):
    fact.append((fact[-1] * i) % MOD)


def nCr(n, r, p=MOD):
    # Modulo binomial coefficients
    return (fact[n] * modInverse(fact[r], p) * modInverse(fact[n - r], p)) % p


if N - 1 <= K:
    # Any combo is achievable so this is stars and bars
    # N people into N rooms, which means N - 1 dividers
    stars = N
    bars = N - 1
    print(nCr(stars + bars, bars))
else:
    # Group by number of empty rooms
    # With K moves there can only be at most K empty rooms
    # Max of N - 1 empty rooms in general
    assert K + 1 < N
    tot = 0
    comb1 = 1
    comb2 = 1
    for numEmpty in range(K + 1):
        way3 = comb1 * comb2
        if CHECK:
            numNonEmpty = N - numEmpty
            stars = numEmpty  # Everyone moving into an occupied room
            bars = numNonEmpty - 1  # Number of dividers
            way1 = nCr(stars + bars, bars) * nCr(N, numEmpty)

            way2 = nCr(N, numEmpty) * nCr(N - 1, numEmpty)

            assert way1 % MOD == way2 % MOD == way3 % MOD
        tot += way3

        j = numEmpty + 1
        comb1 = (N + 1 - j) * comb1 * modInverse(j, MOD) % MOD
        comb2 = (N - j) * comb2 * modInverse(j, MOD) % MOD
    print(tot % MOD)


### FIXED CODE ###

from functools import lru_cache


CHECK = False

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

MOD = 10 ** 9 + 7


@lru_cache(maxsize=None)
def modInverse(a, p):
    # Fermat's little theorem, a**(p-1) = 1 mod p
    return pow(a, p - 2, p)


if N - 1 <= K or CHECK:
    fact = [1]
    for i in range(1, 2 * N + 1):
        fact.append((fact[-1] * i) % MOD)

    def nCr(n, r, p=MOD):
        # Modulo binomial coefficients
        return (fact[n] * modInverse(fact[r], p) * modInverse(fact[n - r], p)) % p


if N - 1 <= K:
    # Any combo is achievable so this is stars and bars
    # N people into N rooms, which means N - 1 dividers
    stars = N
    bars = N - 1
    print(nCr(stars + bars, bars))
else:
    # Group by number of empty rooms
    # With K moves there can only be at most K empty rooms
    # Max of N - 1 empty rooms in general
    assert K + 1 < N
    tot = 0
    comb1 = 1
    comb2 = 1
    for numEmpty in range(K + 1):
        way3 = comb1 * comb2
        if CHECK:
            numNonEmpty = N - numEmpty
            stars = numEmpty  # Everyone moving into an occupied room
            bars = numNonEmpty - 1  # Number of dividers
            way1 = nCr(stars + bars, bars) * nCr(N, numEmpty)

            way2 = nCr(N, numEmpty) * nCr(N - 1, numEmpty)

            assert way1 % MOD == way2 % MOD == way3 % MOD
        tot += way3

        j = numEmpty + 1
        invJ = modInverse(j, MOD)
        comb1 = (N + 1 - j) * comb1 * invJ % MOD
        comb2 = (N - j) * comb2 * invJ % MOD
    print(tot % MOD)
### BUGGY CODE ###

X = int(input())
flag = 0
for a in range(0,1001):
  for b in range(0,1001):
    for c in range(0,1001):
      for d in range(0,1001):
        for e in range(0,1001):
          for f in range(0,1001):
            if 100*a+101*b+102*c+103*d+104*e+105*f == X:
              flag += 1
print(flag)

### FIXED CODE ###

X = int(input())
flag = 0
for C in range(X+1):
  if 100*C <= X and 105*C >= X:
    flag += 1
    break
print(flag)
### BUGGY CODE ###

n, m, q = map(int, input().split())
A, B, C, D = [], [], [], []
for i in range(q):
    a, b, c, d = map(int, input().split())
    A.append(a - 1)
    B.append(b - 1)
    C.append(c)
    D.append(d)

L = []

def f(l):
    if len(l) == n:
        L.append(l)
        return
    for i in range(l[-1], m + 1):
        f(l + [i])

for i in range(1, m + 1):
    f([1])

ans = 0

for l in L:
    ans = max(ans, sum(d for a, b, c, d in zip(A, B, C, D) if l[b] - l[a] == c))

print(ans)


### FIXED CODE ###

n, m, q = map(int, input().split())
A, B, C, D = [], [], [], []
for i in range(q):
    a, b, c, d = map(int, input().split())
    A.append(a - 1)
    B.append(b - 1)
    C.append(c)
    D.append(d)

L = []

def f(l):
    if len(l) == n:
        L.append(l)
        return
    for i in range(l[-1], m + 1):
        f(l + [i])

for i in range(1, m + 1):
    f([i])

ans = 0

for l in L:
    ans = max(ans, sum(d for a, b, c, d in zip(A, B, C, D) if l[b] - l[a] == c))

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

class Unionfind():  # Unionfind
    def __init__(self, N):
        self.N = N
        self.parents = [-1] * N

    def find(self, x):  # グループの根
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):  # グループの併合
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def members(self, x):  # グループの要素
        root = self.find(x)
        return [i for i in range(self.N) if self.find(i) == root]

    def roots(self):  # 根のリスト
        return [i for i, x in enumerate(self.parents) if x < 0]

    def all_group_members(self):  # グループごとの辞書
        return {r: self.members(r) for r in self.roots()}


n, m = map(int, input().split())
minusone = lambda x: int(x) - 1
p = list(map(minusone, input().split()))
xy = [list(map(minusone, input().split())) for _ in range(m)]

uf = Unionfind(n)
for x, y in xy:
    uf.union(x, y)

ans = 0
for lst in uf.all_group_members().values():
    ans += sum((p[num] in lst) for num in lst)
print(ans)


### FIXED CODE ###

class Unionfind():  # Unionfind
    def __init__(self, N):
        self.N = N
        self.parents = [-1] * N

    def find(self, x):  # グループの根
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):  # グループの併合
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def all_group_members(self):  # グループごとの辞書
        members_dict = {i: set([i]) for i, x in enumerate(self.parents) if x < 0}
        for i, x in enumerate(self.parents):
            if x >= 0:
                members_dict[self.find(x)].add(i)
        return members_dict


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

uf = Unionfind(n)
for x, y in xy:
    uf.union(x - 1, y - 1)

ans = 0
for lst in uf.all_group_members().values():
    ans += sum((p[num] - 1 in lst) for num in lst)
print(ans)
### BUGGY CODE ###

import sys
from collections import deque

input = sys.stdin.readline


def main():
    H, W = map(int, input().split())
    C_h, C_w = map(int, input().split())
    D_h, D_w = map(int, input().split())
    field = [list(input()) for _ in range(H)]
    direction = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    F = [[float('inf')] * W for _ in range(H)]
    queue = deque([(C_h - 1, C_w - 1)])
    nextqueue = deque([])
    t = 0
    alr = set()
    while queue:
        i, j = queue.popleft()
        if not (i, j) in alr:
            F[i][j] = min(t, F[i][j])
            if D_h == i + 1 and D_w == j + 1:
                break
            alr.add((i, j))
            for dx, dy in direction:
                nx, ny = i + dx, j + dy
                if 0 <= nx <= H - 1 and 0 <= ny <= W - 1:
                    if field[nx][ny] == '.':
                        queue.append((nx, ny))
            for p in range(i - 2, i + 3):
                for q in range(j - 2, j + 3):
                    if 0 <= p <= H - 1 and 0 <= q <= W - 1:
                        if field[p][q] == '.':
                            nextqueue.append((p, q))
                            F[p][q] = min(F[p][q], t + 1)
        if not queue:
            queue = nextqueue.copy()
            nextqueue.clear()
            t += 1

    if F[D_h-1][D_w-1] == float('inf'):
        print(-1)
    else:
        print(F[D_h - 1][D_w - 1])


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from collections import deque

input = sys.stdin.readline


def main():
    H, W = map(int, input().split())
    C_h, C_w = map(int, input().split())
    D_h, D_w = map(int, input().split())
    field = [list(input()) for _ in range(H)]
    direction = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    F = [[float('inf')] * W for _ in range(H)]
    queue = deque([(C_h - 1, C_w - 1)])
    nextqueue = deque([])
    t = 0
    alr = set()
    while queue:
        i, j = queue.popleft()
        if not (i, j) in alr:
            F[i][j] = min(t, F[i][j])
            if D_h == i + 1 and D_w == j + 1:
                break
            alr.add((i, j))
            for dx, dy in direction:
                nx, ny = i + dx, j + dy
                if 0 <= nx <= H - 1 and 0 <= ny <= W - 1:
                    if field[nx][ny] == '.':
                        queue.append((nx, ny))
            for p in range(i - 2, i + 3):
                for q in range(j - 2, j + 3):
                    if 0 <= p <= H - 1 and 0 <= q <= W - 1:
                        if field[p][q] == '.':
                            nextqueue.append((p, q))
                            F[p][q] = min(F[p][q], t + 1)
        if not queue:
            queue = nextqueue.copy()
            nextqueue.clear()
            t += 1

    if F[D_h-1][D_w-1] == float('inf'):
        print(-1)
    else:
        print(F[D_h - 1][D_w - 1])


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

def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r&1:
                data[r-2]=x
                r=(r-1)//2
            else:
                r//=2
            if l&1:
                data[l-1]=x
                l=(l+1)//2
            else:
                l//=2
    a=[]
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        a.append(-(s==I)or s)
    print('\n'.join(map(str,a)))
if __name__=='__main__':
    main()

### FIXED CODE ###

def main():
    from bisect import bisect_left
    def update(l,r,v):
        L,R=l+N0,r+N0
        while L<R:
            if R&1:
                R-=1
                data[R-1]=v
            if L&1:
                data[L-1]=v
                L+=1
            L>>=1
            R>>=1
    def query(k):
        k+=N0-1
        s=10**18
        while k+1:
            if data[k]<s:s=data[k]
            k=~-k//2
        return s
    n,*t=open(0)
    n,q=map(int,n.split())
    d=[-10**18]+list(map(int,t[n:]))+[10**18]
    N0=2**(q+1).bit_length()
    data=[10**18]*2*N0
    for x,t,s in sorted(list(map(int,t.split()))[::-1]for t in t[:n])[::-1]:
        l=bisect_left(d,s-x)
        r=bisect_left(d,t-x)
        update(l,r,x)
    for i in range(q):
        a=query(i+1)
        print(-(a==10**18)or a)
if __name__=='__main__':
    main()
### BUGGY CODE ###

N=int(input())
A=[]
B=[]
ans=0
for i in range(N):
  a=str(input())
  aa=[]
  for i in a:
    aa.append(i)
  aa.sort()
  aaa=str("0123456789")
  for i in range(len(aa)):
    aaa=aaa.replace(str(i),aa[i])
  if aaa in A:
    i=A.index(aaa)
    ans+=B[i]
    B[i]+=1
  else:
    A.append(aaa)
    B.append(1)
print(ans)
  

### FIXED CODE ###

N=int(input())
C=[]
dic={}
ans=0
for i in range(N):
  a=str(input())
  C.append(a)
for i in C:
  ab=[j for j in i]
  ab.sort()
  aaa="".join(ab)
  #print(aaa)
  if aaa in dic:
    ans+=dic[aaa]
    dic[aaa]+=1
  else:
    dic[aaa]=1
print(ans)
#print(dic)
### BUGGY CODE ###

n,m = list(map(int, input().split()))
x = list(map(int, input().split()))

if n >= m:
    print(0)
    exit()
else:
    difference = []
    x.sort()
 
    for i in range(m-1):
        difference.append(x[i+1]-x[i])
 
    for i in range(n-1):
        difference.remove(max(difference))
 
    print(sum(difference))


### FIXED CODE ###

n,m = list(map(int, input().split()))
x = list(map(int, input().split()))

if n >= m:
    print(0)
    exit()
else:
    difference = []
    x.sort()
 
    for i in range(m-1):
        difference.append(x[i+1]-x[i])
 
    difference.sort()
 
    print(sum(difference[:m-n]))
### BUGGY CODE ###

import re
N = int(input())
S = input()
for _ in range(N):
    S = re.sub("(.)\\1", "\\1", S)
print(len(S))  

### FIXED CODE ###

import re
N = int(input())
S = input()
while re.search("(.)\\1",S):
    S = re.sub("(.)\\1", "\\1", S)
print(len(S))
### BUGGY CODE ###

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

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

N, M = map(int, input().split())
S = input()
T = input()

lcm_num = lcm(N,M)

X_s = ["" for i in range(lcm_num)]
X_t = ["" for i in range(lcm_num)]

# print(X_s,X_t)

for i in range(N):
    tmp = i*lcm_num // N + 1
    X_s[tmp-1] = S[i]


for i in range(M):
    tmp = i*lcm_num // M + 1
    X_t[tmp-1] = T[i]

# print(X_s,X_t)

for s,t in zip(X_s,X_t):
    if s == "" or t == "" :
        continue
    elif s == t:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)


### FIXED CODE ###

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

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

N, M = map(int, input().split())
S = input()
T = input()

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)
### BUGGY CODE ###

N, M = map(int, input().split(' '))
A = []
B = []
for n in range(N):
  A_n, B_n = map(int, input().split(' '))
  A.append(A_n)
  B.append(B_n)
  
# calc answer
drink = 0
ans = 0
while(drink < M):
  min_i = A.index(min(A))
  if B[min_i] > 0:
    B[min_i] -= 1
    drink += 1
    ans += A[min_i]
  else:
    del A[min_i]
    del B[min_i]
  
print(ans)


### FIXED CODE ###

N, M = map(int, input().split(' '))
data = []
for n in range(N):
  A_n, B_n = map(int, input().split(' '))
  data.append([A_n,B_n])
data = sorted(data)
  
# calc answer
drink = 0
ans = 0
for d in data:
  a, b = d
  drink += b
  ans += a*b
  if drink > M:
    ans -= a*(drink-M)
    break
  
print(ans)
### BUGGY CODE ###

import math
def main():
    n, k = list(map(int, input().split()))
    A = list(map(int, input().split()))
    C = [set(range(n)) for _ in range(n)]
    I = set(range(n))
    if k >= math.log2(n):
        print(' '.join([n] * n))
    else:
        for _ in range(k):
            for i in I:
                rm = set()
                for j in C[i]:
                    if A[j] >= abs(i - j):
                        rm.add(j)
                C[i] -= rm
            if len(C[i]) == 0:
                I.remove(i)
            A = [n - len(C[i]) for i in range(n)]
        print(' '.join(list(map(str, A))))

if __name__ == '__main__':
    main()


### FIXED CODE ###

import numpy as np
from numba import njit
@njit
def op(A, n):
    B = np.zeros_like(A)
    for i, a in enumerate(A[:n]):
        B[max(i - a, 0)] += 1
        B[min(n - 1, a + i) + 1] -= 1
    return np.cumsum(B)

def main():
    n, k = list(map(int, input().split()))
    A = np.array(list(map(int, input().split())) + [0, ], dtype = np.int)
    for _ in range(k):
        A_new = op(A, n)
        if all(A[:n] == A_new[:n]):
            break
        A = A_new
    print(' '.join(list(map(str, A[:n]))))

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

def isort(a, n, g):
    global cnt
    for i in range(g, n):
        v = a[i]
        j = i - g
        while j >= 0 and a[j] > v:
            a[j + g] = a[j]
            j -= g
            cnt += 1
        a[j + g] = v

# input data
n = int(raw_input())
a = []
for i in range(n):
    a.append(int(raw_input()))

# determine m, g
g = [1]
m = 1
while True:
    x = 3 * g[m - 1] + 1
    if x >= n: break
    g.append(x)
    m += 1
g = g[::-1]

# 1 output m
print(m)
# 2 output g
print(' '.join(map(str, g)))

# sort it
cnt = 0
for i in range(m):
    isort(a, n, g[i])

# 3 output cnt
print(cnt)
# 4 output sorted a
print('\n'.join(map(str, a)))

### FIXED CODE ###

import sys
 
def isort(a, n, g):
    global cnt
    for i in range(g, n):
        v = a[i]
        j = i - g
        while j >= 0 and a[j] > v:
            a[j + g] = a[j]
            j -= g
            cnt += 1
        a[j + g] = v
 
# input data
n = int(sys.stdin.readline())
a = []
for i in range(n):
    a.append(int(sys.stdin.readline()))
 
# determine m, g
g = [1]
m = 1
while True:
    x = 3 * g[m - 1] + 1
    if x >= n: break
    g.append(x)
    m += 1
g = g[::-1]
 
# 1 output m
print(m)
# 2 output g
print(' '.join(map(str, g)))
 
# sort it
cnt = 0
for i in range(m):
    isort(a, n, g[i])
 
# 3 output cnt
print(cnt)
# 4 output sorted a
print('\n'.join(map(str, a)))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import math
n, a, b = map(int, input().split())

def pow(x, n):
  ans = 1
  while(n > 0):
    if(bin(n & 1) == bin(1)):
      ans = ans*x
    x = x*x
    n = n >> 1 #ビットシフト
  return ans

sum1 = pow(2, n)
sumwhole = sum1 -1

def framod(n, mod, a=1):
    for i in range(1,n+1):
        a=a * i % mod
    return a

def power(n, r, mod):
    if r == 0: return 1
    if r%2 == 0:
        return power(n*n % mod, r//2, mod) % mod
    if r%2 == 1:
        return n * power(n, r-1, mod) % mod

def comb(n, k, mod):
    a=framod(n, mod)
    b=framod(k, mod)
    c=framod(n-k, mod)
    return (a * power(b, mod-2, mod) * power(c, mod-2, mod)) % mod

p = comb(n,a,10**9+7)
q = comb(n,b,10**9+7)

sumf1 = sumwhole - p - q
sumf = sumf1 % (10**9+7)
print(sumf)


### FIXED CODE ###

def cmd(n, r, D):
    r = min(r, n-r)
    upper = 1
    lower = 1
    counter = 1
    while counter <= r:
        upper = (upper * (n - counter +1)) % D
        lower = (lower * counter) % D
        counter = counter + 1
    return (upper * pow(lower, D - 2, D)) % D

D = 10 ** 9 + 7
n, a, b = map(int, input().split())

N = pow(2, n, D)
P = cmd(n, a, D)
Q = cmd(n, b, D)
Ans = (N - 1 - P - Q) % D
print(Ans)
### BUGGY CODE ###

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

p = [ (p+1)/2 for p in ps]

pn_max = 0
for i in range(0, n-k+1):
  pn = 0
  for j in range(k):
  	pn += p[i+j]
  pn_max = max(pn_max, pn)

print(pn_max)

### FIXED CODE ###

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

ans = sum(p[:k])

psum = [0] * n
psum[0] = p[0]
for i in range(1, n):
  psum[i] = psum[i-1] + p[i]

anss = [0] * n
for i in range(n-k):
  anss[i] = psum[i+k] - psum[i]

print((max(ans, max(anss))+k)/2)
### BUGGY CODE ###

S = list(input())
len_size = len(S)

queue = []
cnt = 0

for i in range(len_size):

	# キューの更新
	for j in range(len(queue)):
		queue[j] += S[i]
	queue.append(S[i])

	# 2019で割れるかチェック
	for j in range(len(queue)):
		tmp_val = int(queue[j])
		if(tmp_val % 2019 == 0):
			cnt += 1
			queue[j] = ""

print(cnt)

### FIXED CODE ###

M = 2019

S = list(input())
s_size = len(S)

S.reverse()
x = 1
tot = 0
ans = 0

cnt_list = [0 for i in range(M)]

for i in range(s_size):
	cnt_list[tot] += 1
	tot += (ord(S[i])-ord('0'))*x
	tot %= M
	ans += cnt_list[tot]
	x = x*10%M

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

import math

n = int(raw_input())

print '{:d}'.format(math.factorial(n) % int(math.pow(10,9) + 7))

### FIXED CODE ###

import math
 
n = int(raw_input())
mod = pow(10,9) + 7
ans = 1
for i in xrange(1,n+1):
  ans = (ans*i)%mod
 
print ans
### BUGGY CODE ###

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

for _ in range(Q):
  q = list(input())
  if q[0] == "1":
    S = S[::-1]
  else:
    if q[2] == "1": S.insert(0, q[4])
    else: S.append(q[4])

print("".join(S)) 

### FIXED CODE ###

from collections import deque
S = deque(input())
Q = int(input())
q = [list(input().split()) for _ in range(Q)]

ok = True #Falseで反転させる
for i in range(Q):
    l = q[i]
    if l[0] == "1":
        if ok: ok = False
        else: ok = True
    else:
        if ok:
            if l[1] == "1": S.appendleft(l[2])
            else: S.append(l[2])
        else:
            if l[1] == "1": S.append(l[2])
            else: S.appendleft(l[2])
if not ok:
    S = list(S)[::-1]
print("".join(list(S)))