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
111
112
113
114
115
116
117
118
119
#include <bits/stdc++.h>
using namespace std;

constexpr int base = 1<<19;
vector <int> tree(2*base);

int query(int a){
    a += base;
    int res = tree[a];
    a /= 2;

    while(a){
        res = max(res, tree[a]);
        a /= 2;
    }

    return res;
}

void update(int a, int b, int x){
    a += base-1;
    b += base+1;

    while(a/2 != b/2){
        if(a%2 == 0) tree[a+1] = x;
        if(b%2 == 1) tree[b-1] = x;

        a /= 2; b /= 2;
    }
}

long long nwd(long long a, long long b){
    if(b == 0) return a;
    return nwd(b, a%b);
}

long long mod(long long a){
    long long m = 1'000'000'007;
    long long m0 = m;
    long long y = 0, x = 1;

    while(a > 1){
        long long q = a/m;
        long long t = m;
 
        m = a%m;
        a = t;
        t = y;
 
        y = x - q * y;
        x = t;
    }
 
    if(x < 0) x += m0;
    return x;
}

int main(){
    int n;
    cin >> n;

    vector <vector<int>> graph(n+1);

    for(int i = 1; i<=n; ++i){
        int l, r;
        cin >> l >> r;

        int ll = query(l);
        int rr = query(r);

        if(ll != 0){
            graph[ll].push_back(i);
        }

        if(rr != 0){
            graph[rr].push_back(i);
        }

        if(l+1 != r){
            update(l+1, r-1, i);
        }
    }

    vector <long long> in(n+1);

    for(int i = 1; i<=n; ++i){
        vector <bool> vis(n+1);
        vis[i] = true;

        queue <int> q;
        q.push(i);

        while(!q.empty()){
            int act = q.front();
            in[i]++;
            q.pop();

            for(auto ne: graph[act]){
                if(!vis[ne]){
                    vis[ne] = true;
                    q.push(ne);
                }
            }
        }
    }

    long long p = 0, q = 1;

    for(int i = 1; i<=n; ++i){
        long long gora = q + p*in[i];
        long long dol = q*in[i];
        long long gcd = nwd(gora, dol); 
        
        p = gora/gcd;
        q = dol/gcd;
    }
    
    cout << (p*mod(q)) % 1'000'000'007 << "\n";
}