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
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("popcnt")

using namespace std;
#define PB push_back
#define LL long long
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORD(i,a,b) for (int i = (a); i >= (b); i--)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define VI vector<int>
#define PII pair<int,int>
#define LD long double

template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.st << "," << P.nd << ")";
}

template<class T> ostream &operator<<(ostream &os, vector<T> V){
    os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]";
}

template<class L, class R> ostream &operator<<(ostream &os, map<L,R> M) {
    os<<"[";for(auto v:M)os<<v<<",";return os<<"]";
}

template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}


#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const LL mod = 1e9 + 7;
const int MAXN = 1e6 + 5;
vector<LL> fact(MAXN, 1);
vector<LL> inv(MAXN, 1);

LL RevMod(LL a, LL b) {
  return a > 1 ? b - RevMod(b % a, a) * b / a : 1;
}

void calc_fact() {
    FOR(i, 1, MAXN - 1) {
        fact[i] = fact[i - 1] * i % mod;
    }
    inv[MAXN - 1] = RevMod(fact[MAXN - 1], mod);
    FORD(i, MAXN - 2, 0) {
        inv[i] = inv[i + 1] * (i + 1) % mod;
    }
}

void mul(LL &a, LL b) {
    a *= b;
    a %= mod;
}

void sub(LL &a, LL b){
    a -= b;
    if(a < 0)
        a += mod;
}

void add(LL &a, LL b){
    a += b;
    if(a >= mod)
        a -= mod;
}

LL binom(LL n, LL k) {
  if (n < k) {
    return 0;
  }
  return fact[n] * inv[k] % mod * inv[n - k] % mod;
}

// k piłek do n kubełków
LL divisions_into_groups(LL k, LL n) {
    return binom(k + n - 1, n - 1);
}

LL get_res(LL n, LL reachable){
    reachable--;
    LL res = fact[reachable];
    LL remaining = n - reachable - 1;
    mul(res, divisions_into_groups(remaining, reachable + 2));
    mul(res, fact[remaining]);
    return res;
}

typedef unsigned long long ULL;

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(11);
    cerr << fixed << setprecision(6);
    calc_fact();
    int n;
    cin >> n;
    vector<PII> edges(n, PII{-1, -1});
    vector<PII> intervals(n);
    REP(i, n) {
        cin >> intervals[i].st >> intervals[i].nd;
    }
    map<int, int*> info; // position, index
    FORD(i, n - 1, 0){
        int l = intervals[i].st;
        int r = intervals[i].nd;
        debug(i, l, r, info);
        auto it = info.lower_bound(l);
        //collect
        while(it != info.end() && it->first <= r){
            *(it->second) = i;
            it = info.erase(it);
        }
        info[l] = &edges[i].st;
        info[r] = &edges[i].nd;
    }
    vector<PII> edges_list;
    REP(i, n){
        if(edges[i].st != -1){
            edges_list.PB({i, edges[i].st});
        }
        if(edges[i].nd != -1 && edges[i].st != edges[i].nd){
            edges_list.PB({i, edges[i].nd});
        }
    }
    debug(edges_list);
    VI reachable(n);
    vector<ULL> masks(n);
    for(int i = n - 1; i >= 0; i -= 64){
        int li = max(0, i - 64 + 1);
        int ri = i;
        int idx = 0;
        FOR(j, li, ri){
            masks[j] = 1ULL << idx;
            idx++;
        }
        while(!edges_list.empty() && edges_list.back().st > ri){
            edges_list.pop_back();
        }
        int todo = ri;
        FORD(j, SZ(edges_list) - 1, 0){
            int a = edges_list[j].st;
            int b = edges_list[j].nd;
            masks[b] |= masks[a];
        }
        FORD(j, ri, 1){
            reachable[j] += __builtin_popcountll(masks[j]);
            masks[j] = 0;
        }
        reachable[0] += __builtin_popcountll(masks[0]);
        masks[0] = 0;
    }
    LL res = 0;
    REP(i, n){
        add(res, get_res(n, reachable[i]));
        debug(get_res(n, reachable[i]));
    }
    mul(res, inv[n]);
    cout << res << "\n";
}