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
#include<iostream>

using namespace std;

const int MAX_N = 500007;

const long long MOD = 1000000007;
long long result = 0;

struct Node {
	int left, right, parent, value;
};

struct Segment {
	int s, e;
	Segment() {
		s = e = 0;
	}
	Segment(int _s, int _e) {
		if(_s > _e) {
			s = 0;
			e = -1;
		}
		else {
			s = _s;
			e = _e;
		}
	}

	int len() {
		return e-s+1;
	}

	bool has(int v) {
		return s <= v && v <= e;
	}
};

struct SegmentBranch {
	Segment seg;
	int child;

	SegmentBranch() {
		seg = Segment();
		child = 0;
	}

	SegmentBranch(Segment _seg, int _child) {
		seg = _seg;
		child = _child;
	}
};

Node target[MAX_N];
Node tree[MAX_N];

int readRoot;
void readBST(Node nodes[], int n) {
	int par;
	for(int i = 1; i <= n; i++) {
		nodes[i].value = i;
		cin >> par;
		if(par == -1) {
			readRoot = i;
			continue;
		}
		nodes[i].parent = par;
		if(i > par) nodes[par].right = i;
		else nodes[par].left = i;
	}
}

Segment unwrap(int v, bool dir) {
	Segment total = Segment(v, v), temp;
	if(tree[v].left != 0) {
		temp = unwrap(tree[v].left, 0);
		total.s = temp.s;
		if(dir) result = (result + temp.len()) % MOD;
	}
	if(tree[v].right != 0) {
		temp = unwrap(tree[v].right, 1);
		total.e = temp.e;
		if(!dir) result = (result + temp.len()) % MOD;
	}
	return total;
}

SegmentBranch unwrapSearchLeft(int v, int search) {
	Segment temp = Segment(v, v);
	if(tree[v].right != 0) {
		temp = unwrap(tree[v].right, 1);
		result = (result + temp.len()) % MOD;
	}
	temp.s = v;
	if(temp.has(search)) {
		return SegmentBranch(temp, tree[v].left);
	}
	SegmentBranch branch = unwrapSearchLeft(tree[v].left, search);
	branch.seg.e = temp.e;
	return branch;
}

SegmentBranch unwrapSearchRight(int v, int search) {
	Segment temp = Segment(v, v);
	if(tree[v].left != 0) {
		temp = unwrap(tree[v].left, 0);
		result = (result + temp.len()) % MOD;
	}
	temp.e = v;
	if(temp.has(search)) {
		return SegmentBranch(temp, tree[v].right);
	}
	SegmentBranch branch = unwrapSearchRight(tree[v].right, search);
	branch.seg.s = temp.s;
	return branch;
}

void rerootLeftBranch(SegmentBranch branch, int expected);
void rerootRightBranch(SegmentBranch branch, int expected);

void reroot(int v, int expected) {
	if(v == 0) return;
	if(v == expected) {
		if(target[expected].left != 0) reroot(tree[v].left, target[expected].left);
		if(target[expected].right != 0) reroot(tree[v].right, target[expected].right);
		return;
	}
	SegmentBranch left, right;
	if(v > expected) {
		left = unwrapSearchLeft(tree[v].left, expected);
		result = (result + (left.seg.e-expected+1)) % MOD;
		right = SegmentBranch(Segment(expected+1, v), tree[v].right);
		left.seg = Segment(left.seg.s, expected-1);
	}
	else {
		right = unwrapSearchRight(tree[v].right, expected);
		result = (result + (expected-right.seg.s+1)) % MOD;
		left = SegmentBranch(Segment(v, expected-1), tree[v].left);
		right.seg = Segment(expected+1, right.seg.e);
	}
	if(target[expected].left != 0) rerootLeftBranch(left, target[expected].left);
	if(target[expected].right != 0) rerootRightBranch(right, target[expected].right);
}

void rerootLeftBranch(SegmentBranch branch, int expected) {
	if(branch.seg.len() <= 0) {
		reroot(branch.child, expected);
		return;
	}
	SegmentBranch temp;
	if(!branch.seg.has(expected)) {
		temp = unwrapSearchLeft(branch.child, expected);
		temp.seg.e = branch.seg.e;
		branch = temp;
	}
	result = (result + branch.seg.e-expected) % MOD;
	temp = SegmentBranch(Segment(expected+1, branch.seg.e), 0);
	branch.seg = Segment(branch.seg.s, expected-1);
	if(target[expected].left != 0) rerootLeftBranch(branch, target[expected].left);
	if(target[expected].right != 0) rerootRightBranch(temp, target[expected].right);
}

void rerootRightBranch(SegmentBranch branch, int expected) {
	if(branch.seg.len() <= 0) {
		reroot(branch.child, expected);
		return;
	}
	SegmentBranch temp;
	if(!branch.seg.has(expected)) {
		temp = unwrapSearchRight(branch.child, expected);
		temp.seg.s = branch.seg.s;
		branch = temp;
	}
	result = (result + expected-branch.seg.s) % MOD;
	temp = SegmentBranch(Segment(branch.seg.s, expected-1), 0);
	branch.seg = Segment(expected+1, branch.seg.e);
	if(target[expected].left != 0) rerootLeftBranch(temp, target[expected].left);
	if(target[expected].right != 0) rerootRightBranch(branch, target[expected].right);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, r1, r2; cin >> n;
	readBST(tree, n);
	r1 = readRoot;
	readBST(target, n);
	r2 = readRoot;
	reroot(r1, r2);
	cout << result << "\n";
	return 0;
}