Problem:
 Score : 600 points    Problem Statement We have a set S of N points in a two-dimensional plane. The coordinates of the i -th point are (x_i, y_i) . The N points have distinct x -coordinates and distinct y -coordinates.  For a non-empty subset T of S , let f(T) be the number of points contained in the smallest rectangle, whose sides are parallel to the coordinate axes, that contains all the points in T . More formally, we define f(T) as follows:   f(T) := (the number of integers i  (1 \leq i \leq N) such that a \leq x_i \leq b and c \leq y_i \leq d , where a , b , c , and d are the minimum x -coordinate, the maximum x -coordinate, the minimum y -coordinate, and the maximum y -coordinate of the points in T )   Find the sum of f(T) over all non-empty subset T of S . Since it can be enormous, print the sum modulo 998244353 .      Constraints  1 \leq N \leq 2 \times 10^5  -10^9 \leq x_i, y_i \leq 10^9  x_i \neq x_j (i \neq j)  y_i \neq y_j (i \neq j)  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  x_1  y_1  :  x_N  y_N       Output Print the sum of f(T) over all non-empty subset T of S , modulo 998244353 .        Sample Input 1 3
-1 3
2 1
3 -2      Sample Output 1 13  Let the first, second, and third points be P_1 , P_2 , and P_3 , respectively. S = \{P_1, P_2, P_3\} has seven non-empty subsets, and f has the following values for each of them:   f(\{P_1\}) = 1  f(\{P_2\}) = 1  f(\{P_3\}) = 1  f(\{P_1, P_2\}) = 2  f(\{P_2, P_3\}) = 2  f(\{P_3, P_1\}) = 3  f(\{P_1, P_2, P_3\}) = 3   The sum of these is 13 .       Sample Input 2 4
1 4
2 1
3 3
4 2      Sample Output 2 34       Sample Input 3 10
19 -11
-3 -12
5 3
3 -15
8 -14
-9 -20
10 -9
0 2
-7 17
6 -6      Sample Output 3 7222  Be sure to print the sum modulo 998244353 .

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(10 ** 9)



INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 998244353


class BIT:
    def __init__(self, size):
        self.bit = [0] * size
        self.size = size

    def add(self, i, w):
        x = i + 1
        while x <= self.size:
            self.bit[x - 1] += w
            x += x & -x
        return

    def sum(self, i):
        res = 0
        x = i + 1
        while x:
            res += self.bit[x - 1]
            x -= x & -x
        return res


n = I()
X = []
Y = []
XY = []
for x, y in LIR(n):
    Y += [y]
    X += [x]

co_to_ind = {e: i for i, e in enumerate(sorted(Y))}
Y = [co_to_ind[k] for k in Y]
Y = [Y[i] for i in sorted(range(n), key=lambda j:X[j])]
pow2 = [1]
for i in range(n):
    pow2 += [pow2[-1] * 2 % mod]


ans = pow2[n - 1] * n % mod
bit = BIT(n)
for i, y in enumerate(Y):
    bit.add(y, 1)
    ld = bit.sum(y - 1)
    lu = i - ld
    rd = y - ld
    ru = n - y - 1 - lu
    ans = ans + (pow2[ld] - 1) * (pow2[ru] - 1) % mod * pow2[lu] % mod * pow2[rd] % mod + (pow2[lu] - 1) * \
          (pow2[rd] - 1) % mod * pow2[ld] % mod * pow2[ru] % mod - (pow2[ld] - 1) * (pow2[ru] - 1) % mod * \
          (pow2[rd] - 1) % mod * (pow2[lu] - 1) % mod


print(ans % mod)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(10 ** 9)



INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 998244353


class BIT:
    def __init__(self, size):
        self.bit = [0] * size
        self.size = size

    def add(self, i, w):
        x = i + 1
        while x <= self.size:
            self.bit[x - 1] += w
            x += x & -x
        return

    def sum(self, i):
        res = 0
        x = i + 1
        while x:
            res += self.bit[x - 1]
            x -= x & -x
        return res


n = I()
X = []
Y = []
XY = []
for x, y in LIR(n):
    Y += [y]
    X += [x]

co_to_ind = {e: i for i, e in enumerate(sorted(Y))}
Y = [co_to_ind[k] for k in Y]
Y = [Y[i] for i in sorted(range(n), key=lambda j:X[j])]
pow2 = [1]
for i in range(n):
    pow2 += [pow2[-1] * 2 % mod]


ans = pow2[n - 1] * n % mod
bit = BIT(n)
for i, y in enumerate(Y):
    bit.add(y, 1)
    ld = bit.sum(y - 1)
    lu = i - ld
    rd = y - ld
    ru = n - y - 1 - lu
    ans = ans + (pow2[ld] - 1) * (pow2[ru] - 1) * pow2[lu] % mod * pow2[rd] % mod
    ans = ans + (pow2[lu] - 1) * (pow2[rd] - 1) * pow2[ld] * pow2[ru] % mod
    ans = ans - (pow2[ld] - 1) * (pow2[ru] - 1) * (pow2[rd] - 1) * (pow2[lu] - 1) % mod


print(ans % mod)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(10 ** 9)



INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 998244353


class BIT:
    def __init__(self, size):
        self.bit = [0] * size
        self.size = size

    def add(self, i, w):
        x = i + 1
        while x <= self.size:
            self.bit[x - 1] += w
            x += x & -x
        return

    def sum(self, i):
        res = 0
        x = i + 1
        while x:
            res += self.bit[x - 1]
            x -= x & -x
        return res


n = I()
X = []
Y = []
XY = []
for x, y in LIR(n):
    Y += [y]
    X += [x]

co_to_ind = {e: i for i, e in enumerate(sorted(Y))}
Y = [co_to_ind[k] for k in Y]
Y = [Y[i] for i in sorted(range(n), key=lambda j:X[j])]
pow2 = [1]
for i in range(n):
    pow2 += [pow2[-1] * 2 % mod]


ans = pow2[n - 1] * n % mod
bit = BIT(n)
for i, y in enumerate(Y):
    bit.add(y, 1)
    ld = bit.sum(y - 1)
    lu = i - ld
    rd = y - ld
    ru = n - y - 1 - lu
    ans = ans + (pow2[ld] - 1) * (pow2[ru] - 1) % mod * pow2[lu] % mod * pow2[rd] % mod
    ans = ans + (pow2[lu] - 1) * (pow2[rd] - 1) % mod * pow2[ld] % mod * pow2[ru] % mod
    ans = ans - (pow2[ld] - 1) * (pow2[ru] - 1) % mod * (pow2[rd] - 1) % mod * (pow2[lu] - 1) % mod


print(ans % mod)

Solution (Python):
import sys
input = sys.stdin.readline

def solve():
    MOD = 998244353

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

    iXs = list(range(N))
    iXs.sort(key=lambda iX: pts[iX][0])
    iYs = list(range(N))
    iYs.sort(key=lambda iY: pts[iY][1])
    odrYs = [0]*(N)
    for odrY, iY in enumerate(iYs):
        odrYs[iY] = odrY

    def init(As):
        numPow2 = 2 ** (len(As)-1).bit_length()
        data = [0] * (numPow2+1)
        return data, numPow2
    def addValue(iA, A):
        iA += 1
        while iA <= numPow2:
            data[iA] += A
            iA += iA & -iA
    def getSum(iA):
        iA += 1
        ans = 0
        while iA > 0:
            ans += data[iA]
            iA -= iA & -iA
        return ans

    numPtss = [[0]*(4) for _ in range(N)]

    data, numPow2 = init([0]*N)
    for odr, iX in enumerate(iXs):
        y = odrYs[iX]
        num = getSum(y)
        numPtss[iX][0] = num
        numPtss[iX][1] = odr - num
        addValue(y, 1)

    data, numPow2 = init([0]*N)
    for odr, iX in enumerate(reversed(iXs)):
        y = odrYs[iX]
        num = getSum(y)
        numPtss[iX][2] = num
        numPtss[iX][3] = odr - num
        addValue(y, 1)

    def getPows(base, n, MOD):
        pows = [1] * (n+1)
        for x in range(1, n+1):
            pows[x] = (pows[x-1] * base) % MOD
        return pows

    pow2s = getPows(2, N, MOD)

    ans = 0
    for numPts in numPtss:
        num = pow2s[N-1]
        nums = [pow2s[k] for k in numPts]
        num += (nums[0]-1)*(nums[1])*(nums[2])*(nums[3]-1) % MOD
        num += (nums[0])*(nums[1]-1)*(nums[2]-1)*(nums[3]) % MOD
        num -= (nums[0]-1)*(nums[1]-1)*(nums[2]-1)*(nums[3]-1) % MOD
        ans += num % MOD
        ans %= MOD

    print(ans)


solve()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

MOD = 998244353

N = int(readline())
m = map(int,read().split())
Y = [y for x,y in sorted(zip(m,m))] 


Y_rank = {y:i for i,y in enumerate(sorted(Y),1)}
Y = [Y_rank[y] for y in Y]

class BIT():
    def __init__(self, max_n):
        self.size = max_n + 1
        self.tree = [0] * self.size
        
    def get_sum(self,i):
        s = 0
        while i:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i < self.size:
            self.tree[i] += x
            i += i & -i

LD = [0] * N
bit = BIT(N)
for i,y in enumerate(Y):
    LD[i] = bit.get_sum(y)
    bit.add(y,1)

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

x = np.full(N+10,2,np.int64); x[0] = 1
pow2 = cumprod(x,MOD)

LD = np.array(LD,dtype=np.int32)
D = np.array(Y)-1
L = np.arange(N)
R = N-1-L
U = N-1-D
LU = L-LD
RU = U-LU
RD = D-LD

x = pow2[N] - 1 + pow2[LD] + pow2[RD] + pow2[RU] + pow2[LU]
x -= pow2[U] + pow2[D] + pow2[R] + pow2[L]

answer = x.sum() % MOD
print(answer)

Solution (Python):
import sys
input = sys.stdin.readline
mod = 998244353

def sub(a, b):
    return (a % mod + mod - b % mod) % mod


def main():
    n, = list(map(int, input().split()))
    t = 1
    dd = [t]
    for _ in range(n):
        t=2*t%mod
        dd.append(t)

    xs, ys = [], []
    for _ in range(n):
        x, y = list(map(int, input().split()))
        xs.append(x)
        ys.append(y)

    ini = sorted(list(range(n)), key=lambda x:xs[x])
    for i, yi in enumerate(sorted(list(range(n)), key=lambda x:ys[x])):
        ys[yi] = i 
    nys = [ys[i] for i in ini]

    tree = [0] * (n + 1)
    r = 0
    for i, y in enumerate(nys):
        j = y+1
        while j <= n:
            tree[j] += 1
            j += j & -j

        d = 0
        j = y
        while j > 0:
            d += tree[j]
            j -= j & -j

        c = i - d
        b = (n-1) - y - c
        a = (n-1) - b - c - d
        r = (r + sub(dd[b+c] + dd[c+d] + dd[d+a] + dd[a+b],
                dd[a] + dd[b] + dd[c] + dd[d])) % mod

    print(sub((dd[n] * n) % mod, (r+n)%mod))
main()

Solution (Python):
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
import sys
input = sys.stdin.readline
mod = 998244353

def sub(a, b):
    return (a % mod + mod - b % mod) % mod


def main():
    n, = list(map(int, input().split()))
    t = 1
    dd = [t]
    for _ in range(n):
        t=2*t%mod
        dd.append(t)

    xs, ys = [], []
    for _ in range(n):
        x, y = list(map(int, input().split()))
        xs.append(x)
        ys.append(y)

    ini = sorted(list(range(n)), key=lambda x:xs[x])
    for i, yi in enumerate(sorted(list(range(n)), key=lambda x:ys[x])):
        ys[yi] = i 
    nys = [ys[i] for i in ini]

    ss1 = Bit(n)
    r = 0
    for i, y in enumerate(nys):
        ss1.add(y+1, 1)
        d = ss1.sum(y)
        c = i - d
        b = (n-1) - y - c
        a = (n-1) - b - c - d
        r = (r + sub(dd[b+c] + dd[c+d] + dd[d+a] + dd[a+b],
                dd[a] + dd[b] + dd[c] + dd[d])) % mod

    print(sub((dd[n] * n) % mod, (r+n)%mod))
main()

Solution (Python):
import sys
input = sys.stdin.readline
mod = 998244353

def sub(a, b):
    return (a % mod + mod - b % mod) % mod

class SegTree:
    def __init__(self, init_val, n, ide_ele, seg_func):
        self.segfunc = seg_func
        self.num = 2**(n-1).bit_length()
        self.ide_ele = ide_ele
        self.seg=[self.ide_ele]*2*self.num
        for i in range(n):
            self.seg[i+self.num-1]=init_val[i]    
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) 
        
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k+1:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2])
        
    def query(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res=self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res

def main():
    n, = list(map(int, input().split()))
    t = 1
    dd = [t]
    for _ in range(n):
        t=2*t%mod
        dd.append(t)

    xs, ys = [], []
    for _ in range(n):
        x, y = list(map(int, input().split()))
        xs.append(x)
        ys.append(y)

    ini = sorted(list(range(n)), key=lambda x:xs[x])
    for i, yi in enumerate(sorted(list(range(n)), key=lambda x:ys[x])):
        ys[yi] = i 
    nys = [ys[i] for i in ini]

    ss1 = SegTree([0] * n, n, 0, lambda x,y:x+y)
    r = 0
    for i, y in enumerate(nys):
        ss1.update(y, 1)
        d = ss1.query(0, y)
        c = i - d
        b = (n-1) - y - c
        a = (n-1) - b - c - d
        r = (r + sub(dd[b+c] + dd[c+d] + dd[d+a] + dd[a+b],
                dd[a] + dd[b] + dd[c] + dd[d])) % mod

    print(sub((dd[n] * n) % mod, (r+n)%mod))
main()

Solution (Python):
import sys
input = sys.stdin.readline
mod = 998244353

def sub(a, b):
    return (a % mod + mod - b % mod) % mod

n, = list(map(int, input().split()))
t = 1
dd = [t]
for _ in range(n):
    t=2*t%mod
    dd.append(t)

def pow2(b):
    return dd[b]

class SegTree:
    def __init__(self, init_val, n, ide_ele, seg_func):
        self.segfunc = seg_func
        self.num = 2**(n-1).bit_length()
        self.ide_ele = ide_ele
        self.seg=[self.ide_ele]*2*self.num
        for i in range(n):
            self.seg[i+self.num-1]=init_val[i]    
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) 
        
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k+1:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2])
        
    def query(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res=self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res

def mm(a, b, c, d, n):
    return sub(pow2(b+c) + pow2(c+d) + pow2(d+a) + pow2(a+b),
                pow2(a) + pow2(b) + pow2(c) + pow2(d))

xs, ys = [], []
for _ in range(n):
    x, y = list(map(int, input().split()))
    xs.append(x)
    ys.append(y)

ini = sorted(list(range(n)), key=lambda x:xs[x])
for i, yi in enumerate(sorted(list(range(n)), key=lambda x:ys[x])):
    ys[yi] = i 
nys = [ys[i] for i in ini]

ss1 = SegTree([0] * n, n, 0, lambda x,y:x+y)
r = 0
for i, y in enumerate(nys):
    ss1.update(y, 1)
    d = ss1.query(0, y)
    c = i - d
    b = (n-1) - y - c
    a = (n-1) - b - c - d
    r = (r + mm(a, b, c, d, n)) % mod

print(sub((pow2(n) * n) % mod, (r+n)%mod))

Solution (Python):
import sys
input = sys.stdin.readline
mod = 998244353

def sub(a, b):
    return (a % mod + mod - b % mod) % mod

n, = list(map(int, input().split()))
t = 1
dd = [t]
for _ in range(n):
    t=2*t%mod
    dd.append(t)

def power(a, b):
    return dd[b]

class SegTree:
    def __init__(self, init_val, n, ide_ele, seg_func):
        self.segfunc = seg_func
        self.num = 2**(n-1).bit_length()
        self.ide_ele = ide_ele
        self.seg=[self.ide_ele]*2*self.num
        for i in range(n):
            self.seg[i+self.num-1]=init_val[i]    
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) 
        
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k+1:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2])
        
    def query(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res=self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res

def mm(a, b, c, d, n):
    return sub(power(2, b+c) + power(2, c+d) + power(2, d+a) + power(2, a+b),
                power(2, a) + power(2, b) + power(2, c) + power(2, d))

xs, ys = [], []
for _ in range(n):
    x, y = list(map(int, input().split()))
    xs.append(x)
    ys.append(y)

ini = sorted(list(range(n)), key=lambda x:xs[x])
for i, yi in enumerate(sorted(list(range(n)), key=lambda x:ys[x])):
    ys[yi] = i 
nys = [ys[i] for i in ini]

ss1 = SegTree([0] * n, n, 0, lambda x,y:x+y)
r = 0
for i, y in enumerate(nys):
    ss1.update(y, 1)
    d = ss1.query(0, y)
    c = i - d
    b = (n-1) - y - c
    a = (n-1) - b - c - d
    r = (r + mm(a, b, c, d, n)) % mod

print(sub((power(2, n) * n) % mod, (r+n)%mod))

Solution (Python):
import sys


class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0] * n

    def add(self, k, x):
        while k < self.n:
            self.bit[k] += x
            k |= k + 1

    def sum(self, k):
        s = 0
        while k >= 0:
            s += self.bit[k]
            k = (k & (k + 1)) - 1
        return s


input = sys.stdin.readline
MOD = 998244353
N = int(input())
X, Y = [], []
xs, ys = {}, {}
pow2 = [1]
for i in range(N):
    pow2.append((pow2[-1] * 2) % MOD)
for i in range(N):
    x, y = map(int, input().split())
    xs[x] = 0
    ys[y] = 0
    X.append(x)
    Y.append(y)

s = 0
for i in sorted(X):
    xs[i] = s
    s += 1

s = 0
for i in sorted(Y):
    ys[i] = s
    s += 1

for i in range(N):
    X[i] = xs[X[i]]
    Y[i] = ys[Y[i]]

P = [i for i in range(N)]
P = sorted(P, key=lambda k: X[k])


bit1 = BIT(N)
bit2 = BIT(N)

for i in range(N):
    bit2.add(i, 1)

ans = 0
for i in P:
    y = Y[i]
    bit2.add(y, -1)

    A = bit1.sum(y)
    B = bit2.sum(y)
    C = bit1.sum(N - 1) - bit1.sum(y - 1)
    D = bit2.sum(N - 1) - bit2.sum(y - 1)

    ans += pow2[N - 1]
    ans %= MOD
    ans += (pow2[A] - 1) * (pow2[D] - 1) * pow2[B] * pow2[C]
    ans %= MOD
    ans += pow2[A] * pow2[D] * (pow2[B] - 1) * (pow2[C] - 1)
    ans %= MOD
    ans += MOD - ((pow2[A] - 1) * (pow2[D] - 1) * (pow2[B] - 1) * (pow2[C] - 1)) % MOD
    ans %= MOD

    bit1.add(y, 1)

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

MOD = 998244353

class BIT:
  def __init__(self, n):
    self.size = n
    self.tree = [0 for _ in range(n+1)]
    self.p = 2**(n.bit_length() - 1)
    self.dep = n.bit_length()

  def get(self, i):
    s = 0
    while i > 0:
      s += self.tree[i]
      i -= i & -i
    return s

  def add(self, i, x):
    while i <= self.size:
      self.tree[i] += x
      i += (i & -i)

  def bl(self, v):
    if v <= 0:
      return -1
    s = 0
    k = self.p
    for _ in range(self.dep):
      if s + k <= self.size and self.tree[s+k] < v:
        s += k
        v -= self.tree[s+k]
      k //= 2
    return s + 1


def compress(L):
  L2 = list(set(L))
  L2.sort()
  C = {v: k for k, v in enumerate(L2, 1)}
  return L2, C


def main():
  n = int(input())
  ans = n * pow(2, n-1, MOD) % MOD
  xy = []
  for _ in range(n):
    xy.append(tuple(map(int, input().split())))
  xy.sort()
  py = [_xy[1] for _xy in xy]
  _, Cy = compress(py)
  py = [Cy[p] for p in py]
  BIT1 = BIT(n)

  for i in range(n):
    y = py[i]  
    c = BIT1.get(y)  
    d = y - c - 1  
    a = i - c  
    b = n - 1 - a - c - d  
    a2 = pow(2, a, MOD)
    b2 = pow(2, b, MOD)
    c2 = pow(2, c, MOD)
    d2 = pow(2, d, MOD)
    ans += (a2-1) * b2 * c2 * (d2-1)  
    ans += a2 * (b2-1) * (c2-1) * d2  
    ans -= (a2-1) * (b2-1) * (c2-1) * (d2-1)  
    ans %= MOD
    BIT1.add(y, 1)

  print(ans)


if __name__ == '__main__':
  main()

Solution (Python):
import itertools
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 998244353


class BinaryIndexedTree:
    
    def __init__(self, size):
        """
        :param int size:
        """
        self._bit = [0] * (size + 1)
        self._size = size

    def add(self, i, w):
        """
        i 番目に w を加える
        :param int i:
        :param int w:
        :return:
        """
        x = i
        while x <= self._size:
            self._bit[x] += w
            x += x & -x

    def sum(self, i):
        """
        0 番目から i 番目までの合計
        :param int i:
        :return:
        """
        ret = 0
        x = i
        while x > 0:
            ret += self._bit[x]
            x -= x & -x
        return ret

    def __len__(self):
        return self._size


def compress(li):
    """
    大小関係を保ったまま 1 以上の数値に圧縮する。
    scipy.stats.rankdata に近い感じ
    :param list li:
    :rtype: list of int
    """
    ret = [0] * len(li)
    rank = 0
    prev = None
    for a, i in sorted([(a, i) for i, a in enumerate(li)]):
        if a != prev:
            rank += 1
        ret[i] = rank
    return ret



N = int(sys.stdin.readline())
XY = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]


def test():
    ret = 0
    counts = defaultdict(int)
    for selection in itertools.product([0, 1], repeat=N):
        if not any(selection):
            continue
        xs = set([XY[i][0] for i, s in enumerate(selection) if s])
        ys = set([XY[i][1] for i, s in enumerate(selection) if s])
        xl = min(xs)
        xr = max(xs)
        yl = min(ys)
        yr = max(ys)
        r = 0
        for x, y in XY:
            r += xl <= x <= xr and yl <= y <= yr
        ret += r
        counts[r] += 1
    return ret


XY.sort()
Y = [y for _, y in XY]
Y = compress(Y)



LL = []
LG = []
GL = []
GG = []

bit = BinaryIndexedTree(size=N)
for i, y in enumerate(Y):
    ll = bit.sum(y)
    lg = i - ll
    gl = y - ll - 1
    gg = N - y - lg
    LL.append(ll)
    LG.append(lg)
    GL.append(gl)
    GG.append(gg)
    bit.add(y, 1)

pow2 = [1] * N
p = 1
for i in range(N - 1):
    p = p * 2 % MOD
    pow2[i + 1] = p


ans = 0
for ll, lg, gl, gg in zip(LL, LG, GL, GG):
    
    ans += pow2[N - 1]
    
    
    a = (pow2[ll] - 1) * (pow2[gg] - 1) % MOD * pow2[N - ll - gg - 1]
    
    b = (pow2[lg] - 1) * (pow2[gl] - 1) % MOD * pow2[N - lg - gl - 1]
    
    c = (pow2[ll] - 1) * (pow2[gg] - 1) % MOD * (pow2[lg] - 1) * (pow2[gl] - 1) % MOD
    ans += a + b - c
    ans %= MOD
print(ans)

Solution (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

class CombTools(object):
    def __init__(self, cap: int, mod: int):
        self.cap = cap
        self.mod = mod
        self.inv = self._calc_inv()
        self.fac = self._calc_fac()
        self.fac_inv = self._calc_fac_inv()

    def _calc_inv(self):
        inv = [0, 1]
        for i in range(2, self.cap+1):
            inv.append((self.mod - (self.mod // i) * inv[self.mod % i]) % self.mod)

        return inv

    def _calc_fac(self):
        fac = [1]
        for i in range(1, self.cap+1):
            fac.append((i * fac[-1]) % self.mod)

        return fac

    def _calc_fac_inv(self):
        fac_inv = [1]
        for i in range(1, self.cap+1):
            fac_inv.append((self.inv[i] * fac_inv[-1]) % self.mod)

        return fac_inv

    def nCr(self, n: int, r: int):
        
        if r > n:
            raise ValueError("n must be larger than r (n={}, r={})".format(n, r))

        
        return self.fac[n] * self.fac_inv[n-r] * self.fac_inv[r] % self.mod


class BIT(object):
    def __init__(self, n: int):
        self.bitree = [0]*(pow(2, (n-1).bit_length()) + 1)
        self.size = len(self.bitree) - 1

    def add(self, index, value):
        while index <= self.size:
            self.bitree[index] += value
            index += (-index) & index

    def get(self, index):
        ans = 0
        while index > 0:
            ans += self.bitree[index]
            index -= (-index) & index

        return ans



n = ni()
xs = []
ys = []
ps = []
MOD = 998244353
for _ in range(n):
    xi, yi = li()
    xs.append(xi)
    ys.append(yi)
    ps.append([xi, yi])


xcomp = {xi: i+1 for i, xi in enumerate(sorted(xs))}
ycomp = {yi: i+1 for i, yi in enumerate(sorted(ys))}
pcomp = [[xcomp[xi], ycomp[yi]] for xi, yi in zip(xs, ys)]
pcomp.sort()


ans = 0


ct = CombTools(n, MOD)
for i in range(1,n+1):
    ans += i * ct.nCr(n, i)
    ans %= MOD


leftBIT = BIT(n)
rightBIT = BIT(n)

for _, yi in pcomp:
    rightBIT.add(yi, 1)


pow2 = [pow(2, i, MOD) for i in range(n+1)]
for _, yi in pcomp:
    rightBIT.add(yi, -1)

    nw = leftBIT.get(n) - leftBIT.get(yi)
    ne = rightBIT.get(n) - rightBIT.get(yi)
    sw = leftBIT.get(yi)
    se = rightBIT.get(yi)

    ans += (pow2[nw] - 1) * (pow2[se] - 1) * (pow2[ne] * pow2[sw])
    ans += (pow2[sw] - 1) * (pow2[ne] - 1) * (pow2[se] * pow2[nw])
    ans -= (pow2[nw] - 1) * (pow2[ne] - 1) * (pow2[sw] - 1) * (pow2[se] - 1)
    ans %= MOD

    leftBIT.add(yi, 1)

print(ans)

Solution (Python):
import sys

class BIT:
    
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
        self.p = 2**(n.bit_length() - 1)
        self.dep = n.bit_length()
    
    def get(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
    
    def bl(self, v):
        if v <= 0:
            return -1
        s = 0
        k = self.p
        for _ in range(self.dep):
            if s + k <= self.size and self.tree[s+k] < v:
                s += k
                v -= self.tree[s+k]
            k //= 2
        return s + 1

def compress(L):
    L2 = list(set(L))
    L2.sort()
    C = {v : k for k, v in enumerate(L2, 1)}
    return L2, C

N = int(input())
mod = 998244353
ans = N*pow(2, N-1, mod)%mod

points = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N)]
points.sort()
_, PY = map(list, zip(*points))
_, Ci = compress(PY)
PY = [Ci[p] for p in PY]
T1 = BIT(N)
T2 = BIT(N)

for i in range(1, N+1):
    T2.add(i, 1)

for i in range(N):
    y = PY[i]
    c = T1.get(y)
    d = T2.get(y)-1 - c
    a = i - c
    b = N-1 - a - c - d
    T1.add(y, 1)
    a2 = pow(2, a, mod)
    b2 = pow(2, b, mod)
    c2 = pow(2, c, mod)
    d2 = pow(2, d, mod)
    ans += (a2-1)*b2*c2*(d2-1) + a2*(b2-1)*(c2-1)*d2 - (a2-1)*(b2-1)*(c2-1)*(d2-1)
    ans %= mod
print(ans)

Solution (Python):
import sys
import array

from operator import itemgetter

def main():



	input = sys.stdin.readline


	md = 998244353
	n = int(input())
	tl=n+1
	
	ft=[0]*tl

	xy = [[0]*2 for _ in range(n)]
	for i in range(n):
		xy[i] = [int(item) for item in input().split()]
	xy.sort(key=itemgetter(0))
	yxi = [y for x, y in xy]

	*YS, = set(yxi)
	YS.sort()
	yxi= list(map({e: i for i, e in enumerate(YS)}.__getitem__, yxi))

	ct=[0]*(n+1)
	ct[0]=1
	for i in range(1,n+1):
		ct[i]=ct[i-1]*2%md

	cnt=tuple(ct)

	def upd(i):
		i+=1
		while(i<=n):
			ft[i]+=1
			i+=i&-i

	def get(i):
		i+=1
		ret=0
		while(i!=0):
		    ret+=ft[i]
		    i-=i&-i
		return ret

	def calc(get,upd):
		for i, y in enumerate(yxi):
			v = get(y); upd(y)
			p1 = cnt[v]; p0 = cnt[y - v]
			q1 = cnt[i - v]; q0 = cnt[(n - y - 1) - (i - v)]
			yield (p0 + p1 + q0 + q1 - (p0 + q1) * (p1 + q0)) % md


	print((sum(calc(get,upd))+n*cnt[n] - n)%md)

main()