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
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define L long long
#define MP make_pair
#define REP(i, n) for(int i = 0; i < n; ++i)
#define REPR(i, n) for(int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, a, b) for(int i = b - 1; i >= a; --i)
#define EB emplace_back
#define ST first
#define ND second
#define S size
#define RS resize

template<class T> using P = pair<T, T>;
template<class T> using V = vector<T>;

const L M = (int)1e9 + 7;

struct Tree {
    int ts = 1, ls = 0;
    V<int> val;

    Tree(int n) {
        while (ts / 2 <= n) {
            ts *= 2;
        }
        ls = ts / 2;
        val.RS(ts, -1);
    }

    void insert(int a, int b, int c) {
        a += ls, b += ls;
        val[a] = val[b] = c;
        while ((a >> 1) != (b >> 1)) {
            if ((a & 1) == 0) {
                val[a + 1] = c;
            }
            if ((b & 1) == 1) {
                val[b - 1] = c;
            }
            a >>= 1, b >>= 1;
        }
    }

    int query_max(int a) {
       a += ls;
       int res = val[a];
       while (a > 0) {
          a >>= 1;
          res = max(res, val[a]);
       }
       return res;
    }
};

L power(L a, L b) {
    if (b == 0) {
        return 1LL;
    }

    if ((b & 1) == 0) {   
        L s = power(a, b >> 1LL);
        L res = s * s;
        return res >= M ? res % M : res;
    }
    else {
        L s = power(a, b - 1LL);
        L res = s * a;
        return res >= M ? res % M : res;
    }
}

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

    int n;
    cin >> n;

    V<int> l(n), r(n);
    REP(i, n) {
        cin >> l[i] >> r[i];
        l[i] <<= 1, r[i] <<= 1;
    }

    Tree tree(4 * n + 1);

    const int LOG = 20;
    V<V<int>> jpl(LOG, V<int>(n, -1));
    V<V<int>> jpr(LOG, V<int>(n, -1));
    V<int> ls(n), rs(n), cs(n, -1);

    auto find_cs = [&] (int v) {
        int lv = ls[v], rv = rs[v];
        if (lv < 0 || rv < 0) {
            return;
        } else if (lv == rv) {
            cs[v] = lv;
            return;
        }
        int ml = l[v], mr = r[v];
        while (ml < mr) {
            int s = (ml + mr) >> 1;
            int lj = lv;
            REPR(i, LOG) {
                int nlj = jpr[i][lj];
                if (nlj != -1 && r[nlj] <= s) {
                    lj = nlj;
                }
            }
            if (r[lj] < s && rs[lj] != -1) {
                lj = rs[lj];
            }
            int rj = rv;
            REPR(i, LOG) {
                int nrj = jpl[i][rj];
                if (nrj != -1 && l[nrj] >= s) {
                    rj = nrj;
                }
            }
            if (l[rj] > s && ls[rj] != -1) {
                rj = ls[rj];
            }

            if (lj == rj) {
                cs[v] = lj;
                return;
            } else if (lj < rj) {
                mr = s;
            } else {
                ml = s + 1;
            }
        }

        cs[v] = -1;
    };

    REP(i, n) {
        ls[i] = tree.query_max(l[i]);
        rs[i] = tree.query_max(r[i]);

        if (ls[i] != -1) {
            jpl[0][i] = ls[i];
            FOR(j, 1, LOG) {
                int k = jpl[j - 1][i];
                if (k == -1) {
                    break;
                }
                jpl[j][i] = jpl[j - 1][k];
            }
        }
        if (rs[i] != -1) {
            jpr[0][i] = rs[i];
            FOR(j, 1, LOG) {
                int k = jpr[j - 1][i];
                if (k == -1) {
                    break;
                }
                jpr[j][i] = jpr[j - 1][k];
            }
        }

        find_cs(i);
        tree.insert(l[i], r[i], i);
    }

    V<L> z(n);
    REPR(i, n) {
        L& w = z[i];
        w++;
        if (ls[i] != -1) {
            z[ls[i]] += w;
        }
        if (rs[i] != -1) {
            z[rs[i]] += w;
        }
        if (cs[i] != -1) {
            z[cs[i]] -= w;
        }
    }


    L res = 0;
    REP(i, n) {
        res += power(z[i], M - 2);
    }
    cout << res % M << '\n';
}