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
#include<bits/stdc++.h>
using namespace std;

const long long mod = 1e9 + 7;

long long fast_pow(long long a, long long b) {
    if(b == 0) return 1;
    long long res = 1;
    a %= mod;

    while(b > 0) {
        if(b & 1) res = (res * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return res;
}

vector<vector<int>> G;
vector<int> anc;
vector<int> vis;

void dfs(int v, int num) {
    vis[v] = num;
    anc[v]++;
    for(auto x : G[v]) {
        if(vis[x] != num) {
            dfs(x, num);
        }
    }
}

void solve() {
    int n; cin >> n;
    vector<pair<int, int>> pol(n);
    for(int i = 0; i < n; i++) cin >> pol[i].first >> pol[i].second;
    reverse(pol.begin(), pol.end());
    set<pair<int, int>> s;
    G.assign(n + 1, vector<int>());
    vector<int> st(n + 1, 0);
    for(int i = 0; i < n; i++) {
        while(s.lower_bound({pol[i].first, 0}) != s.end() && (*s.lower_bound({pol[i].first, 0})).first < pol[i].second) {
            G[(*s.lower_bound({pol[i].first, 0})).second].push_back(i + 1);
            s.erase(s.lower_bound({pol[i].first, 0}));
            st[i + 1]++;
        }
        s.emplace(pol[i].first, i + 1);
        s.emplace(pol[i].second, i + 1);
    }
    anc.assign(n + 1, 0);
    
    //toposort
    queue<int> q;
    vector<int> topo;
    for(int i = 1; i <= n; i++) {
        if(st[i] == 0) q.push(i);
    }
    while(!q.empty()) {
        int v = q.front(); q.pop();
        topo.push_back(v);
        for(auto x : G[v]) {
            st[x]--;
            if(st[x] == 0) q.push(x);
        }
    }
    //for(auto x : topo) cout << x << " ";
    //cout << "\n";

    vis.assign(n + 1, 0);
    for(int i = 1; i <= n; i++) {
        dfs(i, i);
    }

    //for(int i = 1; i <= n; i++) anc[i]--;
    //for(int i = 1; i <= n; i++) cout << anc[i] << " ";
    //cout << "\n";


    //calculating inverses
    vector<long long> inv(n + 1, 0);
    for(int i = 0; i <= n; i++) inv[i] = fast_pow(i, mod - 2);
    long long ans = 0LL;
    for(auto x : topo) {
        ans = (ans + inv[anc[x]]) % mod;
    }
    cout << ans << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    solve();
    return 0;
}