#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native,sse,sse2,sse3")
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define F "C"
#define fio freopen(F".INP","r",stdin);freopen(F".OUT","w",stdout);
#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif
using namespace std;
const int N=1e6+3,MOD=1e9+7,INV=500000004;
ll prw[N];
ll binpow(ll x,ll y) {
if(y == MOD - 2 && x <= 1000000) return prw[x];
x%=MOD;
ll res=1;
while(y>0) {
if (y%2) res=res*x%MOD;
x=x*x%MOD;
y/=2;
}
return res;
}
int n,a[N],b[N],c[N],d[N],e[N][2],lft[N][2],rgt[N][2];
int f[N],mi[N],ma[N];
ll pw[N*2],pf[N],pf1[N];
vector<pair<int,int>> in[N*2];
vector<ll> big;
ll ans=0;
struct SegTree {
ll t[2 * N];
void reset() {
for (int i = n - 1; i > 0; --i) t[i] = 0;
}
void update(int p, ll value) {
p--;
for (t[p += n] = value; p > 1; p >>= 1) t[p>>1] = t[p] * t[p^1]%MOD;
}
int query(int l, int r) {
l--;
ll res = 1;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) res = res*t[l++]%MOD;
if (r&1) res = res*t[--r]%MOD;
}
return res;
}
} st;
struct SegTree1 {
struct Node {
ll mul=0,x=0,x1=0;
Node(ll mul=1,ll x=0,ll x1=0): mul(mul),x(x),x1(x1) {};
} tr[N*4];
Node mrg(const Node A,const Node B) {
Node res;
res.mul=A.mul*B.mul%MOD;
res.x=(B.x+A.x*B.mul%MOD)%MOD;
res.x1=(B.x1+A.x1*B.mul%MOD)%MOD;
return res;
}
void reset() {
for (int i = n*2; i > 0; --i) tr[i] = Node(1,0,0);
}
void update(int p) {
p--;
p+=n;
for (tr[p] = Node(c[p-n+1],d[p-n+1]*c[p-n+1]*(p-n+2)%MOD*pw[p-n]%MOD,d[p-n+1]*c[p-n+1]*pw[p-n]%MOD); p > 1; p >>= 1) tr[p>>1] = mrg(tr[p],tr[p^1]);
}
Node query(int l, int r) {
l--;
Node resL = Node(1,0,0),resR = Node(1,0,0);
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) resL = mrg(resL,tr[l++]);
if (r&1) resR = mrg(tr[--r],resR);
}
return mrg(resL,resR);
}
} st1;
struct BIT {
ll pf[N];
ll query(int p) {
int idx=p;
ll ans=0;
while(idx>0) {
ans=ans+pf[idx];
idx-=(idx&(-idx));
}
return ans%MOD;
}
void update(int u,ll v) {
int idx=u;
while(idx<=n) {
pf[idx]=pf[idx]+v;
idx+=(idx&(-idx));
}
}
} bit1,bit;
int find_set(int u) {
return (f[u]<0?u:f[u]=find_set(f[u]));
}
void unite(int u,int v) {
int a=find_set(u),b=find_set(v);
if (a==b) return;
if (f[a]>f[b]) swap(a,b);
f[a]+=f[b];
f[b]=a;
mi[a]=min(mi[a],mi[b]);
ma[a]=max(ma[a],ma[b]);
}
void solve(bool lmao=0) {
For(i,1,n) bit1.pf[i]=bit.pf[i]=0;
For(i,1,n) in[lower_bound(all(big),a[i])-big.begin()+1].pb({i,0});
For(i,1,n) in[lower_bound(all(big),b[i])-big.begin()+1].pb({i,1});
fill(c,c+1+n,0);
st1.reset();
if (!lmao) {
st.reset();
For(i,1,sz(big)+1) {
for(auto el: in[i-1]) {
c[el.ff]++;
st.update(el.ff,c[el.ff]);
}
for(auto el: in[i-1]) {
ll x=st.query(1,el.ff-1),y=st.query(el.ff+1,n);
if (el.ff>1 && el.ff<n) {
ans=(ans+1LL*(e[el.ff][el.ss])*x%MOD*pw[n-el.ff]%MOD)%MOD;
ans=(ans+1LL*(e[el.ff][el.ss])*y%MOD*pw[el.ff-1]%MOD)%MOD;
ans=(ans-1LL*(e[el.ff][el.ss])*x%MOD*y%MOD)%MOD;
}
else ans=(ans+1LL*(e[el.ff][el.ss])*pw[n-1]%MOD)%MOD;
if (el.ff>1) lft[el.ff][el.ss]=x;
else lft[el.ff][el.ss]=1;
if (el.ff<n) rgt[el.ff][el.ss]=y;
else rgt[el.ff][el.ss]=1;
}
}
} else {
For(i,1,n)
For(k,0,1) swap(lft[i][k],rgt[n-i+1][k]);
}
For(i,1,n) f[i]=-1,mi[i]=ma[i]=i,c[i]=0,d[i]=2;
st.reset();
For(i,1,sz(big)) {
for(auto el: in[i]) {
d[el.ff]--;
st1.update(el.ff);
}
for(auto el: in[i-1]) {
c[el.ff]++;
st.update(el.ff,c[el.ff]);
st1.update(el.ff);
if (c[el.ff]==1) {
ll tmp=pw[el.ff-1]*binpow(c[el.ff],MOD-2)%MOD*d[el.ff]%MOD;
if (c[el.ff-1]>0) {
unite(el.ff,el.ff-1);
}
if (c[el.ff+1]>0) {
unite(el.ff,el.ff+1);
}
}
}
for(auto el: in[i]) {
if (c[el.ff-1]>0) {
if (mi[find_set(el.ff-1)]<el.ff-1) {
auto mmb=st1.query(1,el.ff-1);
ll x=mmb.x%MOD,x1=mmb.x1%MOD;
ll tmp=(1LL*x1*el.ff%MOD-x)%MOD;
ans=(ans+big[i-1]*rgt[el.ff][el.ss]%MOD*tmp%MOD);
}
if (mi[find_set(el.ff-1)]>1) {
int j=mi[find_set(el.ff-1)]-1;
ll tmp=st.query(mi[find_set(el.ff-1)],el.ff-1)%MOD;
ll cnst=big[i-1]*tmp%MOD*(el.ff-j-1)%MOD;
For(k,0,1) {
if (e[j][k]>big[i-1]) ans=(ans+big[i-1]*tmp%MOD*pw[j-1]%MOD*rgt[el.ff][el.ss]%MOD*(el.ff-j-1)%MOD)%MOD;
else if (e[j][k]==big[i-1] && !lmao) {
ans=(ans+cnst*(pw[j-1]-lft[j][k])%MOD*rgt[el.ff][el.ss]%MOD)%MOD;
ans=(ans+cnst*lft[j][k]%MOD*pw[n-el.ff]%MOD)%MOD;
}
}
}
}
}
if (!lmao) {
for(auto el: in[i]) {
ll tmp=(pw[el.ff-1]-lft[el.ff][el.ss])%MOD*big[i-1]%MOD;
if (c[el.ff]>0) tmp=tmp*binpow(st.query(mi[find_set(el.ff)],el.ff)%MOD,MOD-2)%MOD;
bit.update(el.ff,tmp);
bit1.update(el.ff,tmp*(el.ff+1)%MOD);
pf[el.ff]=(pf[el.ff]+tmp)%MOD;
pf1[el.ff]=(pf1[el.ff]+tmp*(el.ff+1))%MOD;
}
for(auto el: in[i])
if (c[el.ff-1] && el.ff-1>mi[find_set(el.ff-1)]) {
ll x=bit.query(el.ff-2)-bit.query(mi[find_set(el.ff-1)]-1);
ll x1=bit1.query(el.ff-2)-bit1.query(mi[find_set(el.ff-1)]-1);
ans=(ans+(el.ff*x%MOD-x1)*rgt[el.ff][el.ss]%MOD*(st.query(mi[find_set(el.ff-1)],el.ff-1)%MOD)%MOD)%MOD;
}
for(auto el: in[i]) {
bit.update(el.ff,-pf[el.ff]);
bit1.update(el.ff,-pf1[el.ff]);
pf[el.ff]=pf1[el.ff]=0;
}
for(auto el: in[i]) {
ll tmp=lft[el.ff][el.ss]%MOD*big[i-1]%MOD;
if (c[el.ff]>0) tmp=tmp*binpow(st.query(mi[find_set(el.ff)],el.ff)%MOD,MOD-2)%MOD;
bit.update(el.ff,tmp);
bit1.update(el.ff,tmp*(el.ff+1)%MOD);
pf[el.ff]=(pf[el.ff]+tmp)%MOD;
pf1[el.ff]=(pf1[el.ff]+tmp*(el.ff+1))%MOD;
}
for(auto el: in[i])
if (c[el.ff-1] && el.ff-1>mi[find_set(el.ff-1)]) {
ll x=bit.query(el.ff-2)-bit.query(mi[find_set(el.ff-1)]-1);
ll x1=bit1.query(el.ff-2)-bit1.query(mi[find_set(el.ff-1)]-1);
ans=(ans+(el.ff*x%MOD-x1)*pw[n-el.ff]%MOD*(st.query(mi[find_set(el.ff-1)],el.ff-1)%MOD)%MOD)%MOD;
}
for(auto el: in[i]) {
bit.update(el.ff,-pf[el.ff]);
bit1.update(el.ff,-pf1[el.ff]);
pf[el.ff]=pf1[el.ff]=0;
}
}
}
For(i,1,sz(big)) in[i].clear();
}
ll powv(ll x, ll y)
{
ll ans = 1;
while(y > 0)
{
if(y % 2 == 0)
{
y /= 2;
x *= x;
x %= MOD;
}else
{
y--;
ans *= x;
ans %= MOD;
}
}
return ans;
}
int main() {
// fio
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(ll i = 1;i <= 1000000;i++)
{
prw[i] = powv(i,MOD - 2);
}
pw[0]=1;
For(i,1,n) pw[i]=pw[i-1]*2%MOD;
For(i,1,n) {
a[i]=2;
cin >> a[i];
e[i][0]=a[i];
big.pb(a[i]);
}
For(i,1,n) {
b[i]=1;
cin >> b[i];
e[i][1]=b[i];
big.pb(b[i]);
}
sort(all(big));
unq(big);
solve();
reverse(b+1,b+1+n);
reverse(a+1,a+1+n);
For(i,1,n) {
e[i][0]=a[i];
e[i][1]=b[i];
}
solve(1);
For(i,1,n) ans=(ans-1LL*a[i]*pw[n-1]%MOD)%MOD;
For(i,1,n) ans=(ans-1LL*b[i]*pw[n-1]%MOD)%MOD;
ans=(ans+MOD)%MOD;
cout << ans;
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... |