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

a, b = map(int,input().split())
if a < b:
    buf = a
    a = b
    b = buf
while b != 0:
    a, b = b, a % b
gcd = a
count = 1
if gcd % 2 == 0:
    count += 1
    gcd = int(gcd / 2)
if gcd % 3 == 0:
    count += 1
    gcd = int(gcd / 3)


def hantei(num):
    flag = True
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            flag = False
    return flag


if gcd < 5:
    print(count)
else:
    for i in range(5, gcd + 1, 2):
        if gcd % i == 0 and hantei(i) == True:
            count += 1
            gcd = int(gcd / i)
    print(count)


### FIXED CODE ###

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

a, b = map(int,input().split())
g = gcd(a,b)
count = 0
for i in range(2, int(g**0.5)+2):
    if g % i == 0:
        count += 1
        while g % i == 0:
            g //= i
if g != 1:
    count += 1
print(count+1)
### BUGGY CODE ###

import sys
input=sys.stdin.readline
from math import floor

N,M = (int(x) for x in input().rstrip('\n').split())
As = [int(x) for x in input().rstrip('\n').split()]
As = sorted(As)
for _ in range(M):
  d = As[-1]
  As[-1] = d/2
  As = sorted(As)
print(sum(floor(x) for x in As))

### FIXED CODE ###

import sys
input=sys.stdin.readline

N,M = (int(x) for x in input().rstrip('\n').split())
As = [int(x) for x in input().rstrip('\n').split()]
As = sorted(As,reverse = True)
rest = M
for _ in range(M):
  if rest == 0:
    break
  d = As[0]
  b = d//2 - d//4
  for i in range(N):
    a = As[i]
    check = 0
    if a-a//2<b:
      if rest > i:
        ad = [x//2 for x in As[:i]]
        As = As[i:]
        As.extend(ad)
        rest -= i
        check += 1
        As = sorted(As,reverse = True)
        break
      else:
        ad = [x//2 for x in As[:rest]]
        As = As[rest:]
        As.extend(ad)
        check += 1
        rest = 0
        break
    if i == N-1:
      if check == 0:
        if rest > N:
          As = [x//2 for x in As]
          rest -= N
        else:
          ad = [x//2 for x in As[:rest]]
          As = As[rest:]
          As.extend(ad)
          rest = 0
          break
print(sum(As))
### BUGGY CODE ###

"""
解説と提出 #5747380を参考に作成
"""

from collections import deque


def solve():
    H, W = map(int, input().split())
    # mass = [[False] * W for _ in range(H)]
    # queue1 = deque([])
    # queue2 = deque([])
    queue1 = []
    queue2 = []
    mass = []
    mass.append([False] * (W + 2))  # 外枠
    for Hi in range(1, H + 1):
        mass.append([False] + [True if s == '.' else False for s in list(input())] + [False])
        for Wi in range(1, W + 1):
            if not mass[Hi][Wi]:
                queue1.append([Hi, Wi])
    mass.append([False] * (W + 2))  # 外枠
    import datetime
    d = datetime.datetime.now()
    count = -1
    while True:
        # print('count: {}, queue:{}'.format(count, len(queue1)))
        if len(queue1) == 0:
            break
        for h, w in queue1:
            # left
            if mass[h][w - 1]:
                mass[h][w - 1] = False
                queue2.append([h, w - 1])
            # right
            if mass[h][w + 1]:
                mass[h][w + 1] = False
                queue2.append([h, w + 1])
            # top
            if mass[h + 1][w]:
                mass[h + 1][w] = False
                queue2.append([h + 1, w])
            # button
            if mass[h - 1][w]:
                mass[h - 1][w] = False
                queue2.append([h - 1, w])

        count += 1
        queue1 = queue2
        queue2 = []
    print(count)

    # print(datetime.datetime.now() - d)


if __name__ == '__main__':
    solve()


### FIXED CODE ###

"""
解説と提出 #5747380を参考に作成
"""

from collections import deque


def solve():
    H, W = map(int, input().split())
    # mass = [[False] * W for _ in range(H)]
    # queue1 = deque([])
    # queue2 = deque([])
    queue1 = []
    queue2 = []
    mass = []
    mass.append([False] * (W + 2))  # 外枠
    for Hi in range(1, H + 1):
        mass.append([False] + [s == '.' for s in list(input())] + [False])
        for Wi in range(1, W + 1):
            if not mass[Hi][Wi]:
                queue1.append((Hi, Wi))
    mass.append([False] * (W + 2))  # 外枠
    import datetime
    d = datetime.datetime.now()
    count = -1
    while True:
        # print('count: {}, queue:{}'.format(count, len(queue1)))
        if len(queue1) == 0:
            break
        for h, w in queue1:
            # left
            if mass[h][w - 1]:
                mass[h][w - 1] = False
                queue2.append((h, w - 1))
            # right
            if mass[h][w + 1]:
                mass[h][w + 1] = False
                queue2.append((h, w + 1))
            # top
            if mass[h + 1][w]:
                mass[h + 1][w] = False
                queue2.append((h + 1, w))
            # button
            if mass[h - 1][w]:
                mass[h - 1][w] = False
                queue2.append((h - 1, w))

        count += 1
        queue1 = queue2
        queue2 = []
    print(count)

    # print(datetime.datetime.now() - d)


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

s = list(map(int, input().split(" ")))

a = s[0]
b = s[1]
m = s[2]

a_l = list(map(int, input().split(" ")))
b_l = list(map(int, input().split(" ")))

ans = a_l[0] + b_l[0]

for i in range(m):
    x = list(map(int, input().split(" ")))
    if ans > (a_l[x[0] - 1] + b_l[x[1] -1] - x[2]):
        ans = a_l[x[0] -1] + b_l[x[1] -1] - x[2]
        
while len(a_l) > 0:
    a_ = a_l.pop(0)
    for v in b_l:
        if ans > (a_ + v):
            ans = a_ + v

print(ans)

### FIXED CODE ###

s = list(map(int, input().split(" ")))

a = s[0]
b = s[1]
m = s[2]

a_l = list(map(int, input().split(" ")))
b_l = list(map(int, input().split(" ")))

ans = min(a_l) + min(b_l)

for i in range(m):
    x = list(map(int, input().split(" ")))
    if ans > (a_l[x[0] - 1] + b_l[x[1] -1] - x[2]):
        ans = a_l[x[0] -1] + b_l[x[1] -1] - x[2]


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

N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        # print(A[i], A[j], A[i]^A[j])
        ans += (A[i] ^ A[j]) % mod
        # print(A[i]^A[j])
print(ans % mod)


### FIXED CODE ###

mod = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))
ans = 0
loop_count = len(format(max(A), "b"))

for i in range(loop_count):
    tmp = 0
    for ai in A:
        if ((1 << i) & ai) > 0:
            tmp += 1
    ans += (1 << i) * tmp * (N - tmp) % mod
print(ans % mod)
### BUGGY CODE ###

s=input()
ans=0
for i in range(n:=len(s)):
    for j in range(i+3,n):
        t=int(s[i:j+1])
        if t%2019==0:
            ans+=1
print(ans)

### FIXED CODE ###

S=input()
MOD=2019
dp=[0]*MOD
dp[0]=1
r=0
t=1
for c in reversed(S):
    r+=int(c)*t
    r%=MOD
    t*=10
    t%=MOD
    dp[r]+=1
print(sum(i*(i-1)//2 for i in dp))
### BUGGY CODE ###

n, q = map(int,input().split())
nodes = [[[], 0] for _ in range(n)]

def add(idx, val):
    global nodes
    addnodes = [idx]
    while len(addnodes) > 0:
        n = addnodes.pop()
        nodes[n][1] += val
        for i in nodes[n][0]:
            addnodes.append(i)


for _ in range(n-1):
    a, b = map(int, input().split())
    nodes[a-1][0].append(b-1)
for _ in range(q):
    p, q = map(int, input().split())
    add(p-1, q)
op = str(nodes[0][1])
for n in range(1, len(nodes)):
    op += " {}".format(nodes[n][1])
print(op)


### FIXED CODE ###

n, q = map(int,input().split())
nodes = [[[], 0] for _ in range(n)]

for _ in range(n-1):
    a, b = map(int, input().split())
    nodes[a-1][0].append(b-1)
for _ in range(q):
    p, q = map(int, input().split())
    nodes[p-1][1] += q

search_node = [0]

while len(search_node) > 0:
    n = search_node.pop()
    for i in nodes[n][0]:
        nodes[i][1] += nodes[n][1]
        search_node.append(i)


op = str(nodes[0][1])
for n in range(1, len(nodes)):
    op += " {}".format(nodes[n][1])
print(op)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
score=0
for i in range(n-1):
  for j in range(i+1, n):
    score += a[i]*a[j]
print(score%mod)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
b=sum(a)
score=0
for i in range(len(a)):
  b -= a[i]
  score+=a[i]*b
  
print(score%(10**9+7))
### BUGGY CODE ###

import math
import sys
import collections
def main():
    N=int(input())
    Tr=tuple(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
       if Tr[i]=="R":
           R.append(i)
       if Tr[i]=="G":
           G.append(i)
       if Tr[i]=="B":
           B.append(i)
    cnt=0
    for i in R:
        for j in G:
            for k in B:
                if abs(i-j)!=abs(j-k) and abs(j-k)!=abs(k-i) and abs(j-i)!=abs(i-k):
                    cnt+=1
    print(cnt)
if __name__ == '__main__':
    main()

### FIXED CODE ###

import math
import sys
import collections

def main():
    N=int(input())
    Tr=list(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
       if Tr[i]=="R":
           R.append(i)
           continue
       if Tr[i]=="G":
           G.append(i)
           continue
       if Tr[i]=="B":
           B.append(i)
           continue
    cnt=len(R)*len(G)*len(B)
    for j in range(N):
        for i in range(j):
            if 2*j-i<=N-1:
                if Tr[2*j-i]!=Tr[i] and Tr[2*j-i]!=Tr[j] and Tr[j]!=Tr[i]:
                    cnt-=1
    print(cnt)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

N = int(input())
P = list(map(int, input().split(' ')))
print(sum(1 for i in range(1, N) if P[i] <= min(P[0:i])) + 1)

### FIXED CODE ###

N = int(input())
P = list(map(int, input().split(' ')))
cnt = 1
j = P[0]
for i in range(1, N):
    j = min(j, P[i])
    if P[i] <= j:
        cnt += 1
print(cnt)
### BUGGY CODE ###



from collections import defaultdict
from collections import deque

def main():
        
    N = int(input())
    """
    before[(i,j)]
    iとjの試合より前に終わらせておく必要のある直前の対戦者の組の数

    nxt[(i,j)]
    iとjの試合より後に行える対戦者の組

    例２
    4
    2 3 4
    1 3 4
    4 1 2
    3 1 2
    について、
    before[(1,3)] = 2
    before[(1,4)] = 2
    nxt[(1,2)] = set((1,3), (2,3))
    みたいな
    """
    before = defaultdict(int)
    nxt = defaultdict(set)

    A = [list(map(int, input().split())) for _ in range(N)]

    for i in range(N):

        for j,a in enumerate(A[i]):
            l, h = min(i+1, a), max(i+1, a)
            if j == 0:
                b = (l,h)
            else:
                before[(l,h)] += 1
                nxt[b].add((l,h))
                b = (l,h)



    match_today = deque()

    for i in range(1, N):
        for j in range(i+1, N+1):
            if before[(i, j)] == 0:
                match_today.append((i,j))
                

    
    ans = 0
    cnt_match = 0 
    while len(match_today) > 0:
        #print( match_today)
        ans += 1
        
        for _ in range(len(match_today)):
            l,h = match_today.popleft()
            cnt_match += 1
            for nl, nh in nxt[(l,h)]:
                before[(nl, nh)] -= 1
                
                if before[(nl, nh)] == 0:
                    match_today.append((nl, nh))
        
       


    if cnt_match == N*(N-1) // 2:
        print(ans)
    else:
        print(-1)


if __name__ == "__main__":
    main()

### FIXED CODE ###


from collections import deque

N = int(input())
match_order = [deque(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]


q = deque(range(N))
finish_by = [0] * N
next_opponent = [None] * N

while q:
    a = q.popleft()
    # aの残りの順で一番初めに戦う対戦相手を取り出す
    b = match_order[a].popleft()
    if next_opponent[b] == a:
        finish_by[a] = finish_by[b] = max(finish_by[a], finish_by[b]) + 1
        # a,bについて、まだ対戦相手が残っているならキューに積む
        if match_order[a]:
            q.append(a)
        if match_order[b]:
            q.append(b)
    # bの残りの順で戦う相手がaではないなら、順番が来るまで待つ
    else:
        next_opponent[a] = b

# 対戦相手が残っている人がいる場合、条件通りに終わることができない
if any(match_order):
    print(-1)
else:
    print(max(finish_by))
### BUGGY CODE ###

n = int(input())
base = input().split()
x = []
for i in base:
    x.append(int(i))
for i in range(1,n+1):
    print(x.count(i))

### FIXED CODE ###

import collections

n = int(input())
base = input().split()
x = []
for i in base:
    x.append(int(i))
#x.sort(reverse = False)
c = collections.Counter(x)
for i in range(1,n):
    print(c[i])
print(0)
### BUGGY CODE ###

import heapq

n, m = map(int, input().split())
a = [-(int(i)) for i in input().split()]

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc += [-c] * b

heapq.heapify(a)
heapq.heapify(bc)
ans = 0
x, y = [], []

for _ in range(n):
    if x == []:
        x.append(heapq.heappop(a))
    if y == [] and len(bc) > 0:
        y.append(heapq.heappop(bc))

    if x <= y:
        ans += x.pop()
    else:
        if y != []:
            ans += y.pop()
        else:
            ans += x.pop()

print(-ans)

### FIXED CODE ###

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

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc.append((b, c))

bc = sorted(bc, key=lambda x: x[1], reverse=True)

cnt = 0

for b, c in bc:
    cnt += b
    a += [c] * b
    if cnt >= n:
        break

print(sum(sorted(a,reverse=True)[:n]))
### BUGGY CODE ###

X, Y, Z, K=map(int, input().split())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
C=list(map(int, input().split()))
A=[-A[i] for i in range(X)]
B=[-B[i] for i in range(Y)]
C=[-C[i] for i in range(Z)]
A.sort()
B.sort()
C.sort()

import heapq
nums=[[A[0]+B[0]+C[0], 0, 0, 0]]
heapq.heapify(nums)

sumi=[[[0]*Z for __ in range(Y)] for _ in range(X)]
for _ in range(K):
  out, a, b, c=heapq.heappop(nums)
  print(-out)
  if a<X-1 and sumi[a+1][b][c]==0:
    heapq.heappush(nums, [A[a+1]+B[b]+C[c], a+1, b, c])
    sumi[a+1][b][c]=1
  if b<Y-1 and sumi[a][b+1][c]==0:
    heapq.heappush(nums, [A[a]+B[b+1]+C[c], a, b+1, c])
    sumi[a][b+1][c]=1
  if c<Z-1 and sumi[a][b][c+1]==0:
    heapq.heappush(nums, [A[a]+B[b]+C[c+1], a, b, c+1])
    sumi[a][b][c+1]=1


### FIXED CODE ###

X, Y, Z, K=map(int, input().split())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
C=list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

import heapq
nums=[[-(A[0]+B[0]+C[0]), 0, 0, 0]]
heapq.heapify(nums)

sumi=[[0, 0, 0]]
for _ in range(K):
  out, a, b, c=heapq.heappop(nums)
  print(-out)
  if a<X-1 and not([a+1, b, c] in sumi):
    heapq.heappush(nums, [-(A[a+1]+B[b]+C[c]), a+1, b, c])
    sumi.append([a+1, b, c])
  if b<Y-1 and not([a, b+1, c] in sumi):
    heapq.heappush(nums, [-(A[a]+B[b+1]+C[c]), a, b+1, c])
    sumi.append([a, b+1, c])
  if c<Z-1 and not([a, b, c+1] in sumi):
    heapq.heappush(nums, [-(A[a]+B[b]+C[c+1]), a, b, c+1])
    sumi.append([a, b, c+1])
### BUGGY CODE ###

import bisect
N=int(input())
L=sorted(map(int,input().split()))
cnt=0
for i in range(N):
    for j in range(i):
        a=L[i]
        b=L[j]
        right=a+b
        left=abs(a-b)
        res=bisect.bisect_left(L,right)-bisect.bisect_right(L,left)
        if(left<a<right):
            res-=1
        if(left<b<right):
            res-=1
        cnt+=res
print(cnt//3)


### FIXED CODE ###

import bisect
N=int(input())
L=sorted(map(int,input().split()))
cnt=0
for i in range(N):
    for j in range(i):
        a=L[i]
        b=L[j]
        right=a+b
        left=abs(a-b)
        rind=bisect.bisect_left(L,right)
        lind=bisect.bisect_right(L,left)
        res=rind-lind
        if(left<a<right):
            res-=1
        if(left<b<right):
            res-=1
        cnt+=res
print(cnt//3)
### BUGGY CODE ###

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

MOD = 10**9 + 7
R = 1
for i in range(K - 2):
    R = R * (N * M - 2 - i) // (i + 1)

ans = 0
for i in range(1, N + 1):
    for j in range(1, M + 1):
        u = (N - i + 1)
        d = i
        l = j
        r = (M - j + 1)
        D = 0
        D += u * l * (u + l - 2) // 2
        D += u * r * (u + r - 2) // 2
        D += d * l * (d + l - 2) // 2
        D += d * r * (d + r - 2) // 2
        D -= u * (u - 1) // 2
        D -= d * (d - 1) // 2
        D -= l * (l - 1) // 2
        D -= r * (r - 1) // 2
        ans += D

ans = (ans * R // 2) % MOD
print(ans)


### FIXED CODE ###

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

MOD = 10**9 + 7
R = 1
for i in range(K - 2):
    R = (R * (N * M - 2 - i) * pow(i + 1, MOD - 2, MOD)) % MOD

ans = 0
for i in range(1, N + 1):
    for j in range(1, M + 1):
        u = (N - i + 1)
        d = i
        l = j
        r = (M - j + 1)
        D = 0
        D += u * l * (u + l - 2) // 2
        D += u * r * (u + r - 2) // 2
        D += d * l * (d + l - 2) // 2
        D += d * r * (d + r - 2) // 2
        D -= u * (u - 1) // 2
        D -= d * (d - 1) // 2
        D -= l * (l - 1) // 2
        D -= r * (r - 1) // 2
        ans += D

ans = (ans * R * pow(2, MOD - 2, MOD)) % MOD
print(ans)
### BUGGY CODE ###

from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()

ans = 0
for i in range(60):
    s = 0
    t = 0
    for j in range(n):
        if a[j] % 2 == 0:
            s += 1
        else:
            t += 1
        a[j] //= 2
    ans = ( ans + s * t * 2**i ) % ( 10**9 + 7 )
print(ans)

### FIXED CODE ###

from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()

ans = 0
k = 1
for i in range(60):
    s = 0
    t = 0
    for j in range(n):
        if a[j] % 2 == 0:
            s += 1
        else:
            t += 1
        a[j] //= 2
    ans = ( ans + s * t * k ) % ( 10**9 + 7 )
    k *= 2
print(ans)
### BUGGY CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=0
ans1=0
l=sum(range(1,k))
for i in range(n):
  for j in a:
    if a[i]<j:
      ans+=1
for i in range(n-1):
  for j in range(i+1,n):
    if a[i]>a[j]:
      ans1+=1
print((ans1*k+ans*l)%(10**9+7))

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=0
ans1=0
for i in range(n):
  for j in a:
    if a[i]<j:
      ans+=1
for i in range(n-1):
  for j in range(i+1,n):
    if a[i]>a[j]:
      ans1+=1
print((ans1*k+ans*(k*(k-1)//2))%(10**9+7))
### BUGGY CODE ###

import numpy as np

N, Q = map(int, input().split(' '))
S = input()
len_S = len(S)

flags = [0] * len_S
AC = ('A', 'C')
for i, v in enumerate(zip(S[:-1], S[1:])):
  flags[i + 1] = 1 if v == AC else 0

sums = list(np.cumsum(flags))

for _ in range(Q):
  l, r = map(int, input().split(' '))
  print(sums[r - 1] - sums[l - 1])


### FIXED CODE ###

import numpy as np

N, Q = map(int, input().split(' '))
S = input()
len_S = len(S)

flags = [0] * len_S
for i, v in enumerate(S[:-1]):
  if v == 'A' and S[i + 1] == 'C':
    flags[i + 1] = 1
  else:
    flags[i + 1] = 0

sums = list(np.cumsum(flags))

for _ in range(Q):
  l, r = map(int, input().split(' '))
  print(sums[r - 1] - sums[l - 1])
### BUGGY CODE ###

import sys
def input():
	return sys.stdin.readline()[:-1]

def power(x):
	return pow(2, x, MOD)-1

MOD = 998244353
class BIT():#1-indexed
	def __init__(self, size):
		self.table = [0 for _ in range(size+2)]
		self.size = size

	def Sum(self, i):#1からiまでの和
		s = 0
		while i > 0:
			s += self.table[i]
			i -= (i & -i)
		return s

	def PointAdd(self, i, x):#
		while i <= self.size:
			self.table[i] += x
			i += (i & -i)
		return

n = int(input())
dd = [list(map(int, input().split())) + [i] for i in range(n)]
d = [[-1, -1] for _ in range(n)]
dd.sort()
for i, x in enumerate(dd):
	d[x[2]][0] = i+1
dd.sort(key=lambda x: x[1])
for i, x in enumerate(dd):
	d[x[2]][1] = i+1
d.sort()
d = [(x[0], x[1]) for x in d]
lu = [0 for _ in range(n)]
ld = [0 for _ in range(n)]
rd = [0 for _ in range(n)]
ru = [0 for _ in range(n)]
bitl, bitr = BIT(n), BIT(n)

for i in range(n):
	lu[i] = i - bitl.Sum(d[i][1])
	ld[i] = i - lu[i]
	bitl.PointAdd(d[i][1], 1)

for i in range(n-1, -1, -1):
	ru[i] = n-1-i - bitr.Sum(d[i][1])
	rd[i] = n-1-i - ru[i]
	bitr.PointAdd(d[i][1], 1)
#print(lu, ld, rd, ru, sep="\n")

ans = 0
for i in range(n):
	ans += pow(2, n, MOD)-1
	ans -= pow(2, lu[i]+ld[i], MOD) + pow(2, ld[i]+rd[i], MOD) + pow(2, rd[i]+ru[i], MOD) + pow(2, ru[i]+lu[i], MOD)
	ans += pow(2, lu[i], MOD) + pow(2, ld[i], MOD) + pow(2, rd[i], MOD) + pow(2, ru[i], MOD)
	ans %= MOD

print(ans)


### FIXED CODE ###

import sys
def input():
	return sys.stdin.readline()[:-1]

MOD = 998244353
class Rsum():
	def __init__(self, size):
		#the number of nodes is 2n-1
		self.n = 1
		while self.n < size:
			self.n *= 2
		self.node = [0] * (2*self.n-1)

	def Access(self, x):
		return self.node[x+self.n-1]

	def Add(self, x, val):
		x += self.n-1
		self.node[x] += val
		while x > 0:
			x = (x-1)//2
			self.node[x] = self.node[2*x+1] + self.node[2*x+2]
		return

	#[l, r)
	def Get(self, l, r):
		L, R = l+self.n, r+self.n
		s = 0
		while L<R:
			if R & 1:
				R -= 1
				s += self.node[R-1]
			if L & 1:
				s += self.node[L-1]
				L += 1
			L >>= 1
			R >>= 1
		return s

n = int(input())
d = []
for i in range(n):
	x, yy = map(int, input().split())
	d.append((x, yy, i))
y = [-1 for _ in range(n)]
d.sort(key=lambda x: x[1])
for i, x in enumerate(d):
	y[x[2]] = i

d.sort()
lus = [-1 for _ in range(n)]
lds = [-1 for _ in range(n)]
rus = [-1 for _ in range(n)]
rds = [-1 for _ in range(n)]

st = Rsum(n)
for i in range(n):
	pnt = y[d[i][2]]
	g = st.Get(pnt, n)
	lus[i] = g
	lds[i] = i - g
	st.Add(pnt, 1)

st = Rsum(n)
for i in range(n-1, -1, -1):
	pnt = y[d[i][2]]
	g = st.Get(pnt, n)
	rus[i] = g
	rds[i] = n-1-i - g
	st.Add(pnt, 1)

pws = [1]
for _ in range(n-1):
	pws.append((pws[-1] * 2) % MOD)

ans = (n * pws[n-1]) % MOD
for lu, ld, ru, rd in zip(lus, lds, rus, rds):
	ans += ((pws[lu]-1) * (pws[rd]-1))
	ans += ((pws[ld]-1) * (pws[ru]-1))
	ans += ((pws[lu]-1) * (pws[ld]-1) * (pws[ru]-1))
	ans += ((pws[lu]-1) * (pws[ld]-1) * (pws[rd]-1))
	ans += ((pws[lu]-1) * (pws[rd]-1) * (pws[ru]-1))
	ans += ((pws[ld]-1) * (pws[rd]-1) * (pws[ru]-1))
	ans += ((pws[lu]-1) * (pws[ld]-1) * (pws[ru]-1) * (pws[rd]-1))
	ans %= MOD
print(ans)
### BUGGY CODE ###

import sys
import itertools
tokens = itertools.chain.from_iterable(map(str.split, sys.stdin))

mod = 1000000007
n = int(next(tokens))
a = [int(next(tokens)) for _ in range(n)]

from numba import njit
import numpy as np
a = np.array(a, dtype=np.int64) % mod

@njit('i8(i8[:])')
def fun(a):
    m = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            m = (m + a[i] * a[j]) % mod
    return m

ans = fun(a)
print(ans)


### FIXED CODE ###

import sys
import itertools
tokens = itertools.chain.from_iterable(map(str.split, sys.stdin))

mod = 1000000007
n = int(next(tokens))
a = [int(next(tokens)) % mod for _ in range(n)]

sms = []
sma = sum(a)
for i in range(n):
    sma -= a[i]
    sms.append(sma)
#print(sms)

m = 0
for i in range(n - 1):
    sm = sms[i]
    #print(i, a[i], sm)
    m = (m + a[i] * sm) % mod

print(m)