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 in Python (3.4.3) (judge verdict: Time Limit Exceeded)

from itertools import product
from math import ceil, log2


class SegmentTree:
    def __init__(self, length, init_value=None, aggfunc=min):
        self.length = length
        self.init_value = init_value
        self.aggfunc = aggfunc
        self.depth = ceil(log2(length))
        self.data = [init_value] * (2 ** (1 + self.depth) - 1)
        self.origin = 2 ** self.depth - 1

    def update(self, idx, value):
        i = idx + self.origin
        self.data[i] = value
        while i > 0:
            i = (i - 1) // 2
            new_value = self.aggfunc((self.data[i * 2 + 1], self.data[i * 2 + 2]))
            if self.data[i] != new_value:
                self.data[i] = new_value
            else:
                break

    def query(self, start, stop):
        return self._query(start, stop, 0, 0, 2 ** self.depth)

    def _query(self, start, stop, k, l, r):
        if (start <= l) & (r <= stop):
            return self.data[k]
        elif (r <= start) | (stop <= l):
            return self.init_value
        else:
            return self.aggfunc(
                (
                    self._query(start, stop, k * 2 + 1, l, (l + r) // 2),
                    self._query(start, stop, k * 2 + 2, (l + r) // 2, r),
                )
            )


n = int(input())
xy = [tuple(map(int, input().split())) for _ in range(n)]
mod = 998244353

xy.sort()

xs = [x for x, y in xy]
ys = [y for x, y in xy]

ys.sort()
x_dict = dict(zip(xs, range(n)))
y_dict = dict(zip(ys, range(n)))

points = [(x_dict[x], y_dict[y]) for x, y in xy]

segment_tree = SegmentTree(n, 0, sum)

ans = 0
for x, y in points:

    n0 = segment_tree.query(0, y)
    n1 = x - n0
    n2 = y - n0
    n3 = n - n0 - n1 - n2 - 1

    segment_tree.update(y, 1)

    counts = [n0, n1, n2, n3]
    for exist in product([False, True], repeat=4):
        tmp = 1
        for e, c in zip(exist, counts):
            if e:
                tmp *= (2 ** c) % mod - 1
        if (exist[1] & exist[2]) | (exist[0] & exist[3]):
            tmp *= 2
        ans += tmp
ans %= mod

print(ans)