#include #include #include using namespace std; const int MOD = 1000000007; struct mint { int n; mint(int n_ = 0) : n(n_) {} friend mint operator-(mint a) { return -a.n + MOD * (a.n != 0); } friend mint operator+(mint a, mint b) { int x = a.n + b.n; return x - (x >= MOD) * MOD; } friend mint operator-(mint a, mint b) { int x = a.n - b.n; return x + (x < 0) * MOD; } friend mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; } friend mint &operator+=(mint &a, mint b) { return a = a + b; } friend mint &operator-=(mint &a, mint b) { return a = a - b; } friend mint &operator*=(mint &a, mint b) { return a = a * b; } friend bool operator==(mint a, mint b) { return a.n == b.n; } friend bool operator!=(mint a, mint b) { return a.n != b.n; } friend istream &operator>>(istream &i, mint &a) { return i >> a.n; } friend ostream &operator<<(ostream &o, mint a) { return o << a.n; } }; mint modinv(mint n) { int a = n.n; int b = MOD; int s = 1; int t = 0; while (b != 0) { int q = a / b; a -= q * b; s -= q * t; swap(a, b); swap(s, t); } if (s < 0) s += MOD; return s; } vector F_{1, 1}, R_{1, 1}, I_{0, 1}; void check_fact(int n) { for (int i = I_.size(); i <= n; i++) { I_.push_back(I_[MOD % i] * (MOD - MOD / i)); F_.push_back(F_[i - 1] * i); R_.push_back(R_[i - 1] * I_[i]); } } mint I(int n) { check_fact(abs(n)); return n >= 0 ? I_[n] : -I_[-n]; } mint F(int n) { check_fact(n); return n < 0 ? 0 : F_[n]; } mint R(int n) { check_fact(n); return n < 0 ? 0 : R_[n]; } mint C(int n, int r) { return F(n) * R(n - r) * R(r); } mint P(int n, int r) { return F(n) * R(n - r); } mint H(int n, int r) { return n == 0 ? (r == 0) : C(n + r - 1, r); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector a(n), b(n), c; vector inv(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; c.push_back(a[i]); c.push_back(b[i]); inv[i] = modinv(b[i] - a[i]); } c.push_back((int)1e9 + 1); c.push_back((int)1e9 + 2); sort(c.begin(), c.end()); c.erase(unique(c.begin(), c.end()), c.end()); const int m = c.size() - 1; vector f(n+1); f[0] = 1; f[1] = 1; for (int i = 2; i <= n; i++) { for (int j = 1; j <= i-1; j += 2) { f[i] += f[j] * f[i-1-j] * C(i-1, j); } } for (int i = 2; i <= n; i++) { f[i] *= R(i); } vector> dp(n+1, vector(m)); auto index = [&](int x) -> int { return lower_bound(c.begin(), c.end(), x) - c.begin(); }; for (int i = 0; i < n; i++) { a[i] = index(a[i]); b[i] = index(b[i]); } dp[0][m-1] = 1; for (int i = 0; i < n; i++) { if (i % 2 == 0) { mint s = 0; for (int j = m-1; j >= 0; j--) { mint ns = s + dp[i][j]; dp[i][j] = s; s = ns; } } else { mint s = 0; for (int j = 0; j < m; j++) { mint ns = s + dp[i][j]; dp[i][j] = s; s = ns; } } for (int j = 0; j < m; j++) { mint p = 1; for (int k = i; k < n; k++) { if (a[k] <= j && j < b[k]) { p *= (c[j+1]-c[j])*inv[k]; } else break; dp[k+1][j] += dp[i][j] * p * f[k-i+1]; } } } mint ans = 0; for (int j = 0; j < m; j++) { ans += dp[n][j]; } cout << ans << endl; }