This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <int,pii> piii;
#define forr(_a,_b,_c) for(int _a = (_b); _a <= int (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> int (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < int (_c); ++_a)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
template<class X, class Y>
bool minz(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} return false;
}
template<class X, class Y>
bool maxz(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} return false;
}
const int N = 5e5 + 5;
const ll oo = (ll) 1e16;
const ll mod = 1e9 + 7; // 998244353;
int n;
ll a[N], b[N], bl[N], br[N], al[N], ar[N], res, fa[N], fb[N], za[N], zb[N], pw[N];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef kaguya
freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
#endif
cin >> n;
forr (i, 1, n){
cin >> a[i];
}
forr (i, 1, n){
cin >> b[i];
}
pw[0] = 1;
forr (i, 1, n){
pw[i] = pw[i - 1] * 2 % mod;
}
forr (i, 1, n){
al[i] = ar[i] = bl[i] = br[i] = 1;
ll tmp = 1;
forf (j, 1, i){
tmp = tmp * 2 % mod;
int cnt = (a[j] <= a[i]) + (b[j] <= a[i]);
al[i] = al[i] * cnt % mod;
cnt = (a[j] <= b[i]) + (b[j] <= b[i]);
bl[i] = bl[i] * cnt % mod;
}
forr (j, i + 1, n){
int cnt = (a[j] <= a[i]) + (b[j] <= a[i]);
ar[i] = ar[i] * cnt % mod;
cnt = (a[j] <= b[i]) + (b[j] <= b[i]);
br[i] = br[i] * cnt % mod;
}
}
forr (i, 1, n){
ll way = 1, sum = 0;
forr (j, i + 1, n){
if (b[j] > a[i]){
ll tmp = (a[i] * way % mod * (j - i - 1) - sum + mod) % mod;
(res += al[i] * pw[n - j] % mod * tmp % mod) %= mod;
} else if (b[j] == a[i]){
ll tmp = (a[i] * way % mod * (j - i - 1) - sum + mod) % mod;
(res += (pw[i - 1 + n - j] - (pw[i - 1] - al[i] + mod) * (pw[n - j] - br[j] + mod) % mod + mod) % mod * tmp % mod) %= mod;
}
if (a[j] > a[i]){
ll tmp = (a[i] * (way) % mod * (j - i - 1) - sum + mod) % mod;
(res += al[i] * pw[n - j] % mod * tmp % mod) %= mod;
} else if (a[i] == a[j]){
ll tmp = (a[i] * way % mod * (j - i - 1) - sum + mod) % mod;
(res += (pw[i - 1 + n - j] - (pw[i - 1] - al[i] + mod) * (pw[n - j] - ar[j] + mod) % mod + mod) % mod * tmp % mod) %= mod;
}
if (a[j] < a[i] && b[j] < a[i]){
sum = (sum * 2 + (a[j] + b[j]) * (way)) % mod;
way = way * 2 % mod;
} else if (a[j] < a[i]){
sum = (sum + a[j] * (way)) % mod;
way = way % mod;
} else if (b[j] < a[i]){
sum = (sum + b[j] * (way)) % mod;
way = way % mod;
} else {
break;
}
}
way = 1, sum = 0;
forr (j, i + 1, n){
if (b[j] > b[i]){
ll tmp = (b[i] * way % mod * (j - i - 1) - sum + mod) % mod;
(res += bl[i] * pw[n - j] % mod * tmp % mod) %= mod;
} else if (b[j] == b[i]){
ll tmp = (b[i] * way % mod * (j - i - 1) - sum + mod) % mod;
(res += (pw[i - 1 + n - j] - (pw[i - 1] - bl[i] + mod) * (pw[n - j] - br[j] + mod) % mod + mod) % mod * tmp % mod) %= mod;
}
if (a[j] > b[i]){
ll tmp = (b[i] * (way) % mod * (j - i - 1) - sum + mod) % mod;
(res += bl[i] * pw[n - j] % mod * tmp % mod) %= mod;
} else if (b[i] == a[j]){
ll tmp = (b[i] * way % mod * (j - i - 1) - sum + mod) % mod;
(res += (pw[i - 1 + n - j] - (pw[i - 1] - bl[i] + mod) * (pw[n - j] - ar[j] + mod) % mod + mod) % mod * tmp % mod) %= mod;
}
if (a[j] < b[i] && b[j] < b[i]){
sum = (sum * 2 + (a[j] + b[j]) * (way)) % mod;
way = way * 2 % mod;
} else if (a[j] < b[i]){
sum = (sum + a[j] * (way)) % mod;
way = way % mod;
} else if (b[j] < b[i]){
sum = (sum + b[j] * (way)) % mod;
way = way % mod;
} else {
break;
}
}
// cout << res << "\n";
}
reverse(a + 1, a + 1 + n);
reverse(b + 1, b + 1 + n);
forr (i, 1, n){
al[i] = ar[i] = bl[i] = br[i] = 1;
forf (j, 1, i){
int cnt = (a[j] <= a[i]) + (b[j] <= a[i]);
al[i] = al[i] * cnt % mod;
cnt = (a[j] <= b[i]) + (b[j] <= b[i]);
bl[i] = bl[i] * cnt % mod;
}
forr (j, i + 1, n){
ar[i] = ar[i] * 2 % mod;
br[i] = br[i] * 2 % mod;
}
}
forr (i, 1, n){
ll way = 1, sum = 0;
forr (j, i + 1, n){
// if (i == 2){
// cout << way << " " << sum << " " << b[j] << "\n";
// }
if (b[j] > a[i]){
ll tmp = (a[i] * way % mod * (j - i - 1) - sum + mod) % mod;
(res += al[i] * ar[j] % mod * tmp % mod) %= mod;
}
if (a[j] > a[i]){
ll tmp = (a[i] * (way) % mod * (j - i - 1) - sum + mod) % mod;
(res += al[i] * ar[j] % mod * tmp % mod) %= mod;
}
if (a[j] < a[i] && b[j] < a[i]){
sum = (sum * 2 + (a[j] + b[j]) * (way)) % mod;
way = way * 2 % mod;
} else if (a[j] < a[i]){
sum = (sum + a[j] * (way)) % mod;
way = way % mod;
} else if (b[j] < a[i]){
sum = (sum + b[j] * (way)) % mod;
way = way % mod;
} else {
break;
}
}
//cout << res << "\n";
way = 1, sum = 0;
forr (j, i + 1, n){
if (b[j] > b[i]){
ll tmp = (b[i] * way % mod * (j - i - 1) - sum + mod) % mod;
(res += bl[i] * br[j] % mod * tmp % mod) %= mod;
}
if (a[j] > b[i]){
ll tmp = (b[i] * (way) % mod * (j - i - 1) - sum + mod) % mod;
(res += bl[i] * br[j] % mod * tmp % mod) %= mod;
}
if (a[j] < b[i] && b[j] < b[i]){
sum = (sum * 2 + (a[j] + b[j]) * (way)) % mod;
way = way * 2 % mod;
} else if (a[j] < b[i]){
sum = (sum + a[j] * (way)) % mod;
way = way % mod;
} else if (b[j] < b[i]){
sum = (sum + b[j] * (way)) % mod;
way = way % mod;
} else {
break;
}
}
}
cout << res << "\n";
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |