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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//#ifdef DEBUG
//#define _GLIBCXX_DEBUG
//#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
typedef long double ld;
const int maxN = 1'000'005;
int inv[maxN];
const int mod = (int)1e9 + 7;
int sum(int a, int b) {
    int s = a + b;
    if (s >= mod) s -= mod;
    return s;
}
int mult(int a, int b) {
    return (1LL * a * b) % mod;
}
int sub(int a, int b) {
    int s = a - b;
    if (s < 0) s += mod;
    return s;
}
int n;
int l[maxN], r[maxN];
vector<int> g_l[maxN], g_r[maxN];
int max_h[maxN];
int tin_l[maxN];
int tout_l[maxN];
int tin_r[maxN];
int tout_r[maxN];
int TIMER = 0;
int who[maxN];
void dfs_l(int v) {
    tin_l[v] = TIMER++;
    for (int to : g_l[v]) {
        dfs_l(to);
    }
    tout_l[v] = TIMER++;
}
void dfs_r(int v) {
    tin_r[v] = TIMER++;
    for (int to : g_r[v]) {
        dfs_r(to);
    }
    tout_r[v] = TIMER++;
}
int freq[maxN];
int fenw[maxN];
void upd(int v, int by) {
    while (v <= n) {
        fenw[v] += 1;
        v = (v | (v - 1)) + 1;
    }
}
int get(int r) {
    int ans = 0;
    while (r > 0) {
        ans += fenw[r];
        r &= (r - 1);
    }
    return ans;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    cin >> n;
    inv[1] = 1;
    for (int i = 2; i <= n; i++) {
        inv[i] = mult(inv[mod % i], sub(0, mod / i));
    }
    for (int i = 1; i <= n; i++) {
        cin >> l[i] >> r[i];
        who[l[i]] = i;
        who[r[i]] = i;
    }
    l[0] = 0;
    r[0] = 2 * n + 1;
    set<int> s;
    for (int i = n; i >= 0; i--) {
        while (true) {
            auto it = s.lower_bound(l[i]);
            if (it != s.end() && *it <= r[i]) {
                if (l[who[*it]] == *it) {
                    g_l[i].emplace_back(who[*it]);
                }
                else {
                    g_r[i].emplace_back(who[*it]);
                }
                s.erase(it);
            }
            else {
                break;
            }
        }
        s.insert(l[i]);
        s.insert(r[i]);
    }
    for (int i = 0; i <= n; i++) {
        sort(g_l[i].begin(), g_l[i].end(), [&](int a, int b) {
            return l[a] < l[b];
        });
    }
    for (int i = 0; i <= n; i++) {
        sort(g_r[i].begin(), g_r[i].end(), [&](int a, int b) {
            return r[a] > r[b];
        });
    }
    for (int i = n; i >= 0; i--) {
        max_h[i] = i;
        for (int j : g_l[i]) {
            max_h[i] = max(max_h[i], max_h[j]);
        }
        for (int j : g_r[i]) {
            max_h[i] = max(max_h[i], max_h[j]);
        }
    }
    TIMER = 0;
    dfs_l(0);
    TIMER = 0;
    dfs_r(0);
    int ans = 0;
    vector<pair<int,int>> TL;
    for (int i = 1; i <= n; i++) {
        TL.emplace_back(tin_l[i], ~i);
    }
    for (int i = 1; i <= n; i++) {
        TL.emplace_back(tout_l[i], i);
    }
    sort(TL.rbegin(), TL.rend());
    for (auto& it : TL) {
        if (it.second > 0) {
            freq[it.second] -= get(max_h[it.second]) - get(it.second);
        }
        else {
            upd(~it.second, +1);
        }
    }
    memset(fenw, 0, sizeof fenw);
    vector<pair<int,int>> TR;
    for (int i = 1; i <= n; i++) {
        TR.emplace_back(tin_r[i], ~i);
    }
    for (int i = 1; i <= n; i++) {
        TR.emplace_back(tout_r[i], i);
    }
    sort(TR.rbegin(), TR.rend());
    for (auto& it : TR) {
        if (it.second > 0) {
            freq[it.second] -= get(max_h[it.second]) - get(it.second);
        }
        else {
            upd(~it.second, +1);
        }
    }
    for (int i = 1; i <= n; i++) {
        freq[i] += max_h[i] - i + 1;
        ans = sum(ans, inv[freq[i]]);
    }
    cout << ans;

    return 0;
}