主要是通过那个公式转化成在bk到i之间找最大值,这样就能做了
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define mem(i,a) memset(i,a,sizeof(i))
#define rson mid+1,r,rt<<1|1
#define lson l,mid,rt<<1
#define ll long long
#define LL long long
using namespace std;
template <typename T>inline void read(T &_x_){
_x_=0;bool f=false;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=!f;ch=getchar();}
while ('0'<=ch&&ch<='9') {_x_=_x_*10+ch-'0';ch=getchar();}
if(f) _x_=-_x_;
}
const double eps = 1e-8;
const int maxn = 5e5+7;
const int mod = 1e9+7;
const ll inf = 1e15;
int n;
ll a[maxn],b[maxn];
ll sum[maxn];
int main(){
while(~scanf("%d",&n)){
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]),a[i]-=i;
for(int i=1;i<=n;i++) scanf("%I64d",&b[i]);
sort(b+1,b+n+1);
ll ans=0;
sum[n]=a[n];
for(int i=n-1;i>=1;i--){
sum[i]=max(a[i],sum[i+1]);
}
ll mx = 0;
for(int i=1;i<=n;i++){
ans+=max(mx,sum[b[i]]);
mx=max(mx,sum[b[i]]-n-i);
ans%=mod;
}
printf("%I64d\n",ans);
}
return 0;
}