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
#include <bits/stdc++.h>
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define TRAV(i, a) for(auto & i : (a))
#define SZ(x) ((int)(x).size())
#define PR std::pair
#define MP std::make_pair
#define X first
#define Y second
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

constexpr const int MOD = 1000000007;
constexpr const int LOG = 20;
inline int DOD(int a, int b){
	return (a + b >= MOD ? a + b - MOD : a + b);
}
int ans;

struct K{
	int left, right;
	K(int a=-1, int b=-1) : left(a), right(b) {}
};

struct Tree{
	int n;
	std::vector<K> children;
	std::vector<int> parent;
	std::vector<int> level;
	std::array<VI, LOG> ljump, rjump;

	int read(int sz){
		n = sz;
		children = std::vector<K>(n);
		parent = std::vector<int>(n);
		level = std::vector<int>(n);
		FOR(i, LOG) ljump[i] = rjump[i] = VI(n, -1);

		int root = -1;
		FOR(i, n){
			int a;
			std::cin >> a;
			if(a != -1) a--;
			parent[i] = a;
			if(a != -1){
				if(a > i){
					children[a].left = i;
					ljump[0][i] = a;
				}else{
					assert(a != i);
					children[a].right = i;
					rjump[0][i] = a;
				}
			}else{
				root = i;
			}
		}
		assert(root != -1);
		prepro(root);
		return root;
	}

	int onleft(int a, int upto){
		for(int l = LOG-1; l >= 0; --l){
			if(rjump[l][a] != -1 && level[rjump[l][a]] >= upto)
				a = rjump[l][a];
		}
		return a;
	}

	int onright(int a, int upto){
		for(int l = LOG-1; l >= 0; --l){
			if(ljump[l][a] != -1 && level[ljump[l][a]] >= upto)
				a = ljump[l][a];
		}
		return a;
	}

	void prepro(int root){
		std::function<void(int)> dfs = [&](int v){
			if(ljump[0][v] == -1 && parent[v] != -1)
				ljump[0][v] = ljump[0][parent[v]];
			if(rjump[0][v] == -1 && parent[v] != -1)
				rjump[0][v] = rjump[0][parent[v]];

			int l = children[v].left;
			if(l != -1) level[l] = level[v]+1, dfs(l);
			l = children[v].right;
			if(l != -1) level[l] = level[v]+1, dfs(l);
		};
		dfs(root);

		REP(l, 1, LOG){
			FOR(i, n){
				rjump[l][i] = (rjump[l-1][i] == -1 ? -1 : rjump[l-1][rjump[l-1][i]]);
				ljump[l][i] = (ljump[l-1][i] == -1 ? -1 : ljump[l-1][ljump[l-1][i]]);
			}
		}
	}

	int block1, block2;
	void count(int v, int cnt=0, int dr=-1){
		ans = DOD(ans, cnt);
		int l = children[v].left;
		if(l != -1 && l != block1 && l != block2){
			count(l, cnt + (dr == 1), 0);
		}
		l = children[v].right;
		if(l != -1 && l != block1 && l != block2){
			count(l, cnt + (dr == 0), 1);
		}
	}
};

int n;
Tree A, B;

void rec(int a, int b, int ra, int rb){
	assert(ra != -1 && rb != -1);
	assert(ra >= a && ra <= b && rb >= a && rb <= b);
	if(ra == rb){
		if(a < ra) rec(a, ra-1, A.children[ra].left, B.children[rb].left);
		if(b > ra) rec(ra+1, b, A.children[ra].right, B.children[rb].right);
		return;
	}

	int lo = ra;
	int hi = rb;
	if(lo > hi) std::swap(lo, hi);
	ans = DOD(ans, hi-lo);

	while(true){
		int oldlo = lo;
		int oldhi = hi;
		lo = std::min(lo, A.onleft(lo, A.level[ra]));
		hi = std::max(hi, A.onright(hi, A.level[ra]));
		lo = std::min(lo, B.onleft(lo, B.level[rb]));
		hi = std::max(hi, B.onright(hi, B.level[rb]));
		if(lo == oldlo && hi == oldhi) break;
	}
	assert(lo >= a && hi <= b);

	A.block1 = A.children[lo].left;
	A.block2 = A.children[hi].right;
	A.count(ra);
	B.block1 = B.children[lo].left;
	B.block2 = B.children[hi].right;
	B.count(rb);

	if(lo > a) rec(a, lo-1, A.children[lo].left, B.children[lo].left);
	if(hi < b) rec(hi+1, b, A.children[hi].right, B.children[hi].right);
}

int main(){
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);

	std::cin >> n;
	int ra = A.read(n);
	int rb = B.read(n);
	rec(0, n-1, ra, rb);
	std::cout << ans << "\n";
	return 0;
}