#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define ve vector
#define all(x) x.begin(), x.end()
#define fo(i,a,b) for (int i=(a),_b=(b); i<=_b; ++i)
#define fd(i,a,b) for (int i=(a),_b=(b); i>=_b; --i)
#define maxi(a, b) a = max(a, b)
#define mini(a, b) a = min(a, b)
#define _ << ' ' << 
const int N = 1e5+5, inf = 1e9+10, mod = 1e9+7;
/*
*/
int n, a[N], b[N];
ll s[N];
inline ll C2(ll x) {
    return (x * (x - 1) / 2) % mod;
}
namespace sub1 {
    void solve() {
        ll ans = 0;
        fo(i,1,n) {
            int mn = a[i];
            fo(j,i+1,n) {
                mini(mn, a[j]);
                ans = (ans + C2(mn + 1) * b[i] % mod * b[j] % mod) % mod;
            }
        }
        fo(i,1,n) ans = (ans + C2(a[i] + 1) * C2(b[i] + 1)) % mod;
        cout << ans;
    }
}
namespace full {
    int fa[N], l[N], r[N];
    ll ans;
    int par(int u) {
        return (u == fa[u] ? u : fa[u] = par(fa[u]));
    }
    void join(int u, int v, int h) {
        u = par(u), v = par(v);
        if (u == v) return;
        ans = (ans + C2(h + 1) * (s[r[v]] - s[l[v]-1]) % mod * (s[r[u]] - s[l[u]-1])) % mod;
        fa[u] = v, l[v] = l[u], r[v] = r[u];
    }
    void solve() {
        fo(i,1,n) s[i]=s[i-1]+b[i], fa[i]=l[i]=r[i]=i;
        ve<int> id(n); iota(all(id), 1);
        sort(all(id), [&](int x, int y) -> bool {
            return a[x] > a[y];
        });
        fo(i,0,n-1) {
            int x = id[i];
            if (x>1&&a[x-1]>a[x]) join(x-1, x, a[x]);
            if (x<n&&a[x+1]>a[x]) join(x, x+1, a[x]);
        }
        fo(i,1,n) ans = (ans + C2(a[i] + 1) * C2(b[i] + 1)) % mod;
        cout << ans;
    }
}
void sol() {
    cin >> n;
    fo(i,1,n) cin >> a[i];
    fo(i,1,n) cin >> b[i];
    if (n <= 1000) return sub1::solve();
    return full::solve();
}
signed main(){
    ios::sync_with_stdio(0); cin.tie(0);
    if(fopen("A.inp","r")) {
        freopen("A.inp","r",stdin);
        freopen("A.out","w",stdout);
    }
    int tc = 1; // cin >> tc;
    fo(i,1,tc) sol();
    return 0;
}
Compilation message (stderr)
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |         freopen("A.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~
fancyfence.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         freopen("A.out","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... |