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
#pragma GCC target("sse,sse2,sse3,sse4.1,sse4.2,avx,avx2")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_set>
#include <vector>
#include <climits>
using namespace std;
 
using ll = long long;
using ul = unsigned long long;
using db = long double;
using pi = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define x first
#define y second

template<class T> using V = vector<T>; 
template<class T, size_t SZ> using AR = array<T,SZ>; 

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define each(a,x) for (auto& a: x)
 
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define per(i,a,b) for(int i = (b) - 1; i >= (a); i--)

#ifdef LOCAL 
template<class A, class B> auto& operator<<(auto &o, pair<A, B> p) { return o << '(' << p.x << ", " << p.y << ')'; }
auto& operator<<(auto& o, auto a) {
    o << "{";
    for (auto b : a) o << b << ", ";
    return o << "}";
}
void dump(auto... x) { ((cerr << x << ", "), ...) << "\n"; }
#define debug(x...) cerr << "[" #x "]: ", dump(x)
#else
#define debug(...) ;
#endif

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }
template<class T> int upb(V<T>& a, const T& b) { return int(ub(all(a),b)-bg(a)); }
template<class T> void remDup(vector<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); }

template<int MOD, int RT> struct mint {
    static const int mod = MOD;
    static constexpr mint rt() { return RT; }
    int v; explicit operator int() const { return v; }
    mint():v(0) {}
    mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);
        if (v < 0) v += MOD; }
    bool operator==(const mint& o) const {
        return v == o.v; }
    friend bool operator!=(const mint& a, const mint& b) { 
        return !(a == b); }
    friend bool operator<(const mint& a, const mint& b) { 
        return a.v < b.v; }
    friend ostream& operator<<(ostream& os, mint& a) { return os << a.v; }
    friend istream& operator>>(istream& os, mint& a) { ll x; os >> x; a = mint(x); return os; }

    mint& operator+=(const mint& o) { 
        if ((v += o.v) >= MOD) v -= MOD; 
        return *this; }
    mint& operator-=(const mint& o) { 
        if ((v -= o.v) < 0) v += MOD; 
        return *this; }
    mint& operator*=(const mint& o) { 
        v = int((ll)v*o.v%MOD); return *this; }
    mint& operator/=(const mint& o) { return (*this) *= inv(o); }
    friend mint pow(mint a, ll p) {
        mint ans = 1; assert(p >= 0);
        for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans; }
    friend mint inv(const mint& a) { assert(a.v != 0); 
        return pow(a,MOD-2); }
        
    mint operator-() const { return mint(-v); }
    mint& operator++() { return *this += 1; }
    mint& operator--() { return *this -= 1; }
    friend mint operator+(mint a, const mint& b) { return a += b; }
    friend mint operator-(mint a, const mint& b) { return a -= b; }
    friend mint operator*(mint a, const mint& b) { return a *= b; }
    friend mint operator/(mint a, const mint& b) { return a /= b; }
};

constexpr int MOD = 1e9+7;
using mi = mint<MOD,5>;
using vmi = V<mi>;

const int BASE = 1<<20;
int tree[2 * BASE];

void chg(int l, int r, int v) {
    for (l += BASE, r += BASE + 1; l < r; l /= 2, r /= 2) {
        if (l & 1) tree[l++] = v;
        if (r & 1) tree[--r] = v;
    }
}

int qry(int p) {
    int res = 0;
    for (p += BASE; p; p /= 2) ckmax(res, tree[p]);
    return res;
}

const int B_SIZE = 512;
const int N = 5e5+B_SIZE;
using B = bitset<B_SIZE>;
B can_reach[N];
mi res = 0;
int total_reach_size[N];
vi g[N];
int l[N], r[N];
int n;

void create_graph() {
    rep(i,0,n) {
        int l_son = qry(l[i]) - 1;
        int r_son = qry(r[i]) - 1;
        if (l_son != -1) {
            g[l_son].pb(i);
        }
        if (r_son != -1 && r_son != l_son) {
            g[r_son].pb(i);
        }
        chg(l[i], r[i], i + 1);
    }
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    auto start = clock();

    cin >> n;
    rep(i,0,n) cin >> l[i] >> r[i];
    rep(i,0,n) {
        // l[i] = r[i] = 1;
        // l[i] = rand() % (2 * n) + 1;
        // r[i] = rand() % (2 * n) + 1;
        // if (l[i] > r[i]) swap(l[i], r[i]);
    }

    create_graph();

    // rep(i,0,n) {
    //     int lef = -1;
    //     int rig = -1;
    //     per(j,0,i) {
    //         if (l[j] <= l[i] && l[i] <= r[j]) {
    //             lef = j;
    //             break;
    //         }
    //     }
    //     per(j,0,i) {
    //         if (l[j] <= r[i] && r[i] <= r[j]) {
    //             rig = j;
    //             break;
    //         }
    //     }
    //     if (lef != -1) {
    //         g[lef].pb(i);
    //         // g[i].pb(lef);
    //     }
    //     if (rig != -1 && rig != lef) {
    //         g[rig].pb(i);
    //         // g[i].pb(rig);
    //     }
    // }


    for (int i = 0; i < n; i += B_SIZE) {
        rep(j, 0, i + B_SIZE) {
            can_reach[j].reset();
        }
        rep(j, 0, B_SIZE) {
            if (i + j >= n) break;
            can_reach[i + j][j] = 1;
        }
        per(j, 0, i + B_SIZE) {
            for (auto v : g[j]) {
                can_reach[j] |= can_reach[v];
            }
            total_reach_size[j] += (int)can_reach[j].count();
        }
    }

    per(i,0,n) {
        res += mi(1) / mi(total_reach_size[i]);
    }

    cout << res << '\n';

    auto finish = clock();

    cerr << fixed << setprecision(2) << (((double)finish - start) / CLOCKS_PER_SEC) << endl;

    return 0;
}