// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/*using ll = __int128;*/
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
/*const ll M = 998244353;*/
const ll M = 1e9 + 7;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define ff first
#define ss second
#define PI 3.141592653589793238462643383279502884L
const ll INFL = 1e18;
const int INF = 1e9;
const int maxN = 101+1;
ll suf[10002][1002], suf_sum[10002][1002];
ll pref[10002][1002], pref_sum[10002][1002];
ll a[10001], b[10001];
void solve() {
int n;
cin >> n;
for (int i=1; i<=n; i++) cin >> a[i];
for (int i=1; i<=n; i++) cin >> b[i];
pref[0][0] = 1;
for (int i=1; i<=n; i++) {
for (auto v: {a[i], b[i]}) {
for (int j=v+1; j<=1000; j++) {
pref[i][j] += pref[i-1][j];
pref[i][j] %= M;
}
for (int j=0; j<=v; j++) {
pref[i][v] += pref[i-1][j];
pref[i][j] %= M;
}
}
for (int j=1000; j>=0; j--) {
pref_sum[i][j] = pref_sum[i][j+1] + pref[i][j];
pref_sum[i][j] %= M;
}
}
suf[n+1][0] = 1;
for (int i=n; i>=1; i--) {
for (auto v: {a[i], b[i]}) {
for (int j=v+1; j<=1000; j++) {
suf[i][j] += suf[i+1][j];
suf[i][j] %= M;
}
for (int j=0; j<=v; j++) {
suf[i][v] += suf[i+1][j];
suf[i][j] %= M;
}
}
for (int j=1000; j>=0; j--) {
suf_sum[i][j] = suf_sum[i][j+1] + suf[i][j];
suf_sum[i][j] %= M;
}
}
ll ans = 0;
for (int i=1; i<=n; i++) {
for (auto v: {a[i], b[i]}) {
for (int j=v+1; j<=1000; j++) {
ans += pref[i-1][j]*suf_sum[i+1][j+1]%M*(j-v);
ans += suf[i+1][j]*pref_sum[i-1][j+1]%M*(j-v);
ans += pref[i-1][j]*suf[i+1][j]%M*(j-v);
ans %= M;
}
}
}
cout << ans << '\n';
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout << fixed << setprecision(12);
int tc = 1;
/*cin >> tc;*/
while (tc--) solve();
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |