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.



Sample input:
3
-1 3
2 1
3 -2


Sample output:
13

Submission status: Time Limit Exceeded

Solution code:

class SegmentTree:
    def __init__(self, L, op, default_val):
        self.op = op
        self.default_val = default_val
        self.sz = len(L)
        self.n = 1
        self.s = 1
        for i in range(1000):
            self.n *= 2
            self.s += 1
            if self.n >= self.sz:
                break
        self.node = [self.default_val]*(2*self.n-1)

        for i in range(self.sz):
            self.node[i+self.n-1] = L[i]

        for i in range(self.n-2,-1,-1):
            self.node[i] = self.op(self.node[i*2+1],self.node[i*2+2])
    
    def add(self, a, x):
        k = a+self.n-1
        self.node[k] += x
        for i in range(1000):
            k = (k-1)//2
            self.node[k] = self.op(self.node[2*k+1], self.node[2*k+2])
            if k <= 0:
                break
    
    def get(self, l, r):
        res = self.default_val
        n = self.n
        if self.sz <= r or 0 > l:
            print("ERROR: the indice are wrong.")
            return False

        for i in range(self.s):
            count = 2**i-1
            a = (r+1)//n
            b = (l-1)//n
            if a-b == 3:
                res = self.op(self.node[count+b+1],res)
                res = self.op(self.node[count+b+2],res)
                right = a*n
                left = (b+1)*n-1
                break
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n
                left = (b+1)*n-1
                break
            n = n//2

        n1 = n//2        
        #left
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (left+1)//n1
            b = (l-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                left = (b+1)*n1-1                
            n1 = n1//2

        n1 = n//2        
        #right
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (r+1)//n1
            b = (right-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n1                
            n1 = n1//2
        return res

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

point.sort(lambda x: x[1])
for i in range(len(point)):
    point[i][1] = i

mod = 998244353

ans = ((N-4)*pow(2,N,mod)+3*N+4)%mod

pow2 = []
a = 1
for i in range(N+1):
    pow2.append(a)
    a *= 2
    a %= mod

point.sort(lambda x: x[0])

ST = SegmentTree([0]*len(point), lambda a,b:a+b, 0)


for i in range(len(point)):
    if point[i][1] > 0:
        ls = ST.get(0,point[i][1]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow2[ls]-1
    ans += pow2[rs]-1
    ans %= mod
    ST.add(point[i][1],1)

point.sort(lambda x: x[0],reverse=True)

ST = SegmentTree([0]*len(point), lambda a,b:a+b, 0)

for i in range(len(point)):
    if point[i][1] > 0:
        ls = ST.get(0,point[i][1]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow2[ls]-1
    ans += pow2[rs]-1
    ans %= mod
    ST.add(point[i][1],1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
class SegmentTree:
    def __init__(self, L, op, default_val):
        self.op = op
        self.default_val = default_val
        self.sz = len(L)
        self.n = 1
        self.s = 1
        for i in range(1000):
            self.n *= 2
            self.s += 1
            if self.n >= self.sz:
                break
        self.node = [self.default_val]*(2*self.n-1)

        for i in range(self.sz):
            self.node[i+self.n-1] = L[i]

        for i in range(self.n-2,-1,-1):
            self.node[i] = self.op(self.node[i*2+1],self.node[i*2+2])
    
    def add(self, a, x):
        k = a+self.n-1
        self.node[k] += x
        for i in range(1000):
            k = (k-1)//2
            self.node[k] = self.op(self.node[2*k+1], self.node[2*k+2])
            if k <= 0:
                break
    
    def get(self, l, r):
        res = self.default_val
        n = self.n
        if self.sz <= r or 0 > l:
            print("ERROR: the indice are wrong.")
            return False

        for i in range(self.s):
            count = 2**i-1
            a = (r+1)//n
            b = (l-1)//n
            if a-b == 3:
                res = self.op(self.node[count+b+1],res)
                res = self.op(self.node[count+b+2],res)
                right = a*n
                left = (b+1)*n-1
                break
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n
                left = (b+1)*n-1
                break
            n = n//2

        n1 = n//2        
        #left
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (left+1)//n1
            b = (l-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                left = (b+1)*n1-1                
            n1 = n1//2

        n1 = n//2        
        #right
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (r+1)//n1
            b = (right-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n1                
            n1 = n1//2
        return res

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

x = []
y = []
for p in point:
    x.append(p[0])
    y.append(p[1])

x.sort()
y.sort()

x_dict = {}
y_dict = {}

for i in range(len(x)):
    x_dict[x[i]] = i

for i in range(len(y)):
    y_dict[y[i]] = i

mod = 998244353

ans = ((N-4)*pow(2,N,mod)+3*N+4)%mod

pow2 = []
a = 1
for i in range(N+1):
    pow2.append(a)
    a *= 2
    a %= mod

point.sort(lambda x: x[0])

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)


for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow2[ls]-1
    ans += pow2[rs]-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

point.sort(lambda x: x[0],reverse=True)

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)

for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow2[ls]-1
    ans += pow2[rs]-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:

class SegmentTree:
    def __init__(self, L, op, default_val):
        self.op = op
        self.default_val = default_val
        self.sz = len(L)
        self.n = 1
        self.s = 1
        while self.n < self.sz:
            self.n *= 2
            self.s += 1
        self.node = [self.default_val]*(2*self.n-1)

        for i in range(self.sz):
            self.node[i+self.n-1] = L[i]

        for i in range(self.n-2,-1,-1):
            self.node[i] = self.op(self.node[i*2+1],self.node[i*2+2])
    
    def add(self, a, x):
        k = a+self.n-1
        self.node[k] += x
        for i in range(1000):
            k = (k-1)//2
            self.node[k] = self.op(self.node[2*k+1], self.node[2*k+2])
            if k <= 0:
                break
    
    def get(self, l, r):
        res = self.default_val
        n = self.n
        if self.sz <= r or 0 > l:
            print("ERROR: the indice are wrong.")
            return False

        for i in range(self.s):
            count = 2**i-1
            a = (r+1)//n
            b = (l-1)//n
            if a-b == 3:
                res = self.op(self.node[count+b+1],res)
                res = self.op(self.node[count+b+2],res)
                right = a*n
                left = (b+1)*n-1
                break
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n
                left = (b+1)*n-1
                break
            n = n//2

        n1 = n//2        
        #left
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (left+1)//n1
            b = (l-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                left = (b+1)*n1-1                
            n1 = n1//2

        n1 = n//2        
        #right
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (r+1)//n1
            b = (right-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n1                
            n1 = n1//2
        return res

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

x = []
y = []
for p in point:
    x.append(p[0])
    y.append(p[1])

x.sort()
y.sort()

x_dict = {}
y_dict = {}

for i in range(len(x)):
    x_dict[x[i]] = i

for i in range(len(y)):
    y_dict[y[i]] = i

mod = 998244353

ans = ((N-4)*pow(2,N,mod)+3*N+4)%mod

pow2 = []
a = 1
for i in range(N+1):
    pow2.append(a)
    a *= 2
    a %= mod

point.sort(lambda x: x[0])

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)


for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow2[ls]-1
    ans += pow2[rs]-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

point.sort(lambda x: x[0],reverse=True)

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)

for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow2[ls]-1
    ans += pow2[rs]-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:



class SegmentTree:
    def __init__(self, L, op, default_val):
        self.op = op
        self.default_val = default_val
        self.sz = len(L)
        self.n = 1
        self.s = 1
        while self.n < self.sz:
            self.n *= 2
            self.s += 1
        self.node = [self.default_val]*(2*self.n-1)

        for i in range(self.sz):
            self.node[i+self.n-1] = L[i]

        for i in range(self.n-2,-1,-1):
            self.node[i] = self.op(self.node[i*2+1],self.node[i*2+2])
    
    def add(self, a, x):
        k = a+self.n-1
        self.node[k] += x
        for i in range(1000):
            k = (k-1)//2
            self.node[k] = self.op(self.node[2*k+1], self.node[2*k+2])
            if k <= 0:
                break
    
    def get(self, l, r):
        res = self.default_val
        n = self.n
        if self.sz <= r or 0 > l:
            print("ERROR: the indice are wrong.")
            return False

        for i in range(self.s):
            count = 2**i-1
            a = (r+1)//n
            b = (l-1)//n
            if a-b == 3:
                res = self.op(self.node[count+b+1],res)
                res = self.op(self.node[count+b+2],res)
                right = a*n
                left = (b+1)*n-1
                break
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n
                left = (b+1)*n-1
                break
            n = n//2

        n1 = n//2        
        #left
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (left+1)//n1
            b = (l-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                left = (b+1)*n1-1                
            n1 = n1//2

        n1 = n//2        
        #right
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (r+1)//n1
            b = (right-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n1                
            n1 = n1//2
        return res

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

x = []
y = []
for p in point:
    x.append(p[0])
    y.append(p[1])

x.sort()
y.sort()

x_dict = {}
y_dict = {}

for i in range(len(x)):
    x_dict[x[i]] = i

for i in range(len(y)):
    y_dict[y[i]] = i

mod = 998244353

ans = ((N-4)*pow(2,N,mod)+3*N+4)%mod


point.sort(lambda x: x[0])

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)


for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow(2,ls,mod)-1
    ans += pow(2,rs,mod)-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

point.sort(lambda x: x[0],reverse=True)

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)

for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow(2,ls,mod)-1
    ans += pow(2,rs,mod)-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:

class SegmentTree:
    def __init__(self, L, op, default_val):
        self.op = op
        self.default_val = default_val
        self.sz = len(L)
        self.n = 1
        self.s = 1
        while self.n < self.sz:
            self.n *= 2
            self.s += 1
        self.node = [self.default_val]*(2*self.n-1)

        for i in range(self.sz):
            self.node[i+self.n-1] = L[i]

        for i in range(self.n-2,-1,-1):
            self.node[i] = self.op(self.node[i*2+1],self.node[i*2+2])
    
    def add(self, a, x):
        k = a+self.n-1
        self.node[k] += x
        for i in range(1000):
            k = (k-1)//2
            self.node[k] = self.op(self.node[2*k+1], self.node[2*k+2])
            if k <= 0:
                break
    
    def get(self, l, r):
        res = self.default_val
        n = self.n
        if self.sz <= r or 0 > l:
            print("ERROR: the indice are wrong.")
            return False

        for i in range(self.s):
            count = 2**i-1
            a = (r+1)//n
            b = (l-1)//n
            if a-b == 3:
                res = self.op(self.node[count+b+1],res)
                res = self.op(self.node[count+b+2],res)
                right = a*n
                left = (b+1)*n-1
                break
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n
                left = (b+1)*n-1
                break
            n = n//2

        n1 = n//2        
        #left
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (left+1)//n1
            b = (l-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                left = (b+1)*n1-1                
            n1 = n1//2

        n1 = n//2        
        #right
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (r+1)//n1
            b = (right-1)//n1
            if a-b == 2:
                res = self.op(self.node[count+b+1],res)
                right = a*n1                
            n1 = n1//2
        return res

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

x = []
y = []
for p in point:
    x.append(p[0])
    y.append(p[1])

x.sort()
y.sort()

x_dict = {}
y_dict = {}

for i in range(len(x)):
    x_dict[x[i]] = i

for i in range(len(y)):
    y_dict[y[i]] = i

mod = 998244353

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


point.sort(lambda x: x[0])

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)


for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans -= 4*(pow(2,i,mod)-1)
    ans += pow(2,ls,mod)-1
    ans += pow(2,rs,mod)-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

point.sort(lambda x: x[0],reverse=True)

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)

for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow(2,ls,mod)-1
    ans += pow(2,rs,mod)-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:


class SegmentTree:
    def __init__(self, L, op, default_val):
        self.op = op
        self.default_val = default_val
        self.sz = len(L)
        self.n = 1
        self.s = 1
        while self.n < self.sz:
            self.n *= 2
            self.s += 1
        self.node = [self.default_val]*(2*self.n-1)

        for i in range(self.sz):
            self.node[i+self.n-1] = L[i]

        for i in range(self.n-2,-1,-1):
            self.node[i] = self.op(self.node[i*2+1],self.node[i*2+2])
    
    def add(self, a, x):
        k = a+self.n-1
        self.node[k] += x
        for i in range(1000):
            k = (k-1)//2
            self.node[k] = self.op(self.node[2*k+1], self.node[2*k+2])
            if k <= 0:
                break
    
    def get(self, l, r):
        result_list = []
        n = self.n
        if self.sz <= r or 0 > l:
            print("ERROR: the indice are wrong.")
            return False

        for i in range(self.s):
            count = 2**i-1
            a = (r+1)//n
            b = (l-1)//n
            if a-b == 3:
                result_list.append(count+b+1)
                result_list.append(count+b+2)
                right = a*n
                left = (b+1)*n-1
                break
            if a-b == 2:
                result_list.append(count+b+1)
                right = a*n
                left = (b+1)*n-1
                break
            n = n//2

        n1 = n//2        
        #left
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (left+1)//n1
            b = (l-1)//n1
            if a-b == 2:
                result_list.append(count+b+1)
                left = (b+1)*n1-1                
            n1 = n1//2

        n1 = n//2        
        #right
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (r+1)//n1
            b = (right-1)//n1
            if a-b == 2:
                result_list.append(count+b+1)
                right = a*n1                
            n1 = n1//2
        #print(result_list)
        res = self.default_val
        for r in result_list:
            res = self.op(self.node[r],res)
        return res

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

x = []
y = []
for p in point:
    x.append(p[0])
    y.append(p[1])

x.sort()
y.sort()

x_dict = {}
y_dict = {}

for i in range(len(x)):
    x_dict[x[i]] = i

for i in range(len(y)):
    y_dict[y[i]] = i

mod = 998244353

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


point.sort(lambda x: x[0])

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)


for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans -= 4*(pow(2,i,mod)-1)
    ans += pow(2,ls,mod)-1
    ans += pow(2,rs,mod)-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

point.sort(lambda x: x[0],reverse=True)

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)

for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow(2,ls,mod)-1
    ans += pow(2,rs,mod)-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Aug  4 22:01:34 2019

@author: miz18
"""

class SegmentTree:
    def __init__(self, L, op, default_val):
        self.op = op
        self.default_val = default_val
        self.sz = len(L)
        self.n = 1
        self.s = 1
        while self.n < self.sz:
            self.n *= 2
            self.s += 1
        self.node = [self.default_val]*(2*self.n-1)

        for i in range(self.sz):
            self.node[i+self.n-1] = L[i]

        for i in range(self.n-2,-1,-1):
            self.node[i] = self.op(self.node[i*2+1],self.node[i*2+2])
    
    def add(self, a, x):
        k = a+self.n-1
        self.node[k] += x
        while k > 0:
            k = (k-1)//2
            self.node[k] = self.op(self.node[2*k+1], self.node[2*k+2])
    
    def get(self, l, r):
        result_list = []
        n = self.n
        if self.sz <= r or 0 > l:
            print("ERROR: the indice are wrong.")
            return False

        for i in range(self.s):
            count = 2**i-1
            a = (r+1)//n
            b = (l-1)//n
            if a-b == 3:
                result_list.append(count+b+1)
                result_list.append(count+b+2)
                right = a*n
                left = (b+1)*n-1
                break
            if a-b == 2:
                result_list.append(count+b+1)
                right = a*n
                left = (b+1)*n-1
                break
            n = n//2

        n1 = n//2        
        #left
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (left+1)//n1
            b = (l-1)//n1
            if a-b == 2:
                result_list.append(count+b+1)
                left = (b+1)*n1-1                
            n1 = n1//2

        n1 = n//2        
        #right
        for j in range(i+1,self.s):
            count = 2**j-1 
            a = (r+1)//n1
            b = (right-1)//n1
            if a-b == 2:
                result_list.append(count+b+1)
                right = a*n1                
            n1 = n1//2
        #print(result_list)
        res = self.default_val
        for r in result_list:
            res = self.op(self.node[r],res)
        return res

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

x = []
y = []
for p in point:
    x.append(p[0])
    y.append(p[1])

x.sort()
y.sort()

x_dict = {}
y_dict = {}

for i in range(len(x)):
    x_dict[x[i]] = i

for i in range(len(y)):
    y_dict[y[i]] = i

mod = 998244353

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


point.sort(lambda x: x[0])

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)


for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans -= 4*(pow(2,i,mod)-1)
    ans += pow(2,ls,mod)-1
    ans += pow(2,rs,mod)-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

point.sort(lambda x: x[0],reverse=True)

ST = SegmentTree([0]*len(y), lambda a,b:a+b, 0)

for i in range(len(point)):
    if y_dict[point[i][1]] > 0:
        ls = ST.get(0,y_dict[point[i][1]]-1)
    else:
        ls = 0
    rs = i-ls
    ans += pow(2,ls,mod)-1
    ans += pow(2,rs,mod)-1
    ans %= mod
    ST.add(y_dict[point[i][1]],1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import bisect

MOD = 998244353  # type: int

class BitIndexedTree(object):
    def __init__(self, N):
        self.V = [0] * N
        self.N = N
    
    def __getitem__(self, key):
        return self.sum(key, key+1)
    
    def __setitem__(self, key, value):
        diff = value - self[key]
        self.add(diff, key)

    def add(self, w, a):
        if w == 0:
            return
        x = a
        while x <= self.N:
            self.V[x-1] += w
            x += x & -x
    
    def sum(self, start=0, end=0):
        if end <= start:
            return 0
        if start > 0:
            return self.sum(0, end) - self.sum(0, start)
        x = end
        ret = 0
        while x > 0:
            ret += self.V[x-1]
            x -= x & -x
        return ret


def solve(N: int, P: "List[tuple]"):
    res = 0
    
    P.sort(key=lambda x: x[1])
    for i, p in enumerate(P):
        p[1] = i
    
    P.sort()
    for i, p in enumerate(P):
        p[0] = i
    left = BitIndexedTree(N)
    right = BitIndexedTree(N)

    ll = [0] * N
    lg = [0] * N
    gl = [0] * N
    gg = [0] * N

    for i, p in enumerate(P):
        ll[i] = left.sum(0, p[1])
        lg[i] = i-ll[i]
        left.add(1, p[1]+1)

    for i, p in enumerate(P[::-1]):
        gl[-1-i] = right.sum(0, p[1])
        gg[-1-i] = i-gl[-1-i]
        right.add(1, p[1]+1)

    for a1, a2, a3, a4 in zip(gg, lg, ll, gl):
        b1 = pow(2, a1, MOD)
        b2 = pow(2, a2, MOD)
        b3 = pow(2, a3, MOD)
        b4 = pow(2, a4, MOD)

        res += (b1 - 1) * (b3 - 1) * b2 * b4
        res += (b2 - 1) * (b4 - 1) * b1 * b3

        res -= (b1 - 1) * (b3 - 1) * (b2 - 1) * (b4 - 1)

        res += b1 * b2 * b3 * b4

        res %= MOD
    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [()] * (N)  # type: "List[tuple]" 
    for i in range(N):
        x = int(next(tokens))
        y = int(next(tokens))
        P[i] = [x, y]
    solve(N, P)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import *
from heapq import *

def f(n):
    hp = []
    for _ in range(n):
        heappush(hp, list(map(int, input().split())))
    cy = [[0] * 4 for _ in range(n)]
    xy = []
    ys = []
    for i in range(n):
        x, y = heappop(hp)
        s = bisect(ys, y)
        cy[i][3] = pow(2, s, md)
        cy[i][2] = pow(2, i - s, md)
        ys.insert(s, y)
        xy.append([x, y])
    ys = []
    for i in range(n - 1, -1, -1):
        x, y = xy[i]
        s = bisect(ys, y)
        cy[i][0] = pow(2, s, md)
        cy[i][1] = pow(2, n - 1 - i - s, md)
        ys.insert(s, y)
    ans = pow(2, n - 1, md) * n % md
    for i in range(n):
        c0, c1, c2, c3 = cy[i]
        ans = (ans + (c0 - 1) * (c2 - 1) * c1 * c3 + (c1 - 1) * (c3 - 1) * c0 * c2
               - (c0 - 1) * (c1 - 1) * (c2 - 1) * (c3 - 1)) % md
    print(ans)

md = 998244353
n = int(input())
f(n)

Submission status: Time Limit Exceeded

Solution code:
from bisect import *
from heapq import *

md = 998244353
n = int(input())
hp = []
for _ in range(n):
    heappush(hp, list(map(int, input().split())))
cy = [[0] * 4 for _ in range(n)]
xy = []
ys = []
for i in range(n):
    x, y = heappop(hp)
    s = bisect(ys, y)
    cy[i][3] = pow(2, s, md)
    cy[i][2] = pow(2, i - s, md)
    ys.insert(s, y)
    xy.append([x, y])
ys = []
for i in range(n - 1, -1, -1):
    x, y = xy[i]
    s = bisect(ys, y)
    cy[i][0] = pow(2, s, md)
    cy[i][1] = pow(2, n - 1 - i - s, md)
    ys.insert(s, y)
ans = pow(2, n - 1, md) * n % md
for i in range(n):
    c0, c1, c2, c3 = cy[i]
    ans = (ans + (c0 - 1) * (c2 - 1) * c1 * c3 + (c1 - 1) * (c3 - 1) * c0 * c2
           - (c0 - 1) * (c1 - 1) * (c2 - 1) * (c3 - 1)) % md
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import bisect

MOD = 998244353  # type: int

class BitIndexedTree(object):
    def __init__(self, N):
        self.V = [0] * N
        self.N = N
    
    def __getitem__(self, key):
        return self.sum(key, key+1)
    
    def __setitem__(self, key, value):
        diff = value - self[key]
        self.add(diff, key)

    def add(self, w, a):
        if w == 0:
            return
        x = a
        while x <= self.N:
            self.V[x-1] += w
            x += x & -x
    
    def sum(self, start=0, end=0):
        if end <= start:
            return 0
        if start > 0:
            return self.sum(0, end) - self.sum(0, start)
        x = end
        ret = 0
        while x > 0:
            ret += self.V[x-1]
            x -= x & -x
        return ret


def solve(N: int, P: "List[tuple]"):
    res = 0
    
    P.sort(key=lambda x: x[1])
    for i, p in enumerate(P):
        p[1] = i
    
    P.sort()
    for i, p in enumerate(P):
        p[0] = i
    left = BitIndexedTree(N)
    right = BitIndexedTree(N)

    ll = [0] * N
    lg = [0] * N
    gl = [0] * N
    gg = [0] * N

    for i, p in enumerate(P):
        ll[i] = left.sum(0, p[1])
        lg[i] = i-ll[i]
        left.add(1, p[1]+1)

    for i, p in list(enumerate(P))[::-1]:
        gl[i] = right.sum(0, p[1])
        gg[i] = N-i-1-gl[i]
        right.add(1, p[1]+1)

    for a1, a2, a3, a4 in zip(gg, lg, ll, gl):
        res += ((1 << a3) - 1) * ((1 << a1) - 1) * (1 << (a2+a4))
        res += ((1 << a2) - 1) * ((1 << a4) - 1) * (1 << (a3+a1))

        res -= ((1 << a2) - 1) * ((1 << a4) - 1) * ((1 << a1) - 1) * ((1 << a3) - 1)

        res += (1 << N-1)

        res %= MOD
    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [()] * (N)  # type: "List[tuple]" 
    for i in range(N):
        x = int(next(tokens))
        y = int(next(tokens))
        P[i] = [x, y]
    solve(N, P)

if __name__ == '__main__':
    main()