#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#define F first
#define S second
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
template<class T> bool ckmin(T& a, T b) { return b < a ? a = b, true : false; }
template<class T> bool ckmax(T& a, T b) { return a < b ? a = b, true : false; }
const int N = 5e5 + 7;
const int M = 1e9 + 7;
int add(int a, int b) {
a += b;
if (a >= M) a -= M;
if (a < 0) a += M;
return a;
}
int mul(int a, int b) {
return (ll) a * b % M;
}
int mul(int a, int b, int c) {
return mul(a, mul(b, c));
}
int mul(int a, int b, int c, int d) {
return mul(a, mul(b, c, d));
}
int n, a[N], b[N], c[N];
int pref[N], suff[N];
int pw2[N];
ll f(int p, int x) {
ll ans = 0;
{
int ways = 1;
for (int i = 1; i < p; ++i) ways = mul(ways, (x >= a[i]) + (x >= b[i]));
suff[n+1] = 1;
for (int i = n; i >= 1; --i) suff[i] = mul(suff[i+1], (a[i] < x) + (b[i] < x));
int inter = 1;
for (int i = p + 1; i <= n; ++i) {
int s = add(pw2[n-i], -suff[i+1]);
if (x >= a[i]) ans = add(ans, mul(x - a[i], ways, s, inter));
if (x >= b[i]) ans = add(ans, mul(x - b[i], ways, s, inter));
inter = mul(inter, (x > a[i]) + (x > b[i]));
}
}
{
int ways = 1;
for (int i = n; i > p; --i) ways = mul(ways, (x >= a[i]) + (x >= b[i]));
pref[0] = 1;
for (int i = 1; i <= n; ++i) pref[i] = mul(pref[i - 1], (a[i] <= x) + (b[i] <= x));
int inter = 1;
for (int i = p - 1; i >= 1; --i) {
int s = add(pw2[i - 1], -pref[i - 1]);
if (x >= a[i]) ans = add(ans, mul(x - a[i], ways, s, inter));
if (x >= b[i]) ans = add(ans, mul(x - b[i], ways, s, inter));
inter = mul(inter, (x > a[i]) + (x > b[i]));
}
}
return ans;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i];
pw2[0] = 1;
for (int i = 1; i < N; ++i) pw2[i] = mul(pw2[i - 1], 2);
ll ans = 0;
for (int i = 1; i <= n; ++i) {
ans += f(i, a[i]); if (ans >= M) ans -= M;
ans += f(i, b[i]); if (ans >= M) ans -= M;
}
cout << ans << "\n";
}
# | 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... |