1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include<bits/stdc++.h>
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n";
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define SIZE(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<vector<int> > VVI;
const int INFTY=20000000;
const int MAX=500100;
const ll MOD=1e9+7;

void coutTab(int* tab,int n){
	loop(i,0,n){
		cout<<(tab[i]==-1 ? -1 : tab[i]+1)<<" ";
	}
	cout<<"\n";
}

template<class T> void coutVec(vector<T> tab){
	for(T t : tab){
		cout<<t<<" ";
	}
	cout<<"\n";
}
//------------------------------------------
int a[MAX];
int b[MAX];
int Lb[MAX];
int Rb[MAX];
int n;

int readM(map<int,int> & M, int i) {
	if(M.find(i)==M.end()) return -1;
	return M[i];
}

pair<ll,ll> flatten(map<int,int> & L, map<int,int> & R, int v, int d) {
	if(v == -1) return {0,0};
	auto l = flatten(L, R, readM(L, v), 1);
	auto r = flatten(L, R, readM(R, v), 2);
	ll w = 0;
	ll c = 0;
	if(d == 1) {
		w = (l.ff + 1 + r.ff)%MOD;
		c = (l.ss + r.ss + r.ff)%MOD;
	} else {
		w = (l.ff + 1 + r.ff)%MOD;
		c = (l.ss + r.ss + l.ff)%MOD;
	}
	return {w,c};
}
ll solve(int l, int r, int cr, int er) {
	//ps("WYWOŁANO"); ps(l); ps(r); ps(cr); pln(er);
	if(l >= r) return 0;
	map<int, int> L,R;
	loop(i,l,r) {
		if(a[i]!=-1){
			if(a[i]>i) L[a[i]]=i;
			else R[a[i]]=i;
		}	
	}	
	ll c = 0;
	if(cr > er) {
		int v=readM(L, cr);
		while(v>er) {
			auto f = flatten(L, R, readM(R, v), 2);
			c = (c + f.ff + f.ss) % MOD;
			v=readM(L, v);
		}
		auto f = flatten(L, R, readM(R, v), 2);
		c = (c + f.ff + f.ss + cr - er)%MOD;
		loop(i,v,er) {
			a[i]=i+1;
		}
		a[er]=-1;
		loop(i,er+1,cr+1) {
			a[i]=i-1;
		}
		int lv = readM(L,v);
		if(lv!=-1) a[lv]=v;
	} else if(cr < er) {
		int v=readM(R, cr);
		while(v<er) {
			auto f = flatten(L, R, readM(L, v), 1);
			c = (c + f.ff + f.ss)%MOD;
			v=readM(R, v);
		}
		auto f = flatten(L, R, readM(L, v), 1);
		c = (c + f.ff + f.ss + er - cr)%MOD;
		loop(i,cr,er) {
			a[i]=i+1;
		}
		a[er]=-1;
		loop(i,er+1,v+1) {
			a[i]=i-1;
		}
		int rv = readM(R, v);
		if(rv != -1) a[rv]=v;
	}
	//coutTab(a, n);
	//pln(c);
	int erl = -1;
	int err = -1;
	loop(i,l,r) {
		if(a[i]==er && i < er) erl = i;
		if(a[i]==er && i > er) err = i;
	}
	return (c + (erl != -1 ? solve(l, er, erl, Lb[er]) : 0) + (err != -1 ? solve(er+1, r, err, Rb[er]) : 0))%MOD;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin>>n;
	int cr, er;
	loop(i,0,n) {
		cin>>a[i];
		if(a[i]!=-1) a[i]--;
		else cr = i;
	}
	loop(i,0,n) {
		cin>>b[i];
		if(b[i]!=-1) b[i]--;
		else er = i;
	}
	loop(i,0,n) {
		Lb[i]=-1;
		Rb[i]=-1;	
	}
	loop(i,0,n) {
		if(b[i]!=-1){
			if(b[i]>i) Lb[b[i]]=i;
			else Rb[b[i]]=i;
		}	
	}
	pln(solve(0, n, cr, er));
}