#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e17;
using ll = long long;
const ll MOD = 1e9 + 7;
inline ll add(ll a, ll b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
inline ll mul(ll a, ll b) {
return (a * b) % MOD;
}
inline ll dif(ll a, ll b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
ll binpow(ll a, ll b) {
ll res = 1;
a %= MOD;
while (b > 0) {
if (b & 1) res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
inline ll mod_inv(ll a) {
return binpow(a, MOD - 2);
}
ll v[501][501];
ll fact[501], invfact[501];
ll ncr[501][501];
ll ncr_i[501][501];
ll dp[501][501];
inline ll perm_range(ll a, ll b) {
ll x = 1;
for (ll i = a + 1; i <= b; ++i) x = mul(x, i);
return x;
}
void precompute_factorials(int n) {
fact[0] = invfact[0] = 1;
for (int i = 1; i <= n; ++i) {
fact[i] = mul(fact[i - 1], i);
invfact[i] = mod_inv(fact[i]);
}
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= i; ++j) {
ncr[i][j] = mul(fact[i], mul(invfact[j], invfact[i - j]));
}
}
}
ll f(int n, ll A[], ll B[]) {
precompute_factorials(n);
vector<ll> vp;
for (int i = 0; i < n; ++i) {
vp.push_back(A[i]);
vp.push_back(B[i]);
}
sort(vp.begin(), vp.end());
vp.erase(unique(vp.begin(), vp.end()), vp.end());
vector<ll> l, r;
for (size_t i = 0; i + 1 < vp.size(); ++i) {
l.push_back(vp[i]); r.push_back(vp[i]);
if (vp[i] + 1 <= vp[i + 1] - 1) {
l.push_back(vp[i] + 1);
r.push_back(vp[i + 1] - 1);
}
}
l.push_back(vp.back());
r.push_back(vp.back());
int m = l.size();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
v[i][j] = (A[i] <= l[j] && r[j] <= B[i]);
}
}
for (int j = 0; j <= n; ++j) {
for (int i = 0; i < m; ++i) {
if (j > r[i] - l[i] + 1) {
ncr_i[i][j] = 0;
} else {
ll len = r[i] - l[i] + 1;
ll x = perm_range(len - j, len);
ncr_i[i][j] = mul(x, invfact[j]);
}
}
}
static ll x[501][501];
for (int j = 0; j < m; ++j) {
for (int i = 0; i <= n; ++i) {
x[i][0] = r[j] - l[j] + 1;
for (int g = 1; g <= n; ++g) {
x[i][g] = add(x[i][g - 1], mul(ncr[i][g], ncr_i[j][g + 1]));
}
}
dp[0][j] = v[0][j] ? r[j] - l[j] + 1 : 0;
if (j > 0) dp[0][j] = add(dp[0][j], dp[0][j - 1]);
for (int i = 1; i < n; ++i) {
int c = 0;
dp[i][j] = 0;
if (v[i][j]) {
for (int g = i - 1; g >= 0; --g) {
if (j > 0) {
dp[i][j] = add(dp[i][j], mul(dp[g][j - 1], x[c][c]));
}
if (v[g][j]) ++c;
}
dp[i][j] = add(dp[i][j], x[c][c]);
}
if (j > 0) dp[i][j] = add(dp[i][j], dp[i][j - 1]);
}
}
ll ans = 0;
for (int i = 0; i < n; ++i) ans = add(ans, dp[i][m - 1]);
return ans;
}
void solve() {
int n;
cin >> n;
ll A[501], B[501];
for (int i = 0; i < n; ++i) cin >> A[i] >> B[i];
cout << f(n, A, B) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |