///*** 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())
#define int long long
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;
FOR(i, 1, n){
cin >> h[i];
}
FOR(i, 1, n){
cin >> w[i];
s[i] = (s[i - 1] + w[i]) % MOD;
}
vector<int> st;
st.pb(0);
int res = 0, f = 0;
FOR(i, 1, n){
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) % MOD;
while(h[st.back()] >= h[i]){
int j = st.back(); st.pop_back();
int A = 1ll * h[j] * (h[j] + 1) % MOD * 500000004 % MOD;
int W = (s[j] - s[st.back()] + MOD) % MOD;
f = (f - 1ll * A * W % MOD + MOD) % MOD;
}
// cout << f << " " << w[i] << "\n";
res = (res + 1ll * f * w[i] % MOD) % MOD;
int W = (s[i - 1] - s[st.back()] + MOD) % MOD;
res = (res + 1ll * A * W % MOD * w[i] % MOD) % MOD;
W = (s[i] - s[st.back()] + MOD) % MOD;
f = (f + 1ll * A * W % MOD) % MOD;
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:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen((NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | 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... |