///*** 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); };
int n;
int h[N], w[N], s[N];
void solve(){
cin >> n;
int checksub3 = 1;
FOR(i, 1, n){
cin >> h[i];
if(i == 2 && h[i] != h[i - 1]) checksub3 = 0;
}
FOR(i, 1, n){
cin >> w[i];
s[i] = (s[i - 1] + w[i]) % MOD;
}
if(checksub3){
int A = 1ll * h[1] * (h[1] + 1) % MOD * 500000004 % MOD;
int B = 1ll * s[n] * (s[n] + 1) % MOD * 500000004 % MOD;
cout << (1ll * A * B) % MOD;
return;
}
vector<int> st;
st.pb(0);
int res = 0;
FOR(i, 1, n){
while(h[st.back()] >= h[i]){
st.pop_back();
}
int j = szz(st) - 1, last = i - 1;
int A = 1ll * h[i] * (h[i] + 1) % MOD * 500000004 % MOD;
int B = 1ll * w[i] * (w[i] + 1) % MOD * 500000004 % MOD;
res = (res + 1ll * A * B) % MOD;
REP(H, h[i], 1){
while(h[st[j]] >= H) --j;
long long W = s[last] - s[st[j]];
int A = 1ll * H * (H + 1) % MOD * 500000004 % MOD;
int B = 1ll * w[i] * W % MOD;
res = (res + 1ll * A * B) % MOD;
last = st[j];
}
st.pb(i);
}
cout << res;
}
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)
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen((NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen((NAME + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |