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>
using namespace std;
const int MOD=1000000007;
const int SIZE=1100000;
void imax(int &a, int b){
	a=max(a, b);
}
void imin(int &a, int b){
	a=min(a, b);
}
void lmax(long long &a, long long b){
	a=max(a, b);
}
void lmin(long long &a, long long b){
	a=min(a, b);
}
/*
	WARNING: I'm using strange bracket style!
*/
class node{
	public:
	node* l;
	node* r;
	node* p;
	int val;
	node(){
		l=r=p=nullptr, val=0;
	}
};
long long steps=0;
node* where[SIZE];
int parent[SIZE];
node pool[SIZE];
node* r1;
node* r2;
int N;
bool isRight(node *n){
	return (n->p->r)==n;
}
bool isLeft(node *n){
	return (n->p->l)==n;
}
void dbg(node* n){
	if (n->l!=nullptr)
		cout<<"(", dbg(n->l), cout<<")";
	cout<<n->val;
	if (n->r!=nullptr)
		cout<<"(", dbg(n->r), cout<<")";
}
pair <long long, int> countDp(node *n){
	if (n==nullptr) return {0, 0};
	pair <long long, int> R=countDp(n->r);
	pair <long long, int> L=countDp(n->l);
	return {R.first+L.first+(isLeft(n)?R.second:L.second), R.second+L.second+1};
}
void toLine(node *n, int r){
	bool onLeft=((n->val)>r);
	while ((n->val)!=r)
		{
		if (onLeft)
			{//Na lewo
			steps+=countDp(n->l->r).first+(n->val)-(n->l->val)-1;
			node* L=n->l;
			for (int u=(n->val)-1; u>(L->val); u--)
				where[u]->p=where[u+1], where[u]->l=where[u-1], where[u]->r=nullptr;
			L->p=where[(L->val)+1], n->l=where[(n->val)-1], L->r=nullptr;
			n=n->l;
			}
		else
			{//Na prawo
			steps+=countDp(n->r->l).first-(n->val)+(n->r->val)-1;
			node *R=n->r;
			for (int u=(n->val+1); u<(R->val); u++)
				where[u]->p=where[u-1], where[u]->r=where[u+1], where[u]->l=nullptr;
			R->p=where[(R->val)-1], n->r=where[(n->val)+1], R->l=nullptr;
			n=n->r;
			}
		}
}
void rotate(node* U){
	assert(U->p!=nullptr);
	node* P=U->p;
	node* G=P->p;
	if (G!=nullptr)
		if (isLeft(P))
			G->l=U;
		else
			G->r=U;
	//cout<<"Rootate: "<<U->val<<" "<<P->val<<endl;
	if (isLeft(U))
		assert(U->r==nullptr), U->r=P, U->p=P->p, P->p=U, P->l=nullptr;
	else
		assert(U->l==nullptr), U->l=P, U->p=P->p, P->p=U, P->r=nullptr;
}
void letsGo(int low, int high, node* A, node* B){
	if (A==nullptr) return;
	//cout<<"A:    ", dbg(A), cout<<endl;
	//cout<<"B:    ", dbg(B), cout<<endl;
	bool easyRight=(A->r==nullptr);
	bool easyLeft=(A->l==nullptr);
	int root=B->val;
	toLine(A, root);
	//cout<<"A:    ", dbg(A), cout<<endl;
	//cout<<"B:    ", dbg(B), cout<<endl;
	if (A->val>root)
		while (A->val!=root)
			A=A->l, rotate(A), steps++;
	else
		while (A->val!=root)
			A=A->r, rotate(A), steps++;
	//cout<<"A:    ", dbg(A), cout<<endl;
	//cout<<"B:    ", dbg(B), cout<<endl;
	//cout<<steps<<endl;
	if (easyLeft)
		steps+=countDp(B->l).first;
	else
		letsGo(1, A->val-1, A->l, B->l);
	if (easyRight)
		steps+=countDp(B->r).first;
	else
		letsGo(A->val+1, N, A->r, B->r);
}
int main()
	{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>N;
	for (int i=1; i<=N; i++)
		{
		cin>>parent[i], where[i]=pool+i, where[i]->val=i;
		if (parent[i]==-1)
			r1=pool+i;
		}
	for (int i=1; i<=N; i++)
		{
		cin>>parent[i+N], where[i+N]=pool+i+N, where[i+N]->val=i;
		if (parent[i+N]==-1)
			r2=pool+i+N;
		}
	for (int i=1; i<=N; i++)
		{
		//First Tree;
		if (parent[i]!=-1)
			{
			where[i]->p=where[parent[i]];
			if ((where[i]->p->val)>(where[i]->val))
				where[i]->p->l=where[i];
			else
				where[i]->p->r=where[i];
			}
		//Second Tree;
		if (parent[i+N]!=-1)
			{
			where[i+N]->p=where[parent[i+N]+N];
			if ((where[i+N]->p->val)>(where[i+N]->val))
				where[i+N]->p->l=where[i+N];
			else
				where[i+N]->p->r=where[i+N];
			}
		}
	letsGo(1, N, r1, r2);
	cout<<steps%MOD<<"\n";
	return 0;
	}