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

from sys import stdin

N = int(stdin.readline().rstrip())
Ss = []
Sd = {}
ans = 0

for i in range(N):
    s = sorted([x for x in stdin.readline().rstrip()])
    ss = ("".join(s))
    if ss in Ss:
        ans += Sd[ss]
        Sd[ss] += 1
    else:
        Ss.append(ss)
        Sd[ss] = 1

print(ans)

### FIXED CODE ###

from sys import stdin

N = int(stdin.readline().rstrip())
S = []
ans = 0
count = 0

for i in range(N):
    line = sorted([x for x in stdin.readline().rstrip()])
    line = ("".join(line))
    S.append(line)

S.sort()
for i in range(N):
    if (i == 0) or (S[i] == S[i - 1]):
        count += 1
    else:
        ans += count*(count - 1)//2
        count = 1
ans += count*(count - 1)//2

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

import itertools

nmr = list(map(int,input().split()))
N,M,R = nmr[0],nmr[1],nmr[2]
perm = list(map(int,input().split()))
perm.sort()
dist = [[int(1e9) for i in range(N+1)] for j in range(N+1)]
res = int(1e11)

# 各距離を代入する
for i in range(M):
    abc = list(map(int,input().split()))
    a,b,c = abc[0],abc[1],abc[2]
    dist[a][b] = c
    dist[b][a] = c

# Warshall–Floyd Algorithm
for k in range(1,N+1):
    for i in range(1, N+1):
        for j in range(1,N+1):
            if(dist[i][j] > dist[i][k] + dist[k][j]):
                dist[i][j] = dist[i][k] + dist[k][j]

# 各順列について,距離の総和を求める
for dir in itertools.permutations(perm):
    direction = list(dir)
    tmpdir = 0
    for i in range(len(direction) - 1):
        tmpdir += dist[direction[i]][direction[i+1]]
    res = min(tmpdir,res)

print(res)

### FIXED CODE ###

import itertools

nmr = list(map(int,input().split()))
N,M,R = nmr[0],nmr[1],nmr[2]
perm = list(map(int,input().split()))
perm.sort()
dist = [[int(1e9) for i in range(N+1)] for j in range(N+1)]
res = int(1e11)

# 各距離を代入する
for i in range(M):
    abc = list(map(int,input().split()))
    a,b,c = abc[0],abc[1],abc[2]
    dist[a][b] = c
    dist[b][a] = c

# Warshall–Floyd Algorithm
for k in range(1,N+1):
    for i in range(1, N+1):
        for j in range(1,N+1):
            if(dist[i][j] > dist[i][k] + dist[k][j]):
                dist[i][j] = dist[i][k] + dist[k][j]

# 各順列について,距離の総和を求める
for dir in itertools.permutations(perm):
    direction = list(dir)
    tmpdir = 0
    for i in range(len(direction) - 1):
        tmpdir += dist[direction[i]][direction[i+1]]
    res = min(tmpdir,res)

print(res)
### BUGGY CODE ###

# li = [[0] * 4 for i in range(3)]

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

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

S = input()


memo = {}


def dfs(n):
    if n in memo:
        return memo[n]

    i = n + 1
    while i <= N:
        temp_int = int(S[n:i])
        if temp_int % P == 0:
            if temp_int == 0:
                continue

            memo[n] = 1 + dfs(i)
            return memo[n]

        i += 1
    return 0


ans = 0
for i in range(N):
    temp = dfs(i)
    ans += temp

print(ans)



### FIXED CODE ###

N, P = map(int, input().split())
S = input()
now = 0
hyou = [0] * P
hyou[0] = 1
cnt = 1
ans = 0

if P == 2 or P == 5:
    for i, t in enumerate(S[::-1]):
        temp = int(t)
        if temp % P == 0:

            ans += N - i
    print(ans)
    exit()

for i, t in enumerate(S[::-1]):
    now = (now + int(t) * pow(10, i, P)) % P
    ans += hyou[now]
    hyou[now] += 1

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

#ABC C121
import sys
n, m = map(int, sys.stdin.readline().split())
L = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]

L = sorted(L, key=lambda x:x[0])
total=0
for a, b in L:
    if m-b>=0:
        m-=b
        total+=a*b
        if m==0:
            print(total)
            break
    else:
        for i in range(b):
            total +=a
            m -=1
            if m==0:
                print(total)
                break


### FIXED CODE ###

#ABC C121
import sys
n, m = map(int, sys.stdin.readline().split())
L = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]

L = sorted(L, key=lambda x:x[0])
total=0
for a, b in L:
    if m-b>=0:
        m-=b
        total+=a*b
        if m==0:
            print(total)
            break
    else:
        total +=a*m
        print(total)
        break
### BUGGY CODE ###

s = input()
q = int(input())
for i in range(q):
    temp = input().split()
    t = temp[0]
    if t == "1":
        s = s[::-1]
    else:
        f = temp[1]
        c = temp[2]
        if f == "1":
            s = c + s
        else:
            s = s + c
print(s)

### FIXED CODE ###

from collections import deque
def main():
    s = deque(input())
    q = int(input())
    appendleft=s.appendleft
    append=s.append
    flag = False
    for i in range(q):
        temp = input().split()
        if temp[0] == "1":
            flag = not flag
        else:
            if temp[1] == "1":
                if flag:
                    append(temp[2])
                else:
                    appendleft(temp[2])
            else:
                if flag:
                    appendleft(temp[2])
                else:
                    append(temp[2])
    if flag:
        s.reverse()
    print("".join(list(s)))
if __name__=="__main__":
    main()
### BUGGY CODE ###

count=0
z=0
k,s=map(int,input().split())
for i in range(k+1):
    for j in range(k+1):
        x=i
        y=j
        z=-i-j+s
        if k>=z>=0:
            count+=1
print(count)

### FIXED CODE ###

count=0
z=0
k,s=map(int,input().split())
for i in range(k+1):
    for j in range(k+1):
        x=i
        y=j
        z=-i-j+s
        if k>=z>=0:
            count+=1
print(count)
### BUGGY CODE ###

n =int(input())

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

result = False

if len(set(a)) == int((n+1)/2) and sum(a)==int(n/2)*int((n+1)/2)*2:
    for i in range(n-1, -1, -2):
        if i not in set(a):
            result = False
            break
    result = True

if result:print(2**int(n/2) % (10**9 + 7))
else:
    print(0)

### FIXED CODE ###

from  collections import Counter

n =int(input())

a = list(input().rstrip().split())

count_num = Counter(a)

result = True

if len(a) == n:
    if n % 2 == 0:
        for i in range(n-1, -1, -2):
            if count_num[str(i)] ==2:
                continue
            else:
                result = False
                break

    else:
        if count_num['0'] == 1:
            for i in range(n-1, 1, -2):                
                if count_num[str(i)] ==2:continue
                else:
                    result = False
                    break
        else:result=False

if result: print(2**int(n/2) % (10**9+7))
else:print(0)
### BUGGY CODE ###

L,R=map(int,input().split())

keep=L*R
for i in range(L,R+1):
  for j in range(i+1,R+1):
    mod=(i*j) % 2019
    if mod<keep:
      keep=mod
print(keep)

### FIXED CODE ###

L,R=map(int,input().split())

keep=2018
for i in range(L,R+1):
  for j in range(i+1,R+1):
    keep=min(keep,(i*j)%2019)
    if keep==0:
      print(0)
      exit()
print(keep)
### BUGGY CODE ###

n, k, q = map(int, input().split())
a = [int(input()) for i in range(q)]
p = [k for i in range(n)]
def judge(x):
    if p[x] <= 0:
        return 'No'
    else:
        return 'Yes'
for j in range(q):
    for i in range(n):
     if i + 1 == a[j]:
        continue
     else:
        p[i] -= 1
b = [judge(i) for i in range(n)]
print('\n'.join(b))

### FIXED CODE ###

n, k, q = map(int, input().split())
a = [int(input()) for i in range(q)]
p = [k for i in range(n)]
def judge(x):
    if p[x] - q <= 0:
        return 'No'
    else:
        return 'Yes'
for i in range(q):
    p[a[i] - 1] += 1
b = [judge(i) for i in range(n)]
print('\n'.join(b))
### BUGGY CODE ###

# https://atcoder.jp/contests/abc063/tasks/arc075_a

import itertools
import collections
import bisect

def main():
    N = int(input())
    scores = []
    for _ in range(N):
        s = int(input())
        scores.append(s)

    ans = 0
    if sum(scores) % 10 != 0:
        ans = sum(scores)
    else:
        for i in range(1, len(scores)+1):
            for cand in itertools.combinations(scores, i):
                val = sum(cand)
                if val % 10 != 0:
                    ans = max(ans, val)

    print(ans)

if __name__ == '__main__':
    main()


### FIXED CODE ###

# https://atcoder.jp/contests/abc063/tasks/arc075_a

import itertools
import collections
import bisect

def main():
    N = int(input())
    scores = []
    for _ in range(N):
        s = int(input())
        scores.append(s)

    ans = 0
    if sum(scores) % 10 != 0:
        ans = sum(scores)
    else:
        scores.sort()
        for c in scores:
            if c % 10 != 0:
                ans = sum(scores) - c
                break

    print(ans)

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

import numpy as np

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

dp = np.zeros(n, dtype=int)
dp[0] = 0
dp[1] = dp[0] + abs(h[1]-h[0])

for i in range(2, n): 
    dp[i] = min([dp[i-j]+abs(h[i]-h[i-j]) for j in range(1, min(k, i)+1)])

print(dp[n-1])

### FIXED CODE ###

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

dp = [10**9+1]*n
dp[0] = 0
dp[1] = dp[0] + abs(h[1]-h[0])

for i in range(2, n): 
    dp[i] = min([dp[i-j]+abs(h[i]-h[i-j]) for j in range(1, min(k, i)+1)])

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

n=int(input())
a=sorted(list(map(int,input().split())))

b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
#print(a)
#print(b)
#print(c)
c_count=[]#cから行けるところの回数
b_count=[]#bから行けるところの回数

j=0
count=0
for i in range(n):#cから行けるところの回数
    for j in range(count,n):
        if b[j]<c[i]:
            count+=1
        else:
            break
    c_count.append(count)

count=0

for i in range(n):#bから行けるところの回数
    for j in range(count,n):
        if a[j]<b[i]:
            count+=1
        else:
            break
    b_count.append(count)

ans=0

for i in range(n):
    ans+=sum(b_count[0:c_count[i]])

print(ans)

#print(b_count)
#print(c_count)


### FIXED CODE ###

n=int(input())
a=sorted(list(map(int,input().split())))

b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
#print(a)
#print(b)
#print(c)
c_count=[]#cから行けるところの回数
b_count=[]#bから行けるところの回数

j=0
count=0
for i in range(n):#cから行けるところの回数
    for j in range(count,n):
        
        if b[j]<c[i]:
 #           print("dfghj")
            count+=1
        else:
  #          print("GHJK")
            break
    c_count.append(count)

count=0

for i in range(n):#bから行けるところの回数
    for j in range(count,n):
        if a[j]<b[i]:
            count+=1
        else:
            break
    b_count.append(count)

ans=0
#print(b_count)
#print(c_count)
for i in range(1,n):
    b_count[i]+=b_count[i-1]
#print(b_count)

for i in range(n):
    if 0<=c_count[i]-1:
        ans+=b_count[c_count[i]-1]

print(ans)

#print(b_count)
#print(c_count)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
b = list(reversed(a))
c = [0] * (n + 1)
d = [0] * (n + 1)
cnt = 0
for i in range(n):
    for j in range(i, 0, -1):
        while a[j - 1] < a[j]:
            cnt += 1
            a[j - 1] *= 4
    c[i + 1] = cnt
cnt = 0
for i in range(n):
    for j in range(i, 0, -1):
        while b[j - 1] < b[j]:
            cnt += 1
            b[j - 1] *= 4
    d[i + 1] = cnt
ans = 10 ** 18
for i in range(n + 1):
    ans = min(ans, (c[i] + d[n - i]) * 2 + i)
print(ans)


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
b = list(reversed(a))
c = [0] * (n + 1)
d = [0] * (n + 1)
s = [(0, 10 ** 30)]
res = 0
for i in range(1, n):
    if a[i - 1] < a[i] * 4:
        t = a[i - 1]
        cnt = 0
        while t < a[i]:
            cnt += 1
            t *= 4
        while cnt > 0:
            idx, p = s.pop()
            if cnt >= p:
                res += p * (i - idx)
                cnt -= p
            else:
                res += cnt * (i - idx)
                s.append((idx, p - cnt))
                cnt = 0
    else:
        t = a[i]
        cnt = 0
        while a[i - 1] >= t * 4:
            t *= 4
            cnt += 1
        s.append((i, cnt))
    c[i + 1] = res
s = [(0, 10 ** 30)]
res = 0
for i in range(1, n):
    if b[i - 1] < b[i] * 4:
        t = b[i - 1]
        cnt = 0
        while t < b[i]:
            cnt += 1
            t *= 4
        while cnt > 0:
            idx, p = s.pop()
            if cnt >= p:
                res += p * (i - idx)
                cnt -= p
            else:
                res += cnt * (i - idx)
                s.append((idx, p - cnt))
                cnt = 0
    else:
        t = b[i]
        cnt = 0
        while b[i - 1] >= t * 4:
            t *= 4
            cnt += 1
        s.append((i, cnt))
    d[i + 1] = res
ans = float('inf')
for i in range(n + 1):
    ans = min(ans, (c[i] + d[n - i]) * 2 + i)
print(ans)
### BUGGY CODE ###

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

for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1


d_c = {}
for i in d:
    d_c[i] = d[i] * (d[i] - 1) // 2


d_3 ={}

for i in range(n):
    b = a[i]
    count = 0
    if b not in d_3:
        for j in d:
            if j != b:
                count += d_c[j]
            else:
                count += d_c[j]*(d[j]-2)//d[j]
        d_3[b] = count
        print(count)
    else:
        print(d_3[b])
       

### FIXED CODE ###

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

for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1

count = 0
for i in d:
    count += d[i] * (d[i] - 1) // 2


for i in range(n):
    b = d[a[i]]
    print(count-b+1)
### BUGGY CODE ###

from itertools import combinations

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

c = list(combinations(A, K))

mod = 10**9 + 7

total = 0

for S in c:
  fx = max(S) - min(S)
  total += fx
  total = total % mod

print(total)

### FIXED CODE ###

import math

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

def countComb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
NN = 10 ** 6  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
 
for i in range(2, NN + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

#print(cmb(n, r, p))

totalMaxA = 0
totalMinA = 0
for i in range(K-1, N):
  totalMaxA += A[i] * countComb(i, K-1, p)
for i in range(N-K+1):
  totalMinA += A[i] * countComb(N-i-1, K-1, p)

print((totalMaxA - totalMinA) % p)
### BUGGY CODE ###


# 二部探索で決めていく。dpですでに見られたかどうかは把握
"""
工事 finish  1 eve削除
工事 start   2 eve追加
di start     3
この時間を混ぜてソート。上から順番に捌く
（区間がxi,xjで重なっていたらsetに入れられないのか。）

同じ時刻だった場合
工事finish -> 工事start -> di通過
の順番
"""
import sys
from operator import itemgetter
input = sys.stdin.readline
n,q  = map(int, input().split())
events = []
E = 10**10
for i in range(n):
    s,t,x = map(int, input().split())
    # st
    events.append((s-x,1,x))
    # gl
    events.append((t-x,0,x))
ds = [(int(input()),2,i) for i in range(q)]
events.extend(ds)
events = sorted(events, key=itemgetter(0,1), reverse=True)
ans = [-1] * q
events.sort()
works = set()
for t, e, x in events:
    if e == 1:
        works.add(x)
    elif e == 0:
        works.remove(x)
    else:
        if len(works) == 0:
            ans[x] = -1
        else:
            ans[x] = min(works)
print(*ans)

    

### FIXED CODE ###


# 二部探索で決めていく。dpですでに見られたかどうかは把握
"""
工事 finish  1 eve削除
工事 start   2 eve追加
di start     3
この時間を混ぜてソート。上から順番に捌く
（区間がxi,xjで重なっていたらsetに入れられないのか。）

同じ時刻だった場合
工事finish -> 工事start -> di通過
の順番
"""
import sys
from operator import itemgetter
input = sys.stdin.readline
n,q  = map(int, input().split())
events = []
E = 10**10
for i in range(n):
    s,t,x = map(int, input().split())
    # st
    events.append((s-x,1,x))
    # gl
    events.append((t-x,0,x))
ds = [(int(input()),2,i) for i in range(q)]
events.extend(ds)
ans = [-1] * q
events.sort()
works = set()
upd = True
mn = 10**10
for t, e, x in events:
    if e == 1:
        works.add(x)
        if mn > x:
            mn = x
            upd = False
    elif e == 0:
        works.remove(x)
        if x == mn:
            upd = True
    else:
        if len(works) == 0:
            ans[x] = -1
        else:
            if upd:
                mn = min(works)
                upd = False
            ans[x] = mn
print(*ans)
### BUGGY CODE ###

n = int(input())
s = input()

def is_len(x):
  for i in range(n-2*x+1):
    temp = s[i:i+x]
    if temp in s[i+x:]:
      return True
  return False

ok = 0
ng = n//2 + 1
while ng - ok > 1:
  m = (ok+ng)//2
  if is_len(m):
    ok = m
  else:
    ng = m
    
print(ok)

### FIXED CODE ###

def z_algo(S):
    N = len(S)
    A = [0]*N
    A[0] = N
    i, j = 1, 0
    while i < N:
        while i+j < N and S[j] == S[i+j]:
            j += 1
        A[i] = j
        if not j:
            i += 1
            continue
        k = 1
        while i+k < N and k+A[k] < j:
            A[i+k] = A[k]
            k += 1
        i += k
        j -= k
    return A

n = int(input())
s = input()
ans = 0
for i in range(n):
  A = z_algo(s[i:])
  for j in range(1, n-i):
    m = min(j, A[j])
    ans = max(m, ans)
print(ans)
### BUGGY CODE ###

from collections import Counter

n = int(input())
c_list = []
ans = 0

for i in range(n):
  c = Counter(input())
  if c in c_list:
  	ans += c_list.count(c) 
  c_list.append(c)
  
print(ans)
    

### FIXED CODE ###

from collections import Counter

n = int(input())
ans = 0
ctr = Counter()

for i in range(n):
  s = ''.join(sorted(list(input())))
  if s in ctr:
    ans += ctr[s]
  ctr[s] += 1
  
print(ans)
### BUGGY CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import bisect
import array


def solve():
    n, m, q = map(int, input().split())
    nl = [[] for j in range(n + 1)]
    for e in [input().split() for _ in [0] * m]:
        l, r = int(e[0]), int(e[1])
        bisect.insort_left(nl[l], r)
    table = [array.array("i", [0] * (n + 1)) for j in range(n + 1)]
    ran = array.array("i", range(1, n + 1))
    for i in ran:
        for j in ran:
            table[i][j] = bisect.bisect_right(nl[i], j)

    a = [0] * q
    s = array.array("i", a)
    for i, e in enumerate([input().split() for _ in a]):
        l, r = int(e[0]), int(e[1])
        for j in range(l, r + 1):
            s[i] += table[j][r] - table[j][l - 1]
    print("\n".join([str(e) for e in s]))


if __name__ == "__main__":
    solve()


### FIXED CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import bisect
import array


def solve():
    n, m, q = map(int, input().split())
    nl = [[0 for i in range(n + 1)] for j in range(n + 1)]
    for e in [input().split() for _ in range(m)]:
        l, r = int(e[0]), int(e[1])
        nl[l][r] += 1
    table = [array.array("i", [0] * (n + 2)) for j in range(n + 2)]
    ran = array.array("i", range(n + 1))
    for i in ran:
        for j in ran:
            table[i][j + 1] = table[i][j] + nl[i][j]

    for i, e in enumerate([input().split() for _ in range(q)]):
        l, r = int(e[0]), int(e[1])
        ans = 0
        for j in range(l, r + 1):
            ans += table[j][r + 1] - table[j][l]
        print(ans)


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

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

B = []
for a in A:
    B.append(str(a))
    B = B[::-1]

print(' '.join(B))

### FIXED CODE ###

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

B = ['_' for _ in range(N)]
if N % 2 == 0:
    for i, a in enumerate(A):
        if i % 2 == 0:
            B[N//2 + i//2] = a
        else:
            B[N//2 - (i+1)//2] = a
else:
    for i, a in enumerate(A):
        if i % 2 == 0:
            B[N//2 - i//2] = a
        else:
            B[N//2 + (i+1)//2] = a

print(' '.join(B))
### BUGGY CODE ###

# H,W = 3,3
# s = [".#.", "..#", "#.."]

from collections import deque
H,W = map(int, input().split())
s = [input() for _ in range(H)]

ans = 0
kaburi = 0
for i in range(H):
    for j in range(W):
        if s[i][j]==".": continue
        stk = deque([(i,j)]) # stktack
        visited = set([(i,j)])
        while stk: # なくなるまで
            y,x = stk.popleft()

            for dy,dx in ([1,0],[0,1],[-1,0],[0,-1]):
                if 0 <= y+dy < H and 0 <= x+dx < W:
                    if ((s[y][x]=="." and s[y+dy][x+dx] == "#")
                     or (s[y][x]=="#" and s[y+dy][x+dx] == ".") ):
                        if (y+dy,x+dx) in visited: continue
                        stk.append([y+dy,x+dx])
                        visited.add((y+dy,x+dx))
                        # print(i,j, y+dy,x+dx, ans)
                        if s[y+dy][x+dx]=="#": kaburi += 1
                        ans += 1
print(ans-kaburi)


### FIXED CODE ###

class UnionFind:
    """
    - sizeを指定
    - 要素をグループにまとめる(unite) 
    - 要素同士が同じグループか判定する(isSame)
    - 分離はできない
    - par: 親の番号(はじめ par[i]==i つまりすべての頂点が根)
    - 始め各要素は別々のグループ"""
    def __init__(self, size: int):
        self.par = [-1]*size
        for i in range(size):
            self.par[i] = i 
    
    def root(self, x: int) -> int:
        """根を求める"""
        if self.par[x] == x: # if root
            return x
        else:
            self.par[x] = self.root(self.par[x]) # 経路圧縮
            return self.par[x]
    
    def isSame(self, x:int, y:int)->bool:
        """x と y が同じ集合に属するか否か"""
        return self.root(x)==self.root(y)
    
    def unite(self, x:int, y:int):
        """x と y の属する集合を併合"""
        x = self.root(x)
        y = self.root(y)
        if x == y: return
        self.par[x] = y


H,W = map(int, input().split())
s = [input() for _ in range(H)]
uf = UnionFind(H*W) 

def pos2int(h,w):
    # 座標->[0 - H*W]
    return h*W + w

for h in range(H):
    for w in range(W):        
        for dh, dw in zip((0, 1), (1, 0)):
            if not(0<=h+dh<H and 0<=w+dw<W):
                continue
            if s[h][w] != s[h+dh][w+dw]: # 同じグループにする
                uf.unite(pos2int(h, w), pos2int(h+dh, w+dw))
                
white = [0] * H*W
black = [0] * H*W
 
for h in range(H):
    for w in range(W):
        n = pos2int(h, w)
        r = uf.root(n)
        # print(n, r)
        if s[h][w] == ".":
            white[r] += 1
        else:
            black[r] += 1
# print(white)
# print(black)
ans = 0
for h in range(H):
    for w in range(W):
        n = pos2int(h, w)
        ans += white[n] * black[n]
print(ans)
### BUGGY CODE ###

S=input()
Q=int(input())
Flag=True
for i in range(Q):
    L=input().split()
    if L[0]=="1":
        Flag=not Flag
    else:
        if L[1]=="1" and Flag or L[1]=="2" and not Flag:
            S=L[2]+S
        else:
            S=S+L[2]
if not Flag:
    S=S[::-1]
print(S)
            


    









        

    









    

        




    















    



    





    






        






    
















        


























            
    


    


    
    














    


    














### FIXED CODE ###

from collections import deque
S=input()
d=deque()
for i in S:
    d.append(i)
Q=int(input())
Flag=True
for i in range(Q):
    L=input().split()
    if L[0]=="1":
        Flag=not Flag
    else:
        if L[1]=="1" and Flag or L[1]=="2" and not Flag:
            d.appendleft(L[2])
        else:
            d.append(L[2])
ans=""
for i in d:
    ans+=i
if not Flag:
    ans=ans[::-1]
print(ans)
### BUGGY CODE ###

n = int(input())
a = [int(i) for i in input().split()]
ans = 0
for i in set(a):
    if a.count(i) - i < 0:
        ans += a.count(i)
    else:
        ans += a.count(i) - i
print(ans)

### FIXED CODE ###

n = int(input())
a = [int(i) for i in input().split()] 
count = {}
ans = 0
for i in a:
    if i not in count:
        count[i] = 1
    else:
        count[i] += 1
for i,c in count.items():
    if i > c:
        ans += c
    elif i < c:
        ans += c - i
print(ans)