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
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <vector>

#define REP(a, n) for (int a = 0; a < (n); ++a)
#define FOR(a, b, c) for (int a = (b); a <= (c); ++a)
#define FORD(a, b, c) for (int a = (b); a >= (c); --a)

typedef long long LL;

using namespace std;

template<class T> inline int vsize(const T &t) { return t.size(); }

/////////////////

#define MOD 1'000'000'007

void gcdExtended(LL a, LL b, LL &x, LL &y) {
    if (a == 0) {
        x = 0, y = 1;
    }
    else {
        LL y1;
        gcdExtended(b % a, a, y, y1);
        x = y1 - (b / a) * y;
    }
}

LL inv(int A)
{
    LL x, y;
    gcdExtended(A, MOD, x, y);
    return (x % MOD + MOD) % MOD;
}

LL licz = 0, mian = 1;

inline void dodaj(LL l2, LL m2) {
    licz = (licz * m2 + l2 * mian) % MOD;
    mian = (mian * m2) % MOD;
}

/////

#define MAXN 500'005
#define SIZE (1<<20)

void dp_wstaw(int *dp, int p, int v) {
    p += SIZE;
    while (p) {
        dp[p] += v;
        p /= 2;
    }
}

int dp_ile(int *dp, int x, int y, int p = 1, int s = SIZE) { // koniec wliczamy
    if (x==0 && y==s-1)
        return dp[p];
    s /= 2;
    return (x<s ? dp_ile(dp, x, min(y, s-1), 2*p, s) : 0) + (y>=s ? dp_ile(dp, max(0, x-s), y-s, 2*p+1, s) : 0);
}

int dp_ilezeruj(int *dp, int x, int y, int p = 1, int s = SIZE) { // koniec wliczamy
    int ret = 0;
    if (s==1) {
        ret = dp[p];
        dp[p] = 0;
    }
    else {
        s /= 2;
        if (x<s && dp[2*p])
            ret += dp_ilezeruj(dp, x, min(y, s-1), 2*p, s);
        if (y>=s && dp[2*p+1])
            ret += dp_ilezeruj(dp, max(0, x-s), y-s, 2*p+1, s);
        dp[p] = dp[2*p]+dp[2*p+1];
    }
    return ret;
}

int lewe[2*SIZE], prawe[2*SIZE];

/////

#define SIZE2 (SIZE*4)

int N;
int kra[MAXN][2]; //, dach[MAXN];

int d_status[2*SIZE2]; // 0 = na calosci to samo, 1 = schodz w dol
int d_kto[2*SIZE2];
int d_koniec[MAXN][2], d_par[MAXN][2];

int d_find(int a, int b) {
    return d_par[a][b]==a ? a : (d_par[a][b] = d_find(d_par[a][b], b));
}

void szukaj(int x, int y, vector<int> &wyn, int p = 1, int s = SIZE2) {
    if (d_status[p]==0) {
        if (d_kto[p])
            wyn.push_back(d_kto[p]);
        return;
    }
    s /= 2;
    if (x<s)
        szukaj(x, min(y, s-1), wyn, 2*p, s);
    if (y>=s)
        szukaj(max(0, x-s), y-s, wyn, 2*p+1, s);
}

void ustaw(int x, int y, int ja, int p = 1, int s = SIZE2) {
    if (x==0 && y==s-1) {
        d_status[p] = 0;
        d_kto[p] = ja;
        return;
    }
    if (d_status[p]==0) {
        d_status[p] = 1;
        d_status[2*p] = d_status[2*p+1] = 0;
        d_kto[2*p] = d_kto[2*p+1] = d_kto[p];
    }
    s /= 2;
    if (x<s)
        ustaw(x, min(y, s-1), ja, 2*p, s);
    if (y>=s)
        ustaw(max(0, x-s), y-s, ja, 2*p+1, s);
}

void ustaw_p(int x, int ja, int p = 1, int s = SIZE2) {
    if (s==1) {
        d_status[p] = 0;
        d_kto[p] = ja;
        return;
    }
    if (d_status[p]==0) {
        d_status[p] = 1;
        d_status[2*p] = d_status[2*p+1] = 0;
        d_kto[2*p] = d_kto[2*p+1] = d_kto[p];
    }
    s /= 2;
    if (x<s)
        ustaw_p(x, ja, 2*p, s);
    else
        ustaw_p(x-s, ja, 2*p+1, s);
}

int znajdz_dach(int ja) {
    d_par[ja][0] = d_par[ja][1] = ja;
    int L = d_koniec[ja][0] = kra[ja][0];
    int R = d_koniec[ja][1] = kra[ja][1];
    vector<int> nad;
    szukaj(3*L, 3*R, nad);
    ustaw(3*L+2, 3*R, ja);
//    sort(nad.begin(), nad.end());
//    nad.resize(unique(nad.begin(), nad.end()) - nad.begin());
    int ret = 0;
    int lewy = 0, prawy = 0;
    for (int n : nad) {
        int ll, rr;
        int Ln = d_koniec[ll = d_find(n, 0)][0];
        int Rn = d_koniec[rr = d_find(n, 1)][1];
        if (Ln<L && Rn>R)
            ret = n;
        if (Rn<=R) {
            if (!lewy)
                d_koniec[lewy = rr][1] = L;
            else
                d_par[rr][1] = lewy;
        }
        if (Ln>=L) {
            if (!prawy)
                d_koniec[prawy = ll][0] = R;
            else 
                d_par[ll][0] = prawy;
        }
    }
    ustaw_p(3*L+1, ret);
    ustaw_p(3*R+1, ret);
    return ret;
}

/////

int wynik[MAXN], naddachowe[MAXN];

int main() {
    scanf("%d", &N);
    FORD(a, N, 1)
        scanf("%d%d", &kra[a][0], &kra[a][1]);
    FOR(a, 1, N) {
        int poza = dp_ile(lewe, 0, kra[a][0]-1)+dp_ile(prawe, kra[a][1]+1, 2*N+100);
        int na_mnie = dp_ilezeruj(lewe, kra[a][0], kra[a][1]);
        dp_wstaw(lewe, kra[a][1], na_mnie+1);
        na_mnie = dp_ilezeruj(prawe, kra[a][0], kra[a][1]);
        dp_wstaw(prawe, kra[a][0], na_mnie+1);
        int d = znajdz_dach(a);
        naddachowe[a] = a-poza;
        if (d) poza += naddachowe[d];
        wynik[a] = a-poza;
        dodaj(1, wynik[a]);
        //fprintf(stderr, "%d -> %d\n", a, wynik[a]);
//        fprintf(stderr, "%d -> %d / %d\n", a, wynik[a], d);
    }
    int res = licz*inv(mian)%MOD;
    printf("%d\n", res);
}