#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <stack>
#include <bit>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;
ll mod=1e9+7;
ll binPow(ll a, ll n){
    ll res = 1, b=a;
    while(n){
        if(n&1){
            res *= b;
            res %= mod;
            --n;
        }
        b *= b;
        b %= mod;
        n /= 2;
    }
    return res;
}
ll get(ll x, vector<ll>& parent){
    if(parent[x] == x) return x;
    return parent[x] = get(parent[x], parent);
}
void unite(ll x, ll y, ll val, vector<ll>& parent, vector<set<ll>>& another){
    x = get(x, parent);
    y = get(y, parent);
    if(x == y) return;
    parent[y] = x;
    auto it = another[y].lower_bound(val);
    --it;
    if(it != another[y].begin()){
        cout<<0<<endl;
        exit(0);
    }
    if(another[x].size() < another[y].size()) swap(another[x], another[y]);
    for(auto el: another[y]){
        another[x].insert(el);
    }
}
void solve(){
    ll n, i, l, r, cnt=0, id, x, id2;
    cin>>n;
    vector<pair<ll, ll>> add(2*n);
    set<ll> s;
    vector<ll> parent(n);
    vector<set<ll>> another(n);
    for(i=0;i<n;++i){
        cin>>l>>r;
        --l;
        --r;
        add[l] = {r, i};
        add[r] = {l, i};
        parent[i] = i;
    }
    vector<ll> del;
    for(i=0;i<2*n;++i){
        x = add[i].first;
        id = add[i].second;
        if(i < x){
            s.insert(x);
            another[id].insert(x);
            auto it = s.find(x);
            while(it != s.begin()){
                del.pb(x);
                --it;
                x = *it;
                id2 = add[x].second;
                unite(id, id2, add[i].first, parent, another);
            }
            for(auto el: del){
                s.erase(el);
            }
            del.clear();
        }else{
            s.erase(i);
            id = get(id, parent);
            another[id].erase(i);
            if(another[id].size() > 0){
                s.insert(*another[id].begin());
            }
        }
    }
    for(i=0;i<n;++i){
        cnt += parent[i] == i;
    }
    cout<<binPow(2, cnt)<<endl;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    srand(time(nullptr));
    ll t=1;
//    cin>>t;
    for(;t>0;--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... |