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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,k,n) for(int i=(k);i<(n);++i)
typedef long long ll;
const ll INF = 1LL<<60;
const ll MOD = 998244353;

const int MAX_N = 200010;

inline ll add(ll x, ll y) {return (x+y)%MOD;}
inline ll mul(ll x, ll y) {return (x*y)%MOD;}
inline ll sub(ll x, ll y) {
    ll res = x - y;
    if(res < 0) res += MOD;
    return res;
}

ll pow2[MAX_N];
void pow_init() {
    pow2[0] = 1;
    for(int i=1; i<MAX_N; ++i) {
        pow2[i] = mul(2LL, pow2[i-1]);
    }
}

// sum(0), add(0,x) してはいけない
template<typename T>
class BIT{
public:
    vector<T> bit;
    int size;
    BIT(int size):
        bit(vector<T>(size+1,0)),size(size){}
    
    T sum(int i){
        if(!i) return 0;
        return bit[i]+sum(i-(i&(-i)));
    }

    void add(int i,T x){
        if(i>size)return;
        bit[i]+=x;
        add(i+(i&(-i)),x); 
    }   
};

int main(){
    int N; cin >> N;
    vector<int> xs, ys;
    vector<pair<int,int>> ps;
    REP(i,N) {
        int x, y; cin >> x >> y;
        xs.push_back(x);
        ys.push_back(y);
        ps.push_back(make_pair(x,y));
    }
    // 座標圧縮
    sort(all(xs));
    sort(all(ys));
    sort(all(ps));
    xs.erase(unique(all(xs)),xs.end());
    ys.erase(unique(all(ys)),ys.end());
    REP(i,N) {
        ps[i].first = lower_bound(all(xs), ps[i].first) - xs.begin() + 2;
        ps[i].second = lower_bound(all(ys), ps[i].second) - ys.begin() + 2;
    }
    
    pow_init();
    ll ans = 0;
    BIT<int> y_cnt(MAX_N);
    multiset<int> tmp;
    REP(i,N) {
        int x = ps[i].first;
        int y = ps[i].second;
        ans = add(ans, pow2[y_cnt.sum(MAX_N)]-1);
        ans = sub(ans, pow2[y_cnt.sum(y-1)]-1);
        ans = sub(ans, pow2[y_cnt.sum(MAX_N)-y_cnt.sum(y)]-1);
        tmp.insert(y);
        if(i!=N-1 && ps[i+1].first!=ps[i].first) {
            for(auto e:tmp) {
                y_cnt.add(e,1);
            }
            tmp.clear();
        }
    }

    tmp.clear();
    REP(i,MAX_N+1) y_cnt.bit[i] = 0;
    for(int i=N-1; i>=0; --i) {
        int x = ps[i].first;
        int y = ps[i].second;
        ans = add(ans, pow2[y_cnt.sum(MAX_N)]-1);
        ans = sub(ans, pow2[y_cnt.sum(y-1)]-1);
        ans = sub(ans, pow2[y_cnt.sum(MAX_N)-y_cnt.sum(y)]-1);
        tmp.insert(y);
        if(i!=0 && ps[i-1].first!=ps[i].first) {
            for(auto e:tmp) {
                y_cnt.add(e,1);
            }
            tmp.clear();
        }
    }

    sort(all(ps),[](pair<int,int> p1, pair<int,int> p2){return p1.second<p2.second;});
    int cnt = 0;
    int tmp1 = 0;
    REP(i,N) {
        ans = add(ans, pow2[cnt]-1);
        tmp1++;
        if(i!=N-1 && ps[i].second!=ps[i+1].second) {
            cnt += tmp1;
            tmp1 = 0;
        }
    }

    cnt = 0;
    tmp1 = 0;
    for(int i=N-1; i>=0; --i) {
        ans = add(ans, pow2[cnt]-1);
        tmp1 ++;
        if(i!=0 && ps[i].second!=ps[i-1].second) {
            cnt += tmp1;
            tmp1 = 0;
        }
    }
    ans = sub(mul((ll)N,pow2[N]-1), ans);
    cout << ans << endl;
    return 0;
}