This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
#define fi first
#define se second
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MAXN = 1e6 + 7 ;
const int MOD = 1e9 + 7 ;
int n ;
pair < int , int > a[ MAXN ] ;
int prv[ MAXN ] , rnk[ MAXN ] ;
int get ( int x ) {
    if ( prv[ x ] == -1 ) { return x ; }
    int y = get ( prv[ x ] ) ;
    prv[ x ] = y ;
    return y ;
}
void unite ( int x , int y ) {
    int k1 = get ( x ) , k2 = get ( y ) ;
    if ( k1 != k2 ) {
        if ( rnk[ k1 ] >= rnk[ k2 ] ) {
            rnk[ k1 ] += ( rnk[ k1 ] == rnk[ k2 ] ) ;
            prv[ k2 ] = k1 ;
        }
        else {
            prv[ k1 ] = k2 ;
        }
    }
}
int mx_prv[ MAXN ] , mn_nxt[ MAXN ] ;
int srt[ MAXN ] ;
void solve ( ) {
    cin >> n ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> a[ i ].fi >> a[ i ].se ;
        prv[ i ] = -1 , rnk[ i ] = 0 ;
        mx_prv[ i ] = -1 ;
        mn_nxt[ i ] = 4 * MAXN ;
    }
    sort ( a + 1 , a + n + 1 ) ;
    set < pair < int , int > > s ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        auto it = s.lower_bound ( { a[ i ].fi , 0 } ) ;
        if ( it != s.end ( ) && it->fi < a[ i ].se ) {
            unite ( i , it->se ) ;
        }
        it = s.lower_bound ( { a[ i ].se , 0 } ) ;
        if ( it != s.begin ( ) ) {
            -- it ;
            if ( a[ i ].fi < it->fi ) {
                mx_prv[ i ] = max ( mx_prv[ i ] , it->fi ) ;
            }
        }
        s.insert ( { a[ i ].se , i } ) ;
    }
    s.clear ( ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        srt[ i ] = i ;
    }
    auto cmp = [ & ] ( int x , int y ) {
        return ( a[ x ].se < a[ y ].se ) ;
    };
    sort ( srt + 1 , srt + n + 1 , cmp ) ;
    for ( int wh = n ; wh >= 1 ; -- wh ) {
        int i = srt[ wh ] ;
        auto it = s.lower_bound ( { a[ i ].fi , 0 } ) ;
        if ( it != s.end ( ) && it->fi < a[ i ].se ) {
            mn_nxt[ i ] = min ( mn_nxt[ i ] , it->fi ) ;
        }
        s.insert ( { a[ i ].fi , i } ) ;
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( mn_nxt[ i ] < mx_prv[ i ] ) {
            cout << "0\n" ;
            return ;
        }
    }
    ll ans = 1 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( get ( i ) == i ) {
            ans = ( ans * 2 ) % MOD ;
        }
    }
    cout << ans << "\n" ;
}
int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ; // cin >> t ; 
    while ( t -- ) { solve ( ) ; }
    return 0 ;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |