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

#ifndef LOCAL
#pragma GCC optimize("O3")
#endif

#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define endl '\n'
#define sp <<" "<<
#define eb emplace_back
#define MOD 1000000007
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

using ll = long long;
#define vec vector

template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; }
template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; }

#define fora(a) for(auto e:a)
#define it(i,s,e) for(long long int i=s;i<e;i++)
#define ita(i,s,e) for(long long int i=s;i<=e;i++)
#define itr(i,e,s) for(long long int i=e-1;i>=s;i--)
#define urs(r...)typename decay<decltype(r)>::type
#define rep(i,n)for(urs(n)i=0;i<(n);++i)

int n;
const int MAX_N = 500000 + 10;
const int MAX_M = 2 * MAX_N;
const int BATCH_SIZE = MAX_N/395;

vec<int> p(MAX_N);
vec<vec<int>> children(MAX_N);

ll powmod(ll a, ll x) {
    ll res = 1;
    ll exp = a;
    while (x) {
        if (x&1) {
            res = (res*exp) % MOD;
        }
        exp = (exp*exp) % MOD;
        x = x >> 1;
    }
    return res;
}

ll inverse_euler(ll n) {
    return powmod(n, MOD-2);
}

template<typename Bitset>
void generic_solver()
{
    vec<Bitset> bit_anc(n);
    //cout << bit_anc[0].size() << endl;
    
    for (int i=0; i<n; i++) {
        bit_anc[i].set(i);
        for (int c: children[i]) {
            bit_anc[c] |= bit_anc[i];
        }
    }

    ll res = 0;
    rep(i, n) {
        res = (res + inverse_euler(bit_anc[i].count())) % MOD;
    }
    cout << res << endl;
}

// void solve_slow() {
//     for (int i=0; i<n; i++) {
//         bitset<MAX_N> visited;
//         vec<int> q;
//         q.eb(i);
//         while (!q.empty()) {
//             int curr = q.back();
//             q.pop_back();
//             if (visited.test(curr)) continue;

//             visited.set(curr);
//             p[curr]++;
//             for (int c: children[curr]) {
//                 q.eb(c);
//             }
//         }
//     }

//     ll res = 0;
//     rep(i, n) {
//         res = (res + inverse_euler(p[i])) % MOD;
//     }
//     cout << res << endl;
// }



void solve() {
    // if (n <= MAX_N/10) {
    //     generic_solver<bitset<MAX_N/10>>();
    //     return;
    // }
    // if (n <= 85000) {
    //     generic_solver<bitset<85000+10>>();
    //     return;
    // }
    // if (n <= 90000) {
    //     generic_solver<bitset<90000+10>>();
    //     return;
    // }
    // if (n <= MAX_N/5) {
    //     generic_solver<bitset<MAX_N/5>>();
    //     return;
    // }
    // if (n <= MAX_N/4) {
    //     generic_solver<bitset<MAX_N/4>>();
    //     return;
    // }
    // if (n <= MAX_N/3) {
    //     generic_solver<bitset<MAX_N/3>>();
    //     return;
    // }
    // if (n <= MAX_N*2/5) {
    //     generic_solver<bitset<MAX_N*2/5>>();
    //     return;
    // }
    // if (n <= MAX_N/2) {
    //     generic_solver<bitset<MAX_N/2>>();
    //     return;
    // }
    //generic_solver<bitset<MAX_N>>();
    
    int i=0;
    while (i<n) {
        int BASE = i;
        int CURR_BATCH_SIZE = min(BATCH_SIZE, n-i);
        vec<bitset<BATCH_SIZE>> bit_anc(n-BASE);

        for (; i<n; i++) {
            if (i < BASE + CURR_BATCH_SIZE) bit_anc[i-BASE].set(i-BASE);
            p[i] += bit_anc[i-BASE].count();

            for (int c: children[i]) {
                bit_anc[c-BASE] |= bit_anc[i-BASE];
            }
        }
        //cout << BATCH_SIZE << endl;
        i = BASE + BATCH_SIZE;
    }
    
    ll res = 0;
    rep(i, n) {
        res = (res + inverse_euler(p[i])) % MOD;
    }
    cout << res << endl;
}

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

    cin >> n;
    set<pii> hanging_streams; // (loc, source node)

    vec<pii> data;
    rep (i, n) {
        int a,b;
        cin >> a >> b;
        if (a > b) swap(a,b);
        data.eb(a,b);
    }
    reverse(all(data));
    
    rep (i, n) {
        auto [a,b] = data[i];
        //cout << a sp b  sp hanging_streams.size() << endl;
        auto upper = upper_bound(hanging_streams.begin(), hanging_streams.end(), mp(b,0));
        auto lower = lower_bound(hanging_streams.begin(), hanging_streams.end(), mp(a,0));
        //lower++;

        //cout << (*upper).fi sp (*lower).fi << endl;
        vec<pii> to_erase;
        while (lower != upper) {
            //cout << lower->fi sp lower->se << endl;
            children[(*lower).se].eb(i);
            lower++;
        }
        lower = lower_bound(hanging_streams.begin(), hanging_streams.end(), mp(a,0));
        hanging_streams.erase(lower,upper);

        hanging_streams.insert(mp(a,i));
        hanging_streams.insert(mp(b,i));
    }

    // rep (i, n) {
    //     print_v(children[i]);
    // }

    solve();

    return 0;
}