///*** Sown_Vipro ***///
/// ->TEAM SELECTION TEST<- ///
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i)
#define all(s) s.begin(), s.end()
#define szz(s) int(s.size())
const string NAME = "sown";
const int N = 1e6 + 5, MAX = 1e6, MOD = 1e9 + 7;
void maxi(int &x, int y){ if(x < y) x = y; }
void mini(int &x, int y){ if(x > y) x = y; };
void add(int &x, int y){ x += y; x += MOD * (x < 0); x -= MOD * (x >= MOD); };
void fixmod(int &x){
x += MOD * (x < 0); x -= MOD * (x >= MOD);
}
int n;
int a[N], b[N], c[N];
namespace sub1{
int l[N], r[N];
void solve(){
int res = 0;
FOR(mask, 0, (1 << n) - 1){
FOR(i, 1, n){
if(mask & (1 << i - 1)) c[i] = b[i];
else c[i] = a[i];
l[i] = max(l[i - 1], c[i]);
}
REP(i, n, 1){
r[i] = max(r[i + 1], c[i]);
// cout << l[i] << " " << r[i] << "\n";
add(res, min(l[i], r[i]) - c[i]);
}
// break;
}
cout << res;
}
}
namespace sub2{
int res;
int suf[2][1005][1005];
void calc(int i, int x, int t){
int pre = 1;
FOR(j, 1, i - 1){
pre = pre * ((a[j] <= x) + (b[j] <= x));
fixmod(pre);
}
FOR(j, i + 1, n - 1){
if(a[j] < x){
// cout << i << " " << j << " " << pre << " " << suf[t][i][j + 1] << " " << (x - a[j]) << "\n";
add(res, 1ll * pre * suf[t][i][j + 1] % MOD * (x - a[j]) % MOD);
}
if(b[j] < x){
// cout << i << " " << j << " " << pre << " " << suf[t][i][j + 1] << " " << (x - b[j]) << "\n";
add(res, 1ll * pre * suf[t][i][j + 1] % MOD * (x - b[j]) % MOD);
}
if(a[j] >= x && b[j] >= x) break;
}
}
void rcalc(int i, int x, int t){
int pre = 1;
FOR(j, 1, i - 1){
pre = pre * ((a[j] <= x) + (b[j] <= x));
fixmod(pre);
}
FOR(j, i + 1, n - 1){
if(a[j] < x){
// cout << x << " " << j << " " << a[j] << " " << suf[t][i][j + 1] << "\n";
add(res, 1ll * pre * suf[t][i][j + 1] % MOD * (x - a[j]) % MOD);
// add(res, 1ll * pre * suf[t][i][j + 1] % MOD * (x - b[j]) % MOD);
}
if(b[j] < x){
add(res, 1ll * pre * suf[t][i][j + 1] % MOD * (x - b[j]));
}
if(a[j] >= x && b[j] >= x) break;
}
}
void solve(){
FOR(i, 1, n){
REP(j, n, i + 1){
suf[0][i][j] = suf[0][i][j + 1];
int ans = 1;
FOR(k, i + 1, j - 1){
ans *= ((a[k] < a[i]) + (b[k] < a[i]));
fixmod(ans);
}
ans *= ((a[j] >= a[i]) + (b[j] >= a[i]));
fixmod(ans);
ans = 1ll * ans * (1 << (n - j)) % MOD;
add(suf[0][i][j], ans);
//
suf[1][i][j] = suf[1][i][j + 1];
ans = 1;
FOR(k, i + 1, j - 1){
ans *= ((a[k] < b[i]) + (b[k] < b[i]));
fixmod(ans);
}
ans *= ((a[j] >= b[i]) + (b[j] >= b[i]));
fixmod(ans);
ans = 1ll * ans * (1 << (n - j)) % MOD;
// cout << i << " " << j << " " << suf[1][i][j] << " " << ans << "\n";
add(suf[1][i][j], ans);
}
}
// cout << suf[1][1][3] << "\n";
FOR(i, 1, n){
calc(i, a[i], 0);
calc(i, b[i], 1);
}
reverse(a + 1, a + 1 + n);
reverse(b + 1, b + 1 + n);
FOR(i, 1, n){
REP(j, n, i + 1){
suf[0][i][j] = suf[0][i][j + 1];
int ans = 1;
FOR(k, i + 1, j - 1){
ans *= ((a[k] < a[i]) + (b[k] < a[i]));
fixmod(ans);
}
ans *= ((a[j] > a[i]) + (b[j] > a[i]));
fixmod(ans);
ans = 1ll * ans * (1 << (n - j)) % MOD;
add(suf[0][i][j], ans);
// cout << i << " " << j << " " << suf[0][i][j] << "\n";
//
suf[1][i][j] = suf[1][i][j + 1];
ans = 1;
FOR(k, i + 1, j - 1){
ans *= ((a[k] < b[i]) + (b[k] < b[i]));
fixmod(ans);
}
ans *= ((a[j] > b[i]) + (b[j] > b[i]));
fixmod(ans);
ans = 1ll * ans * (1 << (n - j)) % MOD;
add(suf[1][i][j], ans);
// cout << ans << "\n";
// break;
}
// break;
}
// cout << suf[1][1][3] << "\n";
FOR(i, 1, n){
rcalc(i, a[i], 0);
rcalc(i, b[i], 1);
// break;
}
cout << res;
}
}
void solve(){
cin >> n;
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, n) cin >> b[i];
// sub2::solve();
if(n <= 20) sub1::solve();
else sub2::solve();
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
if(fopen((NAME + ".inp").c_str(), "r")){
freopen((NAME + ".inp").c_str(), "r", stdin);
// freopen((NAME + ".out").c_str(), "w", stdout);
}
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:207:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
207 | freopen((NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |