#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define REP(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
#define Bit(x, i) ((x >> (i)) & 1)
#define Mask(i) (1 << (i))
#define Cnt(x) __builtin_popcount(x)
#define Cntll(x) __builtin_popcountll(x)
#define Ctz(x) __builtin_ctz(x) // so luong so 0 tinh tu ben phai
#define Ctzll(x) __builtin_ctzll(x)
#define Clz(x) __builtin_clz(x) // so luong so 0 tinh tu ben trai
#define Clzll(x) __builtin_clzll(x)
inline bool maximize(int &u, int v){
    return v > u ? u = v, true : false;
}
inline bool minimize(int &u, int v){
    return v < u ? u = v, true : false;
}
inline bool maximizell(long long &u, long long v){
    return v > u ? u = v, true : false;
}
inline bool minimizell(long long &u, long long v){
    return v < u ? u = v, true : false;
}
const int mod = 1e9 + 7;
inline int fastPow(int a, int n){
    if(n == 0) return 1;
    int t = fastPow(a, n >> 1);
    t = 1ll * t * t % mod;
    if(n & 1) t = 1ll * t * a % mod;
    return t;
}
inline void add(int &u, int v){
    u += v;
    if(u >= mod) u -= mod;
}
inline void sub(int &u, int v){
    u -= v;
    if(u < 0) u += mod;
}
const int maxN = 2e5 + 5;
const int inf = 1e9;
const long long infll = 1e18;
struct Node{
    int Left, Right, len, answer;
    Node operator + (const Node &rhs) const{
        Node res;
        res.answer = answer + rhs.answer;
        if(res.answer >= mod) res.answer -= mod;
        if(Right && rhs.Left){
            add(res.answer, 1ll * len * rhs.len % mod);
        }
        res.Left = Left;
        res.Right = rhs.Right;
        res.len = len + rhs.len;
        return res;
    }
}it[maxN << 2];
int w[maxN], h[maxN];
void build(int id, int l, int r){
    if(l == r){
        it[id] = {1, 1, w[l], (1ll * w[l] * (w[l] - 1) / 2 + w[l]) % mod};
        return;
    }
    int mid = l + r >> 1;
    build(id << 1, l, mid);
    build(id << 1 | 1, mid + 1, r);
    it[id] = it[id << 1] + it[id << 1 | 1];
}
void update(int id, int l, int r, int p){
    if(l == r){
        it[id] = {0, 0, 0, 0};
        return;
    }
    int mid = l + r >> 1;
    if(p <= mid) update(id << 1, l, mid, p);
    else update(id << 1 | 1, mid + 1, r, p);
    it[id] = it[id << 1] + it[id << 1 | 1];
}
int n;
vector<int> compress;
vector<int> query[maxN];
void process(){
    cin >> n;
    FOR(i, 1, n){
        cin >> h[i];
        compress.emplace_back(h[i]);
    }
    FOR(i, 1, n){
        cin >> w[i];
    }
    build(1, 1, n);
    sort(all(compress));
    uni(compress);
    int pre = 0;
    int answer = 0;
    FOR(i, 1, n){
        h[i] = lower_bound(all(compress), h[i]) - compress.begin();
        query[h[i]].emplace_back(i);
    }
    FOR(i, 0, compress.size() - 1){
        int valh = compress[i] - pre;
        valh = (1ll * valh * pre + 1ll * valh * (valh - 1) / 2 + valh) % mod;
        add(answer, 1ll * valh * it[1].answer % mod);
        for(int p : query[i])update(1, 1, n, p);
        pre = compress[i];
    }
    cout << answer;
}
#define LOVE ""
int main(){
    if(fopen(LOVE".inp", "r")){
        freopen(LOVE".inp", "r", stdin);
        freopen(LOVE".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin >> t;
    while(t--)
        process();
//    cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ;
    return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
fancyfence.cpp: In function 'void build(int, int, int)':
fancyfence.cpp:67:68: warning: narrowing conversion of '(((((1 * ((long long int)w[l])) * ((long long int)(w[l] - 1))) / 2) + ((long long int)w[l])) % ((long long int)((int)mod)))' from 'long long int' to 'int' [-Wnarrowing]
   67 |         it[id] = {1, 1, w[l], (1ll * w[l] * (w[l] - 1) / 2 + w[l]) % mod};
      |                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         freopen(LOVE".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  119 |         freopen(LOVE".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... |