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
149
150
151
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

constexpr int M=5e5+7;
constexpr int mod=1e9+7;
constexpr int base=1<<19;

int p[2][M];
int som[2][M][2]; //0 -> lewy; 1 -> prawy
int intervals[2][2][M];
int Tree[2][base<<1];
bool was[2][M];
vector<pair<pair<int,int>,bool> > P;

bool cmp(pair<pair<int,int>,bool>a, pair<pair<int,int>,bool>b){
	return abs(a.first.second-a.first.first) > abs(b.first.second-b.first.first);
}

void add(int T, int a, int b){
	a += base-1;
	b += base+1;
	while(b-a>1){
		if(!(a&1)) Tree[T][a+1]++;
		if(b&1) Tree[T][b-1]++;
		a>>=1; b>>=1;
	}
}

int query(int T, int v){
	int w=0;
	v += base;
	while(v){
		w += Tree[T][v];
		v>>=1;
	}
	return w;
}

void straightening(int T, int v, bool lr, int mn, int mx){
	if(som[T][v][0]) straightening(T, som[T][v][0], 0, mn, v-1);
	if(som[T][v][1]) straightening(T, som[T][v][1], 1, v+1, mx);
	
	if(lr && mn+1<=v){
		intervals[T][1][mn+1] = v;
		P.push_back({{mn+1, v},T});
	}
	else if(!lr && v<=mx-1){
		intervals[T][0][mx-1] = v;
		P.push_back({{mx-1, v},T});
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	long long res = 0;
	int n, V, a, b, c, d, T;
	cin >> n;
	
	for(int i=1; i<=n; i++){
		cin >> p[0][i];
		if(i < p[0][i]) som[0][p[0][i]][0] = i;	
		if(i > p[0][i]) som[0][p[0][i]][1] = i;
	}
	for(int i=1; i<=n; i++){
		cin >> p[1][i];
		if(i < p[1][i]) som[1][p[1][i]][0] = i;	
		if(i > p[1][i]) som[1][p[1][i]][1] = i;
	}	
	
	for(int i=1; i<=n; i++) if(p[0][i]==-1) V=i;
	straightening(0, V, 0, 1, n);
	
	for(int i=1; i<=n; i++) if(p[1][i]==-1) V=i;	
	straightening(1, V, 0, 1, n);
	
	/*cout << '\n';
	cout << "L0:\n";
	for(int i=1; i<=n; i++) if(intervals[0][0][i]) cout << '[' << intervals[0][0][i] << ',' << i << "]\n";
	cout << '\n';

	cout << '\n';
	cout << "R0:\n";
	for(int i=1; i<=n; i++) if(intervals[0][1][i]) cout << '[' << i << ',' << intervals[0][1][i] << "]\n";
	cout << '\n';
	
	cout << '\n';
	cout << "L1:\n";
	for(int i=1; i<=n; i++) if(intervals[1][0][i]) cout << '[' << intervals[1][0][i] << ',' << i << "]\n";
	cout << '\n';
	
	cout << '\n';
	cout << "R1:\n";
	for(int i=1; i<=n; i++) if(intervals[1][1][i]) cout << '[' << i << ',' << intervals[1][1][i] << "]\n";
	cout << '\n';*/	
	
	sort(P.begin(), P.end(), cmp);
	
	for(auto w:P){
		a = w.first.first;
		b = w.first.second;
		T = w.second;
		
		
		if(a>b){ //L
			res += a-b+1;
			if(was[0][a]) continue;
			was[0][a]=1;
			
			c = intervals[T^1][0][a];
			
			//cout << a << ' '<< b << ' '<< T << ' ' << c <<'\n';
			
			if(query(T, a) || query(T^1, a)) continue;
			
			if(!c) add(T, b, a);
			else{
				d = max(b, c);
				res -= (a-d+1)*2;
				add(T, b, d-1);
				add(T^1, c, d-1);
			}
		}
		else{ //P
			res += b-a+1;
			if(was[1][a]) continue;	
			was[1][a]=1;
			
			c = intervals[T^1][1][a];
			
			if(query(T, a) || query(T^1, a)) continue;
			
			if(!c) add(T, a, b);
			else{
				d = min(b,c);
				res -= (d-a+1);
				add(T, d+1, b);
				add(T^1, d+1, c);
			}			
		}
	}
	
	cout << res%mod;
	
	return 0;
}