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
211
212
213
214
215
216
217
218
219
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;

__always_inline int rint() { static int res; static char c; res = 0; do { c = getchar_unlocked(); } while(!isdigit(c)); while(isdigit(c)) { res = 10 * res + c - '0', c = getchar_unlocked(); } return res; }
__always_inline void pint(int x) { static int ptr; static char buff[20]; ptr = 0; do { buff[ptr++] = '0' + x % 10, x /= 10; } while(x); while(ptr) { putchar_unlocked(buff[--ptr]); } }

#pragma GCC maksuje_zadania_na_essie("pot200");
#define loop(i, a, b) for(int i = a; i <= b; i++)
#define loop_rev(i, a, b) for(int i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define pb push_back

using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;

struct Shelf { int x1, x2, y; };
constexpr ll MOD = 1e9 + 7;

ll mul(ll a, ll b) { return (a * b % MOD); }
ll add(ll a, ll b) { return (a + b >= MOD ? a + b - MOD : a + b); }
ll sub(ll a, ll b) { return (a - b < 0 ? a - b + MOD : a - b); }
ll qpow(ll a, ll b) {
  ll res = 1;
  while(b) {
    if(b&1) res = mul(res, a);
    a = mul(a, a);
    b /= 2;
  }
  return res;
}

constexpr int MAX_N = 5e5 + 1;
ll fact_inv[MAX_N];
ll fact[MAX_N];

constexpr int LOG = 19;
int jmp[MAX_N][2][LOG];
int depth[MAX_N][2];
Shelf shelves[MAX_N];
int lg;

int lift(int x, int h, int type) {
  int i = 0;
  while(h > 0) {
    if(x == (-1)) return (-1);
    if(h&1) {
      x = jmp[x][type][i];
    }
    h /= 2;
    ++i;
  }
  return x;
}

int first_greater_end(int v, int x) {
  if(shelves[v].x2 > x) return v;
  loop_rev(i, lg-1, 0) {
    int new_v = jmp[v][1][i];
    if(new_v != (-1) && shelves[new_v].x2 <= x) {
      v = new_v;
    }
  }
  return jmp[v][1][0];
}

int first_smaller_start(int v, int x) {
  if(shelves[v].x1 < x) return v;
  loop_rev(i, lg-1, 0) {
    int new_v = jmp[v][0][i];
    if(new_v != (-1) && shelves[new_v].x1 >= x) {
      v = new_v;
    }
  }
  return jmp[v][0][0];
}

int lift_to(int v, int type, int dest) {
  int lifted = lift(v, depth[v][type] - depth[dest][type], type);
  if(lifted == dest) return dest;
  else return (-1);
}

int lca(int a, int b) {
  if(a == b) return a;
  if(b > a) {
    int l = 0, r = depth[b][0];
    while(l < r) {
      int mid = (l+r)/2;
      int b_lifted = lift(b, mid, 0);
      int a_lifted = first_greater_end(a, shelves[b_lifted].x1);
      if(a_lifted == (-1) || shelves[a_lifted].y < shelves[b_lifted].y) {
        l = mid + 1;
      }
      else {
        r = mid;
      }
    }
    int final_b_lifted = lift(b, l, 0);
    int final_a_lifted = lift_to(a, 1, final_b_lifted);
    if(final_b_lifted == final_a_lifted) return final_a_lifted;
  }
  else {
    int l = 0, r = depth[a][1];
    while(l < r) {
      int mid = (l+r)/2;
      int a_lifted = lift(a, mid, 1);
      int b_lifted = first_smaller_start(b, shelves[a_lifted].x2);
      if(b_lifted == (-1) || shelves[b_lifted].y < shelves[a_lifted].y) {
        l = mid + 1;
      }
      else {
        r = mid;
      }
    }
    int final_a_lifted = lift(a, l, 1);
    int final_b_lifted = lift_to(b, 0, final_a_lifted);
    if(final_a_lifted == final_b_lifted) return final_a_lifted;
  }
  return (-1);
}

int main() {
  cin.tie(0)->sync_with_stdio(false);
  int n = rint();

  lg = int(log2(n)) + 1;
  vector<vector<vector<int>>> graph(n, vector<vector<int>>(2));

  loop(i, 0, n-1) {
    loop(j, 0, LOG-1) {
      jmp[i][0][j] = jmp[i][1][j] = (-1);
    }
  }

  fact[0] = 1;
  loop(i, 1, n) fact[i] = mul(fact[i-1], i);

  fact_inv[n] = qpow(fact[n], MOD-2);
  loop_rev(i, n, 1) fact_inv[i-1] = mul(i, fact_inv[i]);

  loop(i, 0, n-1) {
    shelves[i].x1 = rint();
    shelves[i].x2 = rint();
    shelves[i].y = i;
  }

  set<tuple<int,int,int>> open;

  loop_rev(i, n-1, 0) {
    if(!open.empty()) {
      auto it = open.upper_bound({ shelves[i].x1, -1, -1 });
      while(it != open.end() && get<0>(*it) < shelves[i].x2) {
        graph[get<1>(*it)][get<2>(*it)].pb(i);
        it = open.erase(it);
      }
    }
    open.emplace(shelves[i].x1, i, 0);
    open.emplace(shelves[i].x2, i, 1);
  }

  loop(i, 0, n-1) {
    loop(type, 0, 1) {
      jmp[i][type][0] = (-1);
      for(int s : graph[i][type]) { // cause it can be empty (jajo edge case)
        jmp[i][type][0] = s;
        loop(len, 1, lg-1) {
          if(jmp[i][type][len-1] != (-1)) {
            jmp[i][type][len] = jmp[jmp[i][type][len-1]][type][len-1];
          }
        }
      }
    }
  }

  loop(i, 0, n-1) {
    loop(type, 0, 1) {
      if(graph[i][type].empty()) depth[i][type] = 0;
      else depth[i][type] = depth[graph[i][type][0]][type] + 1;
    }
  }

  vector<int> S(n), S_cnt(n);

  loop_rev(i, n-1, 0) {
    if(sz(graph[i][0]) && sz(graph[i][1])) {
      int x = graph[i][0][0], y = graph[i][1][0];
      int l = lca(x, y);
      S[x] += S[i] + 1, S[y] += S[i] + 1;
      if(l != (-1)) S[l] -= (S[i] + 1);
    }
    else {
      for(int s : graph[i][0]) S[s] += S[i] + 1;
      for(int s : graph[i][1]) S[s] += S[i] + 1;
    }
  }

  loop(i, 0, n-1) ++S_cnt[S[i]];

  ll res = 0;

  auto c = [&](int nn, int kk) -> ll {
    return mul(mul(fact[nn], fact_inv[nn - kk]), fact_inv[kk]);
  };

  auto f = [&](int S_val) -> ll {
    return mul(mul(c(n, S_val + 1), fact[S_val]), fact[n - S_val - 1]);
  };

  loop(S_val, 0, n-1) {
    res = add(res, mul(S_cnt[S_val], f(S_val)));
  }

  pint(mul(res, qpow(fact[n], MOD-2))), putchar_unlocked('\n');

}