#include <bits/stdc++.h>
#define fi first
#define se second
#define FOR(i,l,r) for (int i = l; i <= r; i++)
#define FORD(i,l,r) for (int i = l; i >= r; i--)
#define el cout <<"\n"
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define int long long
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&(1LL))
#define Debug(a,n) for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> vii;
typedef unsigned long long ull;
typedef vector<vector<int>> vvi;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
const int N=1e5+5;
const int base=311;
const int mod=1e9+7;
const long long INF=1e18+7;
const int d4x[4] = {-1, 0, 1, 0} , d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8y[8] = {0, 1, 1, 1, 0, -1, -1, -1};
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
void add(int &x, int y) { x += y; if (x>=mod) x-=mod;}
void sub(int &x, int y) { x -= y; if (x<0) x+=mod;}
int mul(int x, int y) {return 1LL * x * y % mod;}
int calPw(int x, int y)
{
    int ans = 1;
    while(y)
    {
        if (y&1) ans = 1LL * ans * x % mod;
        x = 1LL * x * x % mod;
        y >>= 1;
    }
    return ans;
}
///KhengmepfromLTVHSFTG
int n,h[N],w[N],pre[N],dp[N];
ll Get(int i){
    return (pre[n]-pre[i-1]+mod)%mod;
}
void sub34(){
    for(int i=1;i<=n;i++){
        add(pre[i],pre[i-1]);
        add(pre[i],w[i]);
    }
    ll div2=calPw(2,mod-2);
    ll Ans=0;
    for(int i=1;i<=n;i++){
        ll tmp1=mul(2,Get(i));
        sub(tmp1,w[i]);
        add(tmp1,1);
        tmp1=mul(tmp1,w[i]);
        tmp1=mul(tmp1,div2);
        ll tmp2=mul(h[i],h[i]+1);
        tmp2=mul(tmp2,div2);
        add(Ans,mul(tmp1,tmp2));
    }
    cout<<Ans;
    el;
}
void sub15(){
    ll Ans=0;
    ll div2=calPw(2,mod-2);
    for(int i=1;i<=n;i++){
        int mi=h[i];
        ll tmp=mul(mi,mi+1);
        tmp=mul(tmp,div2);
        tmp=mul(tmp,w[i]);
        tmp=mul(tmp,w[i]+1);
        tmp=mul(tmp,div2);
        add(Ans,tmp);
        for(int j=i+1;j<=n;j++){
            mi=min(mi,h[j]);
            ll tmp2=mul(mi,mi+1);
            tmp2=mul(tmp2,div2);
            tmp2=mul(tmp2,w[j]);
            tmp2=mul(tmp2,w[i]);
            add(Ans,tmp2);
        }
    }
    cout<<Ans;
    el;
}
void sub2(){
    ll sum=0;
    for(int i=1;i<=n;i++){
        dp[i]=0;
        add(sum,w[i]);
    }
    for(int i=1;i<=n;i++){
        if(h[i]==2) dp[i]=(dp[i-1]+w[i])%mod;
    }
    ll Ans=0;
    for(int i=1;i<=n;i++){
        if(h[i]==2&&(h[i+1]==1||i==n)){
            add(Ans,mul(dp[i],dp[i]+1));
        }
    }
    add(Ans,mul(calPw(2,mod-2),mul(sum,sum+1)));
    cout<<Ans;
    el;
}
void khengmep()
{
    bool checksub34=true;
    for(int i=1;i<n;i++){
        if(h[i]>h[i+1]) checksub34=false;
    }
    if(checksub34==true){
        sub34();
//        el;
//        cout<<"34";
        return;
    }
    if(n<=1000){
        sub15();
//        el;
//        cout<<"15";
        return;
    }
    sub2();
}
void ip()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>h[i];
    for(int i=1;i<=n;i++) cin>>w[i];
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--)
    {
        ip();
        khengmep();
    }
    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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |