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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define SZ(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
auto &operator<<(auto &o, pair<auto, auto> p) {
    return o << "(" << p.st << ", " << p.nd << ")";
}
auto operator<<(auto &o, auto x)->decltype(end(x), o) {
    o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e;
    return o << "}";
}
#define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x)

struct zwykleDrzewo{
    int roz;
    vector<int> tree;

    void init(int n){
        tree.clear();
        roz = 1;
        while(roz <= n) roz *= 2;
        roz *= 2;
        tree.resize(roz, 0);
    }

    void add(int pos, int delta){
        pos += roz / 2;
        while(pos >= 1){
            tree[pos] += delta;
            pos /= 2;
        }
    }

    int sum(int lo, int hi){
        if(lo > hi) return 0;
        lo += roz / 2; hi += roz / 2;
        int ans = tree[lo];
        if(hi != lo) ans += tree[hi];
        while(lo / 2 != hi / 2){
            if(!(lo & 1)) ans += tree[lo ^ 1];
            if(hi & 1) ans += tree[hi ^ 1];
            lo /= 2; hi /= 2;
        }
        return ans;
    }

    int lastLit(int lo, int hi, int u, int a, int b){
        if(lo == a && b == hi){
            if(lo + 1 == hi){
                if(tree[u]) return u - roz / 2;
                return -1;
            }
            if(tree[u] == 0) return -1;
            int mid = (lo + hi) / 2;
            if(tree[u * 2 + 1]) return lastLit(mid, hi, u * 2 + 1, max(a, mid), b);
            return lastLit(lo, mid, u * 2, a, min(b, mid));
        }
        if(b <= a) return -1;
        int mid = (lo + hi) / 2;
        auto R = lastLit(mid, hi, u * 2 + 1, max(a, mid), b);
        if(R != -1) return R;
        return lastLit(lo, mid, u * 2, a, min(b, mid));
    }

    int ostatni(int lo, int hi){
        return lastLit(0, roz / 2, 1, lo, hi + 1);
    }
};

struct secik{
    int roz;
    vector<int> tree;

    void init(int n){
        tree.clear();
        roz = 1;
        while(roz <= n) roz *= 2;
        roz *= 2;
        tree.resize(roz, 0);
    }

    void add(int pos, int delta){
        pos += roz / 2;
        while(pos >= 1){
            tree[pos] += delta;
            pos /= 2;
        }
    }

    int firstLit(int lo, int hi, int u, int a, int b){
        if(lo == a && b == hi){
            if(lo + 1 == hi){
                if(tree[u]) return u - roz / 2;
                return -1;
            }
            if(tree[u] == 0) return -1;
            int mid = (lo + hi) / 2;
            if(tree[u * 2]) return firstLit(lo, mid, u * 2, a, min(b, mid));
            return firstLit(mid, hi, u * 2 + 1, max(a, mid), b);
        }
        if(b <= a) return -1;
        int mid = (lo + hi) / 2;
        auto L = firstLit(lo, mid, u * 2, a, min(b, mid));
        if(L != -1) return L;
        return firstLit(mid, hi, u * 2 + 1, max(a, mid), b);
    }

    int lowerbound(int from){
        return firstLit(0, roz / 2, 1, from, roz / 2);
    }

};

const int nax = 1e6 + 5;
const int mod = 1e9 + 7;
int l[nax];
int r[nax];
int n;

int leftEdge[nax];
int rightEdge[nax];

int naLewo[nax];
int naPrawo[nax];

ll pp(ll a, ll b){
    ll ans = 1;
    while(b){
        if(b & 1){
            ans *= a;
            ans %= mod;
        }
        a *= a;
        a %= mod;
        b /= 2;
    }
    return ans;
}

ll inv(ll a){
    return pp(a, mod - 2);
}

struct segtree{
    vector<int> tree;
    int roz;

    void init(int n){
        tree.clear();
        roz = 1;
        while(roz <= n) roz *= 2;
        roz *= 2;
        tree.resize(roz, 0);
    }

    void add(int pos, int delta){
        pos += roz / 2;
        while(pos >= 1){
            tree[pos] += delta;
            pos /= 2;
        }
    }

    int sum(int lo, int hi){
        if(lo > hi) return 0;
        lo += roz / 2; hi += roz / 2;
        int ans = tree[lo];
        if(hi != lo) ans += tree[hi];
        while(lo / 2 != hi / 2){
            if(!(lo & 1)) ans += tree[lo ^ 1];
            if(hi & 1) ans += tree[hi ^ 1];
            lo /= 2; hi /= 2;
        }
        return ans;
    }
};

vector<int> sons[nax];
int pre[nax];
int post[nax];
int sz[nax];
int timer;
int nxt[nax];
int whopre[nax];
int parent[nax];

void dfs_sz(int v){
    sz[v] = 1;
    for(auto &u : sons[v]){
        dfs_sz(u);
        parent[u] = v;
        sz[v] += sz[u];
        if(sz[u] > sz[sons[v][0]]){
            swap(u, sons[v][0]);
        }
    }
}

void dfs_hld(int v){
    pre[v] = ++timer;
    for(auto u : sons[v]){
        nxt[u] = (u == sons[v][0] ? nxt[v] : u);
        dfs_hld(u);
    }
    post[v] = ++timer;
}

zwykleDrzewo drzewo;

int lastlit(int v){
    while(true){
        int gora = nxt[v];
        if(drzewo.sum(pre[gora], pre[v]) == 0){
            v = parent[gora];
            continue;
        }
        int prePos = drzewo.ostatni(pre[gora], pre[v]);
        return whopre[prePos];
    }
    assert(false);
}

void change(int v, int delta){
    drzewo.add(pre[v], delta);
}

vector<int> adj[nax];
int limit[nax];

void dfs(int v, int p){
    change(v, +1);
    for(int s : adj[v]){
        if(s == p) continue;
        limit[s] = lastlit(s);
        dfs(s, v);
    }
    change(v, -1);
}

int rightPrefsum[nax];
int leftPrefsum[nax];

pii typek[nax];

void solve(){
    cin >> n;
    rep(i, 1, n) cin >> l[i] >> r[i];
    l[n + 1] = 0;
    r[n + 1] = n * 2 + 1;
    n += 1;
    // 0 ... 2 * n - 1
    rep(i, 0, nax - 1) leftEdge[i] = rightEdge[i] = -1;
    secik poczatki;
    poczatki.init(n + 5);
    segtree konce;
    konce.init(n + 5);
    rep(i, 1, n){
        typek[l[i]] = mp(i, +1);
        typek[r[i]] = mp(i, -1);
    }
    
    rep(i, 0, n * 2 - 1){
        auto [id, op] = typek[i];
        if(op == +1){
            int dokad = poczatki.lowerbound(id);
            if(dokad != -1){
                leftEdge[id] = dokad;
            }
            poczatki.add(id, +1);
            if(leftEdge[id] != -1){
                int low = id;
                int high = leftEdge[id];
                naLewo[id] = konce.sum(low, high);
            }
        }
        else{
            poczatki.add(id, -1);
            konce.add(id, +1);
        }
    }
    
    poczatki.init(n + 5);
    konce.init(n + 5);

    per(i, 0, n * 2 - 1){
        auto [id, op] = typek[i];
        if(op == -1){
            int dokad = poczatki.lowerbound(id);
            if(dokad != -1){
                rightEdge[id] = dokad;
            }
            poczatki.add(id, +1);
            if(rightEdge[id] != -1){
                int low = id;
                int high = rightEdge[id];
                naPrawo[id] = konce.sum(low, high);
            }
        }
        else{
            poczatki.add(id, -1);
            konce.add(id, +1);
        }
    }

    rep(i, 1, n - 1){
        int ja = i;
        int upward = rightEdge[i];
        sons[upward].pb(ja);
    }
    
    rep(i, 1, n - 1){
        int ja = i;
        int on = leftEdge[i];
        adj[ja].pb(on);
        adj[on].pb(ja);
    }

    dfs_sz(n);
    nxt[n] = n;
    dfs_hld(n);
    rep(i, 1, n) whopre[pre[i]] = i;
    drzewo.init(n * 2 + 77);
    dfs(n, n);

    per(i, 1, n - 1){
        rightPrefsum[i] = naPrawo[i] + rightPrefsum[rightEdge[i]];
        leftPrefsum[i] = naLewo[i] + leftPrefsum[leftEdge[i]];
    }

    ll ans = 0;
    rep(i, 1, n - 1){
        int skipped = i - 1;
        skipped += leftPrefsum[i] - leftPrefsum[limit[i]];
        skipped += rightPrefsum[i] - rightPrefsum[limit[i]];
        skipped += (n - limit[i] + 1);
        int visited = n - skipped;
        ans += inv(visited);
        ans %= mod;
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tt = 1;
    // cin >> tt;
    rep(te, 1, tt) solve();
    return 0;
}