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 BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

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

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

N = int(input())
xy = [list(map(int,input().split())) for _ in range(N)]
xy.sort(key=lambda x:x[1])
for i in range(N):
    xy[i][1] = i
xy.sort()
y = [xy[i][1] for i in range(N)]

mod = 998244353
p2 = [1]*(N+1)
for i in range(N):
    p2[i+1] = p2[i]*2%mod

bit = BIT(N)

ans = 0

for i in range(N):
    bit.add(y[i],1)
    n1 = i+1-bit.sum(y[i])
    n2 = i-n1
    n3 = N-y[i]-1-n1
    n4 = N-i-1-n3
    ans += 1
    ans += (p2[n1]-1)+(p2[n2]-1)+(p2[n3]-1)+(p2[n4]-1)
    ans += (p2[n1]-1)*(p2[n2]-1)+(p2[n3]-1)*(p2[n4]-1)+(p2[n1]-1)*(p2[n3]-1)+(p2[n2]-1)*(p2[n4]-1)+2*(p2[n1]-1)*(p2[n4]-1)+2*(p2[n2]-1)*(p2[n3]-1)
    ans += 2*((p2[n1]-1)*(p2[n2]-1)*(p2[n3]-1)+(p2[n1]-1)*(p2[n2]-1)*(p2[n4]-1)+(p2[n1]-1)*(p2[n3]-1)*(p2[n4]-1)+(p2[n2]-1)*(p2[n3]-1)*(p2[n4]-1))
    ans += 2*(p2[n1]-1)*(p2[n2]-1)*(p2[n3]-1)*(p2[n4]-1)
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

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

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

N = int(input())
xy = [list(map(int,input().split())) for _ in range(N)]
xy.sort(key=lambda x:x[1])
for i in range(N):
    xy[i][1] = i
xy.sort()
y = [xy[i][1] for i in range(N)]

mod = 998244353
p2 = [1]*(N+1)
for i in range(N):
    p2[i+1] = p2[i]*2%mod

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

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

ans = 0

for i in range(N):
    bit1.add(y[i],1)
    bit2.add(y[i],-1)
    n1 = bit1.sum(N-1)-bit1.sum(y[i])
    n2 = bit1.sum(y[i]-1)
    n3 = bit2.sum(N-1)-bit2.sum(y[i])
    n4 = bit2.sum(y[i]-1)
    ans += 1
    ans += (p2[n1]-1)+(p2[n2]-1)+(p2[n3]-1)+(p2[n4]-1)
    ans += (p2[n1]-1)*(p2[n2]-1)+(p2[n3]-1)*(p2[n4]-1)+(p2[n1]-1)*(p2[n3]-1)+(p2[n2]-1)*(p2[n4]-1)+2*(p2[n1]-1)*(p2[n4]-1)+2*(p2[n2]-1)*(p2[n3]-1)
    ans += 2*((p2[n1]-1)*(p2[n2]-1)*(p2[n3]-1)+(p2[n1]-1)*(p2[n2]-1)*(p2[n4]-1)+(p2[n1]-1)*(p2[n3]-1)*(p2[n4]-1)+(p2[n2]-1)*(p2[n3]-1)*(p2[n4]-1))
    ans += 2*(p2[n1]-1)*(p2[n2]-1)*(p2[n3]-1)*(p2[n4]-1)
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
mod=998244353

def bit_add(i):
    while i<=n:
        tree[i]+=1
        i+=i&(-i)

def bit_sum(i):
    s=0
    while i>0:
        s+=tree[i]
        i-=i&(-i)
    return s

n=int(input())
xy=[[i+1]+list(map(int,input().split())) for i in range(n)]
data=[[] for i in range(n+1)]
for i in [1,2]:
    xy.sort(key=itemgetter(i))
    for j in range(n):
        data[xy[j][0]].append(j+1)
        
lsls=[1]*(n+1)
for i in range(1,n+1):
    lsls[i]=lsls[i-1]*2%mod
for i in range(n+1):
    lsls[i]-=1

data=sorted(data[1:])
lst=[]
for i1,i2 in data:
    lst.append(i2)

s=pow(2,n,mod)-1
tree=[0]*(n+1)
ans=0
for i in range(n):
    y=lst[i]
    num=bit_sum(y)
    bit_add(y)
    a,b,c,d=i-num,num,y-1-num,n-y-i+num
    t=(lsls[i]+lsls[n-i-1]+lsls[n-y]+lsls[y-1]-(lsls[a]+lsls[b]+lsls[c]+lsls[d]))%mod
    ans=(ans+s-t)%mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import itemgetter
input = sys.stdin.readline


def compress_coordinate(x: list, key=None, reverse=False):
    zipped = {}
    unzipped = {}
    for i, xi in enumerate(sorted(set(x), key=None, reverse=reverse)):
        zipped[xi] = i + 1
        unzipped[i + 1] = xi
    return zipped, unzipped


class BIT1():
    """
    Binary Indexed Tree (1-indexed)
    """
    def __init__(self, n):
        self.n = n
        self.bit = [0] * (self.n + 1)

    def add(self, idx, x):
        # add x to idx-th element
        # idx: 1-indexed
        while idx <= self.n:
            self.bit[idx] += x
            idx += (idx & (-idx))

    def sum(self, idx):
        # get sum of [1, idx]
        # idx: 1-indexed
        s = 0
        while idx:
            s += self.bit[idx]
            idx -= (idx & (-idx))
        return s

    def bisect_left(self, w):
        # condition : always all element is not minus
        # return minimum idx where bit.sum(idx) >= w
        if w <= 0:
            return 0
        idx = 0  # self.bit[idx] < w
        k = 1 << ((self.n).bit_length() - 1)
        while k > 0:
            if idx + k <= self.n and self.bit[idx + k] < w:
                w -= self.bit[idx + k]
                idx += k
            k = k >> 1
        return idx + 1


MOD = 998244353
n = int(input())
x_list = []
y_list = []
for i in range(n):
    x, y = map(int, input().split())
    x_list.append(x)
    y_list.append(y)
x_zipped, x_unzipped = compress_coordinate(x_list)
y_zipped, y_unzipped = compress_coordinate(y_list)

p = []
for i, (x, y) in enumerate(zip(x_list, y_list)):
    p.append((x_zipped[x], y_zipped[y], i))

info = [[0] * 5 for _ in range(n)]
p.sort(key=itemgetter(0))
# print(*p)
bit1 = BIT1(n)
for m, (px, py, idx) in enumerate(p):
    bit1.add(py, 1)
    s_left = bit1.sum(py) - 1
    s_right = (m + 1) - s_left - 1
    info[idx][2] = s_right
    info[idx][3] = s_left

p.sort(key=itemgetter(0), reverse=True)
# print(*p)

bit2 = BIT1(n)
for m, (px, py, idx) in enumerate(p):
    bit2.add(py, 1)
    s_left = bit2.sum(py) - 1
    s_right = (m + 1) - s_left - 1
    info[idx][1] = s_right
    info[idx][4] = s_left

ans = 0
for _, c1, c2, c3, c4 in info:
    c1 = pow(2, c1, MOD) - 1
    c2 = pow(2, c2, MOD) - 1
    c3 = pow(2, c3, MOD) - 1
    c4 = pow(2, c4, MOD) - 1
    ans += 1
    ans += c1 + c2 + c3 + c4
    ans += c1 * c3 * 2 + c2 * c4 * 2
    ans += c1 * c2 + c2 * c3 + c3 * c4 + c4 * c1
    ans += (c2 * c3 * c4 + c1 * c3 * c4 + c1 * c2 * c4 + c1 * c2 * c3) * 2
    ans += c1 * c2 * c3 * c4 * 2
    ans %= MOD

# print(*info, sep='\n')
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import itemgetter
input = sys.stdin.readline


def compress_coordinate(x: list, key=None, reverse=False):
    zipped = {}
    unzipped = {}
    for i, xi in enumerate(sorted(set(x), key=None, reverse=reverse)):
        zipped[xi] = i + 1
        unzipped[i + 1] = xi
    return zipped, unzipped


class BIT1():
    """
    Binary Indexed Tree (1-indexed)
    """
    def __init__(self, n):
        self.n = n
        self.bit = [0] * (self.n + 1)
        self.data = [0] * (self.n + 1)

    def add(self, idx, x):
        # add x to idx-th element
        # idx: 1-indexed
        self.data[idx] += x
        while idx <= self.n:
            self.bit[idx] += x
            idx += (idx & (-idx))

    def sum(self, idx):
        # get sum of [1, idx]
        # idx: 1-indexed
        s = 0
        while idx:
            s += self.bit[idx]
            idx -= (idx & (-idx))
        return s

    def bisect_left(self, w):
        # condition : always all element is not minus
        # return minimum idx where bit.sum(idx) >= w
        if w <= 0:
            return 0
        idx = 0  # self.bit[idx] < w
        k = 1 << ((self.n).bit_length() - 1)
        while k > 0:
            if idx + k <= self.n and self.bit[idx + k] < w:
                w -= self.bit[idx + k]
                idx += k
            k = k >> 1
        return idx + 1


MOD = 998244353
n = int(input())
x_list = []
y_list = []
for i in range(n):
    x, y = map(int, input().split())
    x_list.append(x)
    y_list.append(y)
x_zipped, x_unzipped = compress_coordinate(x_list)
y_zipped, y_unzipped = compress_coordinate(y_list)

p = []
for i, (x, y) in enumerate(zip(x_list, y_list)):
    p.append((x_zipped[x], y_zipped[y], i))

info = [[0] * 5 for _ in range(n)]
p.sort(key=itemgetter(0))
# print(*p)
bit1 = BIT1(n)
for m, (px, py, idx) in enumerate(p):
    bit1.add(py, 1)
    s_left = bit1.sum(py) - 1
    s_right = (m + 1) - s_left - 1
    info[idx][2] = s_right
    info[idx][3] = s_left

p.sort(key=itemgetter(0), reverse=True)
# print(*p)

bit2 = BIT1(n)
for m, (px, py, idx) in enumerate(p):
    bit2.add(py, 1)
    s_left = bit2.sum(py) - 1
    s_right = (m + 1) - s_left - 1
    info[idx][1] = s_right
    info[idx][4] = s_left

ans = 0
for _, c1, c2, c3, c4 in info:
    c1 = pow(2, c1, MOD) - 1
    c2 = pow(2, c2, MOD) - 1
    c3 = pow(2, c3, MOD) - 1
    c4 = pow(2, c4, MOD) - 1
    ans += 1
    ans += c1 + c2 + c3 + c4
    ans += c1 * c3 * 2 + c2 * c4 * 2
    ans += c1 * c2 + c2 * c3 + c3 * c4 + c4 * c1
    ans += (c2 * c3 * c4 + c1 * c3 * c4 + c1 * c2 * c4 + c1 * c2 * c3) * 2
    ans += c1 * c2 * c3 * c4 * 2
    ans %= MOD

# print(*info, sep='\n')
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline


def compress_coordinate(x: list, key=None, reverse=False):
    zipped = {}
    unzipped = {}
    for i, xi in enumerate(sorted(set(x), key=None, reverse=reverse)):
        zipped[xi] = i + 1
        unzipped[i + 1] = xi
    return zipped, unzipped


class BIT1():
    """
    Binary Indexed Tree (1-indexed)
    """
    def __init__(self, n):
        self.n = n
        self.bit = [0] * (self.n + 1)
        self.data = [0] * (self.n + 1)

    def add(self, idx, x):
        # add x to idx-th element
        # idx: 1-indexed
        self.data[idx] += x
        while idx <= self.n:
            self.bit[idx] += x
            idx += (idx & (-idx))

    def sum(self, idx):
        # get sum of [1, idx]
        # idx: 1-indexed
        s = 0
        while idx:
            s += self.bit[idx]
            idx -= (idx & (-idx))
        return s

    def bisect_left(self, w):
        # condition : always all element is not minus
        # return minimum idx where bit.sum(idx) >= w
        if w <= 0:
            return 0
        idx = 0  # self.bit[idx] < w
        k = 1 << ((self.n).bit_length() - 1)
        while k > 0:
            if idx + k <= self.n and self.bit[idx + k] < w:
                w -= self.bit[idx + k]
                idx += k
            k = k >> 1
        return idx + 1


MOD = 998244353
n = int(input())
x_list = []
y_list = []
for i in range(n):
    x, y = map(int, input().split())
    x_list.append(x)
    y_list.append(y)
x_zipped, x_unzipped = compress_coordinate(x_list)
y_zipped, y_unzipped = compress_coordinate(y_list)

p = []
for i, (x, y) in enumerate(zip(x_list, y_list)):
    p.append((x_zipped[x], y_zipped[y], i))

info = [[0] * 5 for _ in range(n)]
p.sort()
# print(*p)
bit1 = BIT1(n)
for m, (px, py, idx) in enumerate(p):
    bit1.add(py, 1)
    s_left = bit1.sum(py) - 1
    s_right = (m + 1) - s_left - 1
    info[idx][2] = s_right
    info[idx][3] = s_left

p.sort(reverse=True)
# print(*p)

bit2 = BIT1(n)
for m, (px, py, idx) in enumerate(p):
    bit2.add(py, 1)
    s_left = bit2.sum(py) - 1
    s_right = (m + 1) - s_left - 1
    info[idx][1] = s_right
    info[idx][4] = s_left

ans = 0
for _, c1, c2, c3, c4 in info:
    c1 = pow(2, c1, MOD) - 1
    c2 = pow(2, c2, MOD) - 1
    c3 = pow(2, c3, MOD) - 1
    c4 = pow(2, c4, MOD) - 1
    ans += 1
    ans += c1 + c2 + c3 + c4
    ans += c1 * c3 * 2 + c2 * c4 * 2
    ans += c1 * c2 + c2 * c3 + c3 * c4 + c4 * c1
    ans += (c2 * c3 * c4 + c1 * c3 * c4 + c1 * c2 * c4 + c1 * c2 * c3) * 2
    ans += c1 * c2 * c3 * c4 * 2
    ans %= MOD

# print(*info, sep='\n')
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=998244353
input=lambda :sys.stdin.readline().rstrip()
class BIT(object):
    def __init__(self,A,dot,e,inv=None):
        n=len(A)
        self.__n=n
        self.__dot=dot
        self.__e=e
        self.__inv=inv
        self.__node=['$']+A # 1-indexed
        for i in range(1,n+1):
            j=i+(i&-i)
            if(j<=n): self.__node[j]=dot(self.__node[i],self.__node[j])

    def add(self,i,w=1):
        i+=1
        while(i<=self.__n):
            self.__node[i]=self.__dot(self.__node[i],w)
            i+=i&-i

    def sum(self,i):
        res=self.__e
        while(i>0):
            res=self.__dot(res,self.__node[i])
            i-=i&-i
        return res

def resolve():
    n=int(input())
    R=[0]*n
    for i in range(n):
        R[i]=list(map(int,input().split()))

    # coordinate compress
    R.sort(lambda x:x[1])
    for i in range(n):
        R[i][1]=i
    R.sort()
    for i in range(n):
        R[i][0]=i

    # line sweep
    Q=[[0]*5 for _ in range(n)]
    from operator import add
    dot=add
    e=0
    bit=BIT([0]*n,dot,e)
    # left
    for i in range(n):
        y=R[i][1]
        bit.add(y)
        L=bit.sum(y)
        Q[i][2]=i-L
        Q[i][3]=L
    # right
    bit=BIT([0]*n,dot,e)
    for i in range(n):
        y=R[-i-1][1]
        bit.add(y)
        L=bit.sum(y)
        Q[-i-1][1]=i-L
        Q[-i-1][4]=L

    # count
    ans=0
    for i in range(n):
        a=pow(2,Q[i][1],MOD)
        b=pow(2,Q[i][2],MOD)
        c=pow(2,Q[i][3],MOD)
        d=pow(2,Q[i][4],MOD)
        ans+=(a-1)*b*(c-1)*d%MOD
        ans+=a*(b-1)*c*(d-1)%MOD
        ans-=(a-1)*(b-1)*(c-1)*(d-1)%MOD
        ans+=a*b*c*d%MOD
        ans%=MOD
    print(ans)
resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)

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

    def add(self, i, x):
        while i <= self.n:
            self.data[i] += x
            i += i & -i
mod = 998244353
N = int(input())
V = []
for _ in range(N):
  x, y = map(int, input().split())
  V.append([x, y])
V.sort(key = lambda x:x[1])
Bl = BIT(N)
Br = BIT(N)
for i in range(N):
    V[i][1] = i+1
    Br.add(i+1, 1)
V.sort()
ans = 0
for i in range(N):
    x, y = V[i]
    ld = Bl.sum(y-1)
    lu = i-ld
    rd = Br.sum(y-1)
    ru = (N-i)-rd-1
    Br.add(y, -1)
    Bl.add(y, 1)
    A = pow(2, N-1, mod)
    B = pow(2, lu+rd, mod)*(pow(2, ru, mod)-1)*(pow(2, ld, mod)-1)%mod
    C = pow(2, ru+ld, mod)*(pow(2, lu, mod)-1)*(pow(2, rd, mod)-1)%mod
    BC = (pow(2, ru, mod)-1)*(pow(2, ld, mod)-1)*(pow(2, lu, mod)-1)*(pow(2, rd, mod)-1)%mod
    ans += A+B+C-BC
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)

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

    def add(self, i, x):
        while i <= self.n:
            self.data[i] += x
            i += i & -i
mod = 998244353
N = int(input())
V = []
for _ in range(N):
    x, y = map(int, input().split())
    V.append([x, y])
V.sort(key = lambda x:x[1])
Bl = BIT(N)
Br = BIT(N)
for i in range(N):
    V[i][1] = i+1
    Br.add(i+1, 1)
V.sort()
ans = 0
for i in range(N):
    x, y = V[i]
    ld = Bl.sum(y-1)
    lu = i-ld
    rd = Br.sum(y-1)
    ru = (N-i)-rd-1
    Br.add(y, -1)
    Bl.add(y, 1)
    A = pow(2, N-1, mod)
    B = pow(2, lu+rd, mod)*(pow(2, ru, mod)-1)*(pow(2, ld, mod)-1)%mod
    C = pow(2, ru+ld, mod)*(pow(2, lu, mod)-1)*(pow(2, rd, mod)-1)%mod
    BC = (pow(2, ru, mod)-1)*(pow(2, ld, mod)-1)*(pow(2, lu, mod)-1)*(pow(2, rd, mod)-1)%mod
    ans += A+B+C-BC
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)

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

    def add(self, i, x):
        while i <= self.n:
            self.data[i] += x
            i += i & -i
mod = 998244353
N = int(input())
V = []
Y = []
for _ in range(N):
    x, y = map(int, input().split())
    V.append([x, y])
    Y.append(y)
Y.sort()
V.sort()
Bl = BIT(N)
Br = BIT(N)
for i in range(N):
    y = V[i][1]
    y = bisect_right(Y, y)
    V[i][1] = y
    Br.add(y, 1)
ans = 0
for i in range(N):
    x, y = V[i]
    ld = Bl.sum(y-1)
    lu = i-ld
    rd = Br.sum(y-1)
    ru = (N-i)-rd-1
    Br.add(y, -1)
    Bl.add(y, 1)
    A = pow(2, N-1, mod)
    B = pow(2, lu+rd, mod)*(pow(2, ru, mod)-1)*(pow(2, ld, mod)-1)%mod
    C = pow(2, ru+ld, mod)*(pow(2, lu, mod)-1)*(pow(2, rd, mod)-1)%mod
    BC = (pow(2, ru, mod)-1)*(pow(2, ld, mod)-1)*(pow(2, lu, mod)-1)*(pow(2, rd, mod)-1)%mod
    ans += A+B+C-BC
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)

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

    def add(self, i, x):
        while i <= self.n:
            self.data[i] += x
            i += i & -i
mod = 998244353
N = int(input())
V = []
Y = []
for _ in range(N):
    x, y = map(int, input().split())
    V.append([x, y])
    Y.append(y)
Y.sort()
V.sort()
Bl = BIT(N)
Br = BIT(N)
for i in range(N):
    y = V[i][1]
    y = bisect_right(Y, y)
    V[i][1] = y
    Br.add(y, 1)
ans = 0
for i in range(N):
    x, y = V[i]
    ld = Bl.sum(y-1)
    lu = i-ld
    rd = Br.sum(y-1)
    ru = (N-i)-rd-1
    Br.add(y, -1)
    Bl.add(y, 1)
    A = pow(2, N-1, mod)
    B = pow(2, lu+rd, mod)*(pow(2, ru, mod)-1)*(pow(2, ld, mod)-1)
    C = pow(2, ru+ld, mod)*(pow(2, lu, mod)-1)*(pow(2, rd, mod)-1)
    BC = (pow(2, ru, mod)-1)*(pow(2, ld, mod)-1)*(pow(2, lu, mod)-1)*(pow(2, rd, mod)-1)
    ans += A+B+C-BC
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
mod = 998244353

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

def power(x, y):
    return pow(x, y, 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 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))

n, = list(map(int, input().split()))
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))