1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#include<vector>
#include<iostream>
#include<set>
#include<stack>
#include<math.h>

using namespace std;

vector<int> topo[500100];
int ile_topo[500100];

set< pair<int, int> > zbior;

int visited[500100];

void dfs(int ktory, int v) {
    visited[v] = ktory;
    ile_topo[v]++;
    
    for(int w : topo[v]) {
        if(visited[w] == ktory) continue;
        dfs(ktory, w);
    }
}

long long modulo = pow(10, 9) + 7;

long long odwrotnosc(long long x) {
    long long res = 1;
    int y = modulo-2;
 
    // Check till the number becomes zero
    while (y > 0) {

        if (y % 2 == 1)
            res = (res * x)%modulo;
 
        y = y >> 1;
        x = (x * x)%modulo;
    }
    
    return res;
}

long long oblicz(int n) {
    long long podziel = 0;
    
    //cout<<"Odwrotnosc odwrotnosci 2 to "<<(odwrotnosc(2)+odwrotnosc(2))%modulo<<endl;
    
    for(int i=0;i<n;i++) {
        podziel = (podziel+odwrotnosc(ile_topo[i]))%modulo;
    }
    
    return podziel;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin>>n;
    
    stack< pair<int, int> > stos;
    
    int l,r;
    for(int i=0;i<n;i++) {
        cin>>l>>r;
        stos.push(make_pair(l, r));
    }
    
    int index = -1;
    
    while(!stos.empty()) {
        index++;
        pair<int, int> w = stos.top();
        l = w.first;
        r = w.second;
        stos.pop();
        
        auto p = zbior.upper_bound(make_pair(l, 0));
        auto q = zbior.upper_bound(make_pair(r, 0));
        
        while(p != q) {
            topo[(*p).second].push_back(index);
            
            p = zbior.erase(p);
        }
        
        zbior.insert(make_pair(l, index));
        zbior.insert(make_pair(r, index));
    }
    
    for(int i=0;i<n;i++) dfs(i+1, i);
    
    cout<<oblicz(n)<<endl;

    return 0;
}