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
101
102
103
104
105
106
107
108
109
110
#include<bits/stdc++.h>
using namespace std;

const int base = 1048576;
// const int base = 16;
const long long mod = 1e9+7;

int tree[base * 2];

vector<int> graph[base];
int visited[base];
int upper_size[base];
int v_pop[base];

void add(int a, int b, int value, int v = 1, int l = 0, int r = base - 1){
    if(a <= l and b >= r){
        tree[v] = value;
        return;
    }
    if(a > r or b < l){
        return;
    }
    int mid = (l+r)/2;
    add(a, b, value, v * 2, l, mid);
    add(a, b, value, v * 2 + 1, mid + 1, r);
}

int find(int v){
    if(v == 0){
        return 0;
    }
    return max(tree[v], find(v/2));
}

void DFS(int v, int lvl, int pop = 1){
    if(v == lvl){
        pop = 1 + v_pop[v];
        if(graph[v].size() == 1){
            int u = graph[v][0];
            v_pop[u] += pop;
            upper_size[v] += pop;
            return;
        }
    }
    
    if(visited[v] == lvl){
        return;
    }
    visited[v] = lvl;
    upper_size[v] += pop;
    for(auto u: graph[v]){
        DFS(u, lvl, pop);
    }
}

long long fast_pot(long long a, long long stopien){
    if(stopien == 0){
        return 1;
    }
    if(stopien%2 == 0){
        return fast_pot((a*a)%mod, stopien/2);
    }else{
        return (fast_pot((a*a)%mod, stopien/2)*a)%mod;
    }
}


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        int l, r;
        cin >> l >> r;
        int son1, son2;
        son1 = find(l + base);
        son2 = find(r + base);
        if(son1 < son2){
            swap(son1, son2);
        }
        if(son1 != 0){
            graph[i].push_back(son1);
        }
        if(son2 != 0 and son2 != son1){
            bool happened = false;
            for(auto u: graph[son1]){
                if(u == son2){
                    happened = true;
                }
            }
            if(!happened){
                graph[i].push_back(son2);
            }
        }
        add(l, r, i);
    }
    for(int i = n; i > 0; i--){
        DFS(i, i);
    }
    long long ans = 0;
    for(int i = 1; i <= n; i++){
        ans = (ans + fast_pot(upper_size[i], mod - 2))%mod;
        //cout << upper_size[i] << '\n';
    }
    cout << ans << '\n';


    return 0;
}