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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <array>
#include <random>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <climits>
#include <stack>
#include <random>
#include <bitset>
#include <numeric>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
#define rep(x, b, e) for(int x=(b); x<(e); ++x)
#define trav(a, x) for(auto& a : x)
#define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define st first
#define nd second
#define mp(x,y) make_pair(x,y)
typedef short int sint;

#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int N = 5e5 + 10;
const int P = 1e9 + 7;
int n;

int add(int a, int b) {
	a += b;
	if (a >= P) a -= P;
	return a;
}

struct Node {
	int mini, maxi;
	int lCost, rCost;
	int val;

	int lson, rson;
	int zakresL, zakresR;

	int getLson() {
		if (zakresL < val) {
			return val - 1;
		}
		return lson;
	}

	int getRson() {
		if (zakresR > val) {
			return val + 1;
		}
		return rson;
	}

	int zakres() {
		return maxi - mini + 1;
	}

	// void print() {
	// 	printf("\tNode: %d\n", val);
	// 	printf("\t\t l: %d, r: %d, mini: %d, maxi: %d, lCost: %d, rCost: %d\n", lson, rson, mini, maxi, lCost, rCost);
	// 	printf("\t\t zakresL: %d, zakresR: %d\n", zakresL, zakresR);
	// }

	Node() {
		mini = maxi = lCost = rCost = val = zakresL = zakresR = 0;
		lson = -1, rson = -1;
	}
} stare[N], nowe[N];

int zakres(int index) {
	if (index == -1) return 0;
	return stare[index].zakres();
}

int costL(int index) {
	if (index == -1) return 0;
	return stare[index].lCost;
}

int costR(int index) {
	if (index == -1) return 0;
	return stare[index].rCost;
}

int roz(int index) {
	if (index == -1) return 0;
	return stare[index].zakres();
}

int maxi(int index) {
	if (index == -1) return INT_MIN;
	return stare[index].maxi;
}

int mini(int index) {
	if (index == -1) return INT_MAX;
	return stare[index].mini;
}

void compute(Node *t, int v) {
	if (v == -1) return;
	// printf("compute %d\n", v);
	t[v].val = t[v].mini = t[v].maxi = t[v].zakresL = t[v].zakresR = v;
	int lef = t[v].lson, rig = t[v].rson;
	compute(t, lef);
	compute(t, rig);
	if (lef != -1) {
		t[v].mini = t[lef].mini;
	}
	if (rig != -1) {
		t[v].maxi = t[rig].maxi;
	}
	t[v].lCost = costR(rig) + costL(lef) + roz(rig);
	t[v].rCost = costR(rig) + costL(lef) + roz(lef);

}

int readTree(Node* t, vi& p) {
	int root;
	rep(i, 1, n + 1) {
		int x;
		scanf("%d", &x);
		if (x == -1) root = i;
		else {
			if (x < i) {
				t[x].rson = i;
			} else {
				t[x].lson = i;
			}
		}
		p[i] = x;
	}
	compute(t, root);
	return root;
}

int res;

// void printTree(Node *t, int root) {
// 	if (root == -1) {
// 		return ;
// 	}
// 	t[root].print();
// 	printTree(t, t[root].lson);
// 	printTree(t, t[root].rson);
// }

void prepareSons(int root) {
	int lson = stare[root].getLson();
	if (lson != stare[root].lson) {
		// z patyka
		stare[lson].zakresL = stare[root].zakresL;
		stare[lson].zakresR = lson;
		stare[lson].maxi = lson;
		stare[lson].mini = stare[root].mini;
		stare[lson].rson = -1;
		stare[lson].lson = stare[root].lson;
	}
	int rson = stare[root].getRson();
	if (rson != stare[root].rson) {
		stare[rson].zakresR = stare[root].zakresR;
		stare[rson].zakresL = rson;
		stare[rson].mini = rson;
		stare[rson].maxi = stare[root].maxi;
		stare[rson].lson = -1;
		stare[rson].rson = stare[root].rson;
	}
}

void solve(int old_root, int new_root) {
	// printf("Solving for: %d at old root, and should be: %d\n", old_root, new_root);
	// printTree(stare, old_root);
	// szukam new_root w old_root
	if (new_root < old_root) {
		// printf("new root on left side\n");
		int helper = old_root;
		while (stare[old_root].zakresL > new_root) {
			// kill him
			helper = stare[old_root].lson;
			// printf("killing node: %d\n", helper);
			res = add(res, costR(stare[helper].rson));
			res = add(res, zakres(stare[helper].rson));

			stare[old_root].zakresL = helper;
			stare[old_root].lson = stare[helper].lson;
		}
	
		// czyli juz mam patyka ktory siega za mojego roota
		res = add(res, old_root - new_root);
		stare[new_root].rson = stare[old_root].rson;
		stare[new_root].lson = stare[helper].lson; // inne
		stare[new_root].zakresL = stare[old_root].zakresL;
		stare[new_root].zakresR = stare[old_root].zakresR;
		stare[new_root].mini = stare[old_root].mini;
		stare[new_root].maxi = stare[old_root].maxi;

		stare[old_root].lson = -1;
		stare[old_root].zakresL = old_root;
		stare[old_root].mini = old_root;
	} else if (new_root > old_root) {
		// analogicznie :D
		// printf("new root on right side\n");
		int helper = old_root;
		while (stare[helper].zakresR < new_root) {
			helper = stare[old_root].rson;
			// printf("killing node: %d\n", helper);
			res = add(res, costL(stare[helper].lson));
			res = add(res, zakres(stare[helper].lson));

			stare[old_root].zakresR = helper;
			stare[old_root].rson = stare[helper].rson;
		}

		res = add(res, new_root - old_root);
		// stare[new_root] = stare[old_root];
		stare[new_root].lson = stare[old_root].lson;
		stare[new_root].rson = stare[helper].rson; // inne
		stare[new_root].zakresL = stare[old_root].zakresL;
		stare[new_root].zakresR = stare[old_root].zakresR;
		stare[new_root].mini = stare[old_root].mini;
		stare[new_root].maxi = stare[old_root].maxi;

		stare[old_root].rson = -1;
		stare[old_root].zakresR = old_root;
		stare[old_root].maxi = old_root;
	}
	if (old_root == -1) {
		// printf("job is done\n");
		return;
	}
	prepareSons(new_root);
	// printf("AFTER OPERATION:\n");
	// printTree(stare, new_root);
	solve(stare[new_root].getLson(), nowe[new_root].lson);
	solve(stare[new_root].getRson(), nowe[new_root].rson);
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	scanf("%d", &n);
	vi old_p(n), new_p(n);
	int old_root = readTree(stare, old_p);
	int new_root = readTree(nowe, new_p);
	solve(old_root, new_root);
	printf("%d\n", res);
}