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

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif

#define x first
#define y second
#define ir(a, x, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()

using ll = long long;
const int _N = int(5e5)+20, K = 20;

int N;
vec<array<int, 2>> g;
int bj[K][2][_N];
vec<int> ct;
vec<pair<int, int>> coords;



const int MOD = int(1e9)+7;
struct mint {
  int value;

  mint(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
  mint(long long a, long long b) : value(0){ *this += a; *this /= b;}

  mint& operator+=(mint const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
  mint& operator-=(mint const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
  mint& operator*=(mint const& b) {value = (long long)value * b.value % MOD;return *this;}

  friend mint mexp(mint a, long long e) {
    mint res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
    return res;
  }
  friend mint inverse(mint a) { return mexp(a, MOD - 2); }

  mint& operator/=(mint const& b) { return *this *= inverse(b); }
  friend mint operator+(mint a, mint const b) { return a += b; }
  friend mint operator-(mint a, mint const b) { return a -= b; }
  friend mint operator-(mint const a) { return 0 - a; }
  friend mint operator*(mint a, mint const b) { return a *= b; }
  friend mint operator/(mint a, mint const b) { return a /= b; }
  friend std::ostream& operator<<(std::ostream& os, mint const& a) {return os << a.value;}
  friend bool operator==(mint const& a, mint const& b) {return a.value == b.value;}
  friend bool operator!=(mint const& a, mint const& b) {return a.value != b.value;}
};


// #define TEST
void graph() {
    vec<pair<int, pair<int, bool>>> events;
    events.reserve(2*N);
    
    coords[0] = {-1, 2*N+1};
#ifdef TEST
    mt19937 gen;
    vec<int> perm(2*N);
    iota(all(perm), 1);
    shuffle(all(perm), gen);
    // debug(perm);
#endif
    rep (i, 1, N) {
        int a, b;
#ifdef TEST
        a = perm[2*i-2], b = perm[2*i-1];
        if (a > b) swap(a, b);
#else
        cin >> a >> b;
#endif
        coords[i] = {a, b};
        events.push_back({a, {i, true}});
        events.push_back({b, {i, false}});
    }
    
    sort(all(events));
    set<int> broom;
    broom.insert(0); // add ground
    for (auto [_, data] : events) {
        // debug(data, broom);
        auto [i, add] = data;
        auto it = broom.lower_bound(i);
        auto it2 = it;
        it2--;
        // to avoid duplicates, we check that the other child (the first child) of i isn't *it
        g[i][!add] = *it2; // add child
        if (add) broom.insert(it, i);
        else broom.erase(it);
    }
    debug(g);
}

int jumpto(int v, bool b, int i) { // jumps to the lowest platform of height >= i
    // assert(v >= i);
    for (int k = K-1; k >= 0; --k) {
        if (bj[k][b][v] >= i) {
            v = bj[k][b][v];
        }
        if (v == i) return v;
    }
    return v;
}

int findmeet(int vl, int vr) {
    // vl jumps right, vr jumps left
    // for (int k = K-1; k >= 0; --k) {
    //     if (coords[bj[k][1][vl]].y < coords[bj[k][0][vr]].x) {
    //         vl = bj[k][1][vl];
    //         vr = bj[k][0][vr];
    //         v = bj[k][b][v];
    //     }
    // }
    int l = 0, r = min(vl, vr)+1;
    while (l+1 < r) {
        int m = (l+r)/2;
        debug(l, m, r);
        int _t0, _t1;
        if (coords[_t0 = jumpto(vl, 1, m)].y < coords[_t1 = jumpto(vr, 0, m)].x) {
            r = m;
        } else {
            l = m;
        }
        // if (_t0 == _t1) return _t0;
        debug(_t0, coords[_t0]);
        debug(_t1, coords[_t1]);
    }
    return l;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef TEST
    N = 500000; ++N;
    // N = 10; ++N;
    #else
    cin >> N; ++N; // n will be artificial node - the ground
    #endif
    g.resize(N);
    ct.assign(N, 1);
    coords.resize(N);
    graph();
    
    // topo sort - do not reverse for now
    rep (v, 0, N) {
        bj[0][0][v] = g[v][0];
        bj[0][1][v] = g[v][1];
        rep (b, 0, 2) rep (k, 1, K) {
            if (bj[k-1][b][v] == 0) break;
            bj[k][b][v] = bj[k-1][b][bj[k-1][b][v]];
        }
        // #ifdef LOCAL
        // rep (b, 0, 2) {
        //     debug(v, b);
        //     rep (k, 0, K) {
        //         cout << bj[k][b][v] << " ";
        //     }
        //     cout << "\n";
        // }
        // #endif
    }
    
    mint s = 0;
    for (int v = N-1; v > 0; --v) {
        int l = g[v][0], r = g[v][1];
        ct[l] += ct[v];
        ct[r] += ct[v];
        int meet = findmeet(l, r);
        debug(v, l, r, meet, ct[v]);
        ct[meet] -= ct[v];
        // s += mint(1);
        s += mint(1)/ct[v];
    }
    debug(ct);
    cout << s << "\n";
    return 0;
}