#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],vl[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,y=0,y1=0;
Node(ll mul=1,ll x=0,ll x1=0,ll y=0,ll y1=0): mul(mul),x(x),x1(x1),y(y),y1(y1) {};
} 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;
res.y=(B.y+A.y*B.mul%MOD)%MOD;
res.y1=(B.y1+A.y1*B.mul%MOD)%MOD;
return res;
}
void build(int id,int l,int r) {
if (l==r) {
tr[id]=Node(c[l],d[l]*(l+1)%MOD*pw[l-1]%MOD,d[l]*pw[l-1]%MOD,0,0);
return;
}
int mid=l+r>>1;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
tr[id]=mrg(tr[id*2],tr[id*2+1]);
}
void update(int id,int l,int r,int u) {
if (l>u || r<u) return;
if (l==r) {
tr[id]=Node(c[l],d[l]*(l+1)%MOD*pw[l-1]%MOD,d[l]*pw[l-1]%MOD,vl[l]*(l+1),vl[l]);
return;
}
int mid=l+r>>1;
update(id*2,l,mid,u);
update(id*2+1,mid+1,r,u);
tr[id]=mrg(tr[id*2],tr[id*2+1]);
}
Node query(int id,int l,int r,int u,int v) {
if (l>v || r<u) return Node(1,0,0);
if (l>=u && r<=v) return tr[id];
int mid=l+r>>1;
return mrg(query(id*2,l,mid,u,v),query(id*2+1,mid+1,r,u,v));
}
} st1;
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) 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);
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) c[i]=0,d[i]=2;
st1.build(1,1,n);
st.reset();
For(i,1,sz(big)) {
if (lmao) {
for(auto el: in[i]) {
d[el.ff]--;
st1.update(1,1,n,el.ff);
}
}
for(auto el: in[i-1]) {
c[el.ff]++;
st1.update(1,1,n,el.ff);
}
for(auto el: in[i]) {
if (el.ff>1) {
auto mmb=st1.query(1,1,n,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 (!lmao) {
for(auto el: in[i]) {
d[el.ff]--;
vl[el.ff]=(vl[el.ff]+lft[el.ff][el.ss])%MOD;
st1.update(1,1,n,el.ff);
}
for(auto el: in[i])
if (el.ff>1) {
auto mmb=st1.query(1,1,n,1,el.ff-1);
ll x=mmb.y%MOD,x1=mmb.y1%MOD;
ll tmp=(1LL*x1*el.ff%MOD-x)%MOD;
ans=(ans+big[i-1]*(pw[n-el.ff]-rgt[el.ff][el.ss])%MOD*tmp%MOD);
}
for(auto el: in[i])
if (vl[el.ff]!=0) {
vl[el.ff]=0;
st1.update(1,1,n,el.ff);
}
}
}
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... |