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 Rust (1.15.1) (judge verdict: Accepted)

static MOD: isize = 998244353;

#[derive(Debug)]
struct FenwickTree {
    n: usize,
    bit: Vec<isize>,
}

impl FenwickTree {
    fn new(n: usize) -> Self {
        FenwickTree {
            n: n,
            bit: vec![0; n],
        }
    }

    // [0, i)
    fn sum(&self, i: usize) -> isize {
        let mut s = 0;
        let mut i = i as isize;
        while i > 0 {
            i -= 1;
            s += self.bit[i as usize];
            i &= i + 1;
        }
        s
    }

    fn add(&mut self, i: usize, x: isize) {
        let mut i = i;
        while i < self.n {
            self.bit[i] += x;
            i |= i + 1;
        }
    }
}

fn solve(xys: Vec<(isize, isize)>) -> isize {
    let n = xys.len() as isize;
    let mut p2 = vec![0 as isize; 200002];
    p2[0] = 1;
    for x in 1..p2.len() {
        p2[x] = (p2[x - 1] * 2) % MOD;
    }

    let mut sortedx = xys.iter().map(|&(x, _)| x).collect::<Vec<_>>();
    sortedx.sort();

    let mut sortedy = xys.iter().map(|&(_, y)| y).collect::<Vec<_>>();
    sortedy.sort();

    let mut compressed = vec![(0, 0); xys.len()];
    for (i, &(x, y)) in xys.iter().enumerate() {
        let ix = sortedx.binary_search(&x).unwrap() as isize;
        let iy = sortedy.binary_search(&y).unwrap() as isize;
        compressed[i] = (ix, iy);
    }

    compressed.sort_by_key(|&(x, _)| x);

    let mut lft = FenwickTree::new(xys.len());
    let mut rft = FenwickTree::new(xys.len());
    for i in 0..xys.len() {
        rft.add(i, 1);
    }

    let mut ans = 0;

    for (x, y) in compressed {
        let r3 = lft.sum(y as usize) as usize;
        let r2 = (x - r3 as isize) as usize;
        let r4 = rft.sum(y as usize) as usize;
        let r1 = (n - x - 1 - r4 as isize) as usize;

        let pr1 = p2[r1] - 1;
        let pr2 = p2[r2] - 1;
        let pr3 = p2[r3] - 1;
        let pr4 = p2[r4] - 1;

        // 各象限と自身のみ
        ans += pr1 + pr2 + pr3 + pr4;
        ans %= MOD;

        // 隣り合う象限と自身
        ans += if r1 > 0 && r2 > 0 {
            (pr1 * pr2) % MOD
        } else {
            0
        } + if r2 > 0 && r3 > 0 {
            (pr2 * pr3) % MOD
        } else {
            0
        } + if r3 > 0 && r4 > 0 {
            (pr3 * pr4) % MOD
        } else {
            0
        } + if r4 > 0 && r1 > 0 {
            (pr4 * pr1) % MOD
        } else {
            0
        };
        ans %= MOD;

        // r1+r3, r2+r4 (自身を含む場合と含まない場合で２倍)
        ans += if r1 > 0 && r3 > 0 {
            (pr1 * pr3) * 2 % MOD
        } else {
            0
        } + if r2 > 0 && r4 > 0 {
            (pr2 * pr4) * 2 % MOD
        } else {
            0
        };
        ans %= MOD;

        // 3象限を含む場合
        ans += if r1 > 0 && r2 > 0 && r3 > 0 {
            (((pr1 * pr2) % MOD) * pr3 * 2) % MOD
        } else {
            0
        } + if r2 > 0 && r3 > 0 && r4 > 0 {
            (((pr2 * pr3) % MOD) * pr4 * 2) % MOD
        } else {
            0
        } + if r3 > 0 && r4 > 0 && r1 > 0 {
            (((pr3 * pr4) % MOD) * pr1 * 2) % MOD
        } else {
            0
        } + if r4 > 0 && r1 > 0 && r2 > 0 {
            (((pr4 * pr1) % MOD) * pr2 * 2) % MOD
        } else {
            0
        };
        ans %= MOD;

        // 全て含む場合(自身を含む場合と含まない場合で２倍)
        ans += if r1 > 0 && r2 > 0 && r3 > 0 && r4 > 0 {
            (((((pr1 * pr2) % MOD) * pr3) % MOD) * pr4 * 2) % MOD
        } else {
            0
        };
        ans %= MOD;

        lft.add(y as usize, 1);
        rft.add(y as usize, -1);
    }

    ans += n;
    ans %= MOD;

    ans
}

#[test]
fn solve_test() {
    assert_eq!(solve(vec![(-1, 3), (2, 1), (3, -2)]), 13);
    assert_eq!(solve(vec![(1, 4), (2, 1), (3, 3), (4, 2)]), 34);
    assert_eq!(
        solve(vec![
            (19, -11),
            (-3, -12),
            (5, 3),
            (3, -15),
            (8, -14),
            (-9, -20),
            (10, -9),
            (0, 2),
            (-7, 17),
            (6, -6)
        ]),
        7222
    );
}

#[allow(unused_macros)]
macro_rules! parse_line {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            iter.next().unwrap().parse::<$t>().unwrap()
        }
    );

    ( $( $t:ty), +) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ( $(iter.next().unwrap().parse::<$t>().unwrap()),* )
        }
    );
}

#[allow(unused_macros)]
macro_rules! read_line {
    () => {{
        let mut line = String::new();
        ::std::io::stdin().read_line(&mut line).unwrap();
        line.pop();
        line
    }};
}

#[allow(unused_macros)]
macro_rules! parse_vec {
    ( $t:ty ) => {{
        let mut line = String::new();
        ::std::io::stdin().read_line(&mut line).unwrap();
        let iter = line.split_whitespace();
        iter.map(|v| v.parse::<$t>().unwrap()).collect::<Vec<_>>()
    }};
}

fn main() {
    let n = parse_line!(usize);
    let xys = (0..n)
        .map(|_| parse_line!(isize, isize))
        .collect::<Vec<_>>();
    println!("{}", solve(xys));
}