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
#include <bits/stdc++.h>
#define dbg(x) " [" << #x << ": " << (x) << "] "
using namespace std;
using ll = long long;
template<typename A, typename B>
ostream& operator<<(ostream& out, const pair<A,B>& p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& c) {
    out << "{";
    for(auto it = c.begin(); it != c.end(); it++) {
        if(it != c.begin()) out << ", ";
        out << *it;
    }
    return out << "}";
}

const int mod = 1e9 + 7;
const int lg = 20; // TODO maybe change

ll bp(ll a, ll b) {
    ll r = 1;
    while(b) {
        if(b & 1) r = r * a % mod;
        b /= 2;
        a = a * a % mod;
    }
    return r;
}

vector<int> t;
int sz;

void upd(int i, int x) {
    for(t[i += sz] = x; i > 1; i >>= 1) {
        t[i >> 1] = max(t[i], t[i ^ 1]);
    }
}

int get(int l, int r) {
    int mx = 0;
    for(l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
        if(l & 1) mx = max(mx, t[l++]);
        if(r & 1) mx = max(mx, t[--r]);
    }
    return mx;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

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

    int n;
    cin >> n;
    vector<int> l(n + 1), r(n + 1), id(2 * n + 1);
    sz = n + 1;
    t.resize(2 * sz);
    for(int i = 1; i <= n; i++) {
        cin >> l[i] >> r[i];
        id[l[i]] = i;
        id[r[i]] = i;
    }

    vector<vector<int>> lt(n + 1), rt(n + 1);
    for(int i = 0; i <= n; i++) {
        lt[i].resize(lg);
        rt[i].resize(lg);
    }
    for(int i = 1; i <= 2 * n; i++) {
        int j = id[i];
        int mx = get(0, j);
        if(l[j] == i) {
            lt[j][0] = mx;
            upd(j, j);
        } else {
            rt[j][0] = mx;
            upd(j, 0);
        }
    }
    for(int j = 1; j < lg; j++) {
        for(int i = 1; i <= n; i++) {
            lt[i][j] = lt[lt[i][j - 1]][j - 1];
            rt[i][j] = rt[rt[i][j - 1]][j - 1];
        }
    }

    vector<ll> f(n + 1), invf(n + 1), inv(n + 1);
    f[0] = 1;
    for(int i = 1; i <= n; i++) {
        f[i] = i * f[i - 1] % mod;
    }
    invf.back() = bp(f.back(), mod - 2);
    inv[n] = invf[n] * f[n - 1] % mod;
    for(int i = n - 1; i >= 1; i--) {
        invf[i] = invf[i + 1] * (i + 1) % mod;
        inv[i] = invf[i] * f[i - 1] % mod;
    }

    auto find_left = [&](int id, int h) {
        for(int j = lg - 1; j >= 0; j--) {
            int nxt = lt[id][j];
            if(nxt >= h) id = nxt;
        }
        return id;
    };

    vector<int> mid(n + 1);
    for(int i = 1; i <= n; i++) {
        if(!lt[i][0] || !rt[i][0]) {
            continue;
        }
        int cur = lt[i][0];
        if(cur == find_left(rt[i][0], cur)) {
            mid[i] = cur;
        } else {
            for(int j = lg - 1; j >= 0; j--) {
                int nxt = rt[cur][j];
                if(nxt > rt[i][0]) {
                    cur = nxt;
                } else if(nxt < rt[i][0]) {
                    int x = find_left(rt[i][0], nxt);
                    if(l[x] > r[nxt]) cur = nxt;
                }
            }
            mid[i] = rt[cur][0];
        }
    }

    vector<ll> res(n + 1, 1);
    for(int i = n; i >= 1; i--) {
        res[lt[i][0]] += res[i];
        res[rt[i][0]] += res[i];
        res[mid[i]] -= res[i];
    }

    ll ans = 0;
    for(int i = 1; i <= n; i++) {
        ans += inv[res[i]];
        if(ans >= mod) ans -= mod;
    }
    cout << ans << endl;

    return 0;
}