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
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define FORD(i, a, b) for (int i=(a); i>(b); i--)
#define PPC(x) __builtin_popcount(x)
#define MSB(x) (63 - __builtin_clzll(x))
#define SZ(x) ((int)(x).size())
#define HASK(S, x) (S.find(x) != S.end())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std; 

template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b);	}
template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b);	}

const int maxN = 1 << 19, mod = 1000000007;
long long res = 0;

struct Seg
{
	int mn, mx, top, ff, ch[2], fid;
	bool amr;

	void init(int a, int b, int c, int i, int l, int r, int f, bool mr);
	void pinSon();
	void joinup();
	void sepLeft(int id);
	void sepRight(int id);
	void cutOut();
	void print(int d);
}S[maxN * 3];

map <int, int> sid;

/*
void Seg::print(int dep = 0)
{
	if (ff == 0)	return;
	FOR(_, 0, dep) printf("  ");
	printf("%d:  %d %d %d,  %d %d %d   %d\n", ff, mn, mx, top, ch[0], ch[1], fid, amr);
	S[ch[0]].print(dep+1);
	S[ch[1]].print(dep+1);
}
*/

void Seg::init(int a, int b, int c, int i, int l, int r, int f, bool mr = false)
{
	mn = a, mx = b, top = c, ff =  i, ch[0] = l, ch[1] = r, fid = f;
	amr = mr;
	sid[mx] = ff;
}

void Seg::pinSon()
{
	int s = ch[amr];
	S[fid].ch[amr] = s;
	if (s != 0)
		S[s].fid = fid;
}
	
void Seg::joinup()
{	
	if (fid == 0)	return;
	while (ch[!amr] != 0)
		S[ch[!amr]].joinup();
	res += amr ? top - mn : mx - top;
	
	if (mx < S[fid].mx)
		sid.erase(mx);
	else
		sid[mx] = fid, sid.erase(S[fid].mx);
	
	remin(S[fid].mn, mn);
	remax(S[fid].mx, mx);
	pinSon();
}

void Seg::sepLeft(int nid)
{
	int left = ch[0];
	S[nid].init(mn, top-1, top-1, nid, left, 0, ff, false);
	if (left != 0)	S[left].fid = nid;	
	ch[0] = nid;
	mn = top;
}

void Seg::sepRight(int nid)
{
	int right = ch[1];
	S[nid].init(top+1, mx, top+1, nid, 0, right, ff, true);
	if (right != 0)	S[right].fid = nid;	
	ch[1] = nid;
	mx = top;
}

void Seg::cutOut()
{
	S[ch[0]].fid = S[ch[1]].fid = 0;
	ch[0] = ch[1] = 0;
	sid.erase(top);
}

int m;

struct BST
{
	int L[maxN], R[maxN], root;
	
	void load(int n)
	{
		FOR(i, 1, n+1)
		{
			int fat;
			scanf ("%d", &fat);
			if (fat == -1)	root = i;
			else
				(fat < i ? R : L)[fat] = i;
		}
	}
	
	int cons(int v, int fid = 0)
	{
		if (v == 0)	return 0;
		int a = v, b = v, id = ++m;
		while (a > 1 and L[a] == a-1)
			a--;
		while (R[b] == b+1)
			b++;
		int l = cons(L[a], id), r = cons(R[b], id);
		S[id].init(a, b, v, id, l, r, fid);
		S[l].amr = false;	S[r].amr = true;
		return id;
	}
	
	void run(int v)
	{
		if (v == 0)	return;
		
		int id = sid.lower_bound(v)->sd;
		while (S[id].fid != 0)
			S[id].joinup(), id = S[id].fid;

		res += abs(S[id].top - v);
		S[id].top = v;
		
		if (v != S[id].mn)
			S[id].sepLeft(++m);
		if (v != S[id].mx)
			S[id].sepRight(++m);
			
		S[id].cutOut();				
		run(L[v]), run(R[v]);
	}
}T0, T1;

void solve()
{
	int n;
	scanf ("%d", &n);
	T0.load(n);
	T1.load(n);
	T0.cons(T0.root);	
	T1.run(T1.root);
	printf("%lld\n", res % mod);
}
 
int main()
{
	int t = 1;
	//scanf ("%d", &t);
	FOR(tid, 1, t+1)
	{
		//printf("Case #%d: ", tid);
		solve();
	}
	return 0;
}