#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std ;
const int maxn=250000 +5 ;
const int mod=1e9 +7 ;
typedef long long int ll;
ll ans;
int n,m;
int a[maxn*2 ],b[maxn*2 ],c[maxn*2 ],h[maxn*2 ];
int lowbit(int x)
{
return x & (-x);
}
void update(int x)
{
int lx,i;
while (x<=2 *n)
{
h[x]=a[x];
lx=lowbit(x);
for (i=1 ;i<lx;i<<=1 )
h[x]=max(h[x],h[x-i]);
x+=lowbit(x);
}
}
int query(int x, int y)
{
int ans = 0 ;
while (y >= x)
{
ans = max(a[y], ans);
y --;
for (; y-lowbit(y) >= x; y -= lowbit(y))
ans = max(h[y], ans);
}
return ans;
}
int main()
{
while (~scanf ("%d" ,&n))
{
ans=0 ;
ll maxv=0 ;
memset (a,0 ,sizeof (a));
memset (b,0 ,sizeof (b));
memset (c,0 ,sizeof (c));
memset (h,0 ,sizeof (h));
for (int i=1 ;i<=n;i++)
{
scanf ("%d" ,&c[i]);
a[i]=c[i]-i;
}
for (int i=1 ;i<=n;i++)
{
scanf ("%d" ,&b[i]);
}
sort(b+1 ,b+1 +n);
for (int i=1 ;i<=2 *n;i++)
{
update(i);
}
for (int i=n+1 ;i<=2 *n;i++)
{
int ql=b[i-n];
maxv=query(ql,i);
ans=(ans+maxv)%mod;
a[i]=maxv-i;
update(i);
}
printf ("%lld\n" ,ans);
}
return 0 ;
}