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
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;

inline int nextInt() { int n; scanf("%d", &n); return n; }
inline int myAbs(int n) { return n >= 0 ? n : -n; }
int res;
const int p = 1000000000 + 7;
inline void add(int &a, int b) { a += b; if (a >= p) a -= p; }
inline void addRes(int cnt) { add(res, cnt); }

const int N = 500000 + 9;

int n;

struct Node {
	int left;
	int right;
};

struct Task {
	Node *t1;
	int r1;
	Node *t2;
	int r2;
	int a;
	int b;
};

queue<Task> mainQueue;

Node t1[N];
Node t2[N];

void init() {
	res = 0;
	n = nextInt();
	int r1;
	int r2;
	for (int i=1; i<=n; ++i) {
		int p = nextInt();
		if (p < 0) r1 = i;
		else if (p < i) t1[p].right = i;
		else t1[p].left = i;
	}
	for (int i=1; i<=n; ++i) {
		int p = nextInt();
		if (p < 0) r2 = i;
		else if (p < i) t2[p].right = i;
		else t2[p].left = i;
	}

	mainQueue.push( {t1, r1, t2, r2, 1, n} );
}

queue<Task> leftQueue;
queue<Task> rightQueue;

void toList() {
	while ((!leftQueue.empty()) || (!rightQueue.empty())) {
		while (!leftQueue.empty()) {
			Node *t = leftQueue.front().t1;
			int r = leftQueue.front().r1;
			int a = leftQueue.front().a;
			int b = leftQueue.front().b;
			leftQueue.pop();
			if (r == 0) continue;
			leftQueue.push( {t, t[r].left, 0, 0, a, r - 1} );
			rightQueue.push( {t, t[r].right, 0, 0, r + 1, b} );
			addRes(b - r);
		}
		while (!rightQueue.empty()) {
			Node *t = rightQueue.front().t1;
			int r = rightQueue.front().r1;
			int a = rightQueue.front().a;
			int b = rightQueue.front().b;
			rightQueue.pop();
			if (r == 0) continue;
			leftQueue.push( {t, t[r].left, 0, 0, a, r - 1} );
			rightQueue.push( {t, t[r].right, 0, 0, r + 1, b} );
			addRes(r - a);
		}
	}
}

void toLeftList(Node *t, int r, int a, int b) {
	leftQueue.push( {t, r, 0, 0, a, b} );
	toList();
}

void toRightList(Node *t, int r, int a, int b) {
	rightQueue.push( {t, r, 0, 0, a, b} );
	toList();
}

void solve(Node *t1, int r1, Node *t2, int r2, int a, int b) {
	if (r1 == 0 || r2 == 0) {
		return;
	}

	if (r1 == r2) {
		mainQueue.push( {t1, t1[r1].left, t2, t2[r2].left, a, r1 - 1} );
		mainQueue.push( {t1, t1[r1].right, t2, t2[r2].right, r1 + 1, b} );
		return;
	}

	addRes(myAbs(r2 - r1));

	int cur1;
	int cur2;

	// right side
	cur1 = r1;
	cur2 = r2;

	while (cur1 != cur2) {
		if (cur1 < cur2) {
			int next1 = t1[cur1].right;
			toLeftList(t1, t1[next1].left, cur1 + 1, next1 - 1);
			addRes(next1 - cur1 - 1);
			cur1 = next1;
		} else {
			int next2 = t2[cur2].right;
			toLeftList(t2, t2[next2].left, cur2 + 1, next2 - 1);
			addRes(next2 - cur2 - 1);
			cur2 = next2;
		}
	}

	mainQueue.push( {t1, t1[cur1].right, t2, t2[cur2].right, cur1 + 1, b} );

	// left side
	cur1 = r1;
	cur2 = r2;

	while (cur1 != cur2) {
		if (cur1 > cur2) {
			int next1 = t1[cur1].left;
			toRightList(t1, t1[next1].right, next1 + 1, cur1 - 1);
			addRes(cur1 - next1 - 1);
			cur1 = next1;
		} else {
			int next2 = t2[cur2].left;
			toRightList(t2, t2[next2].right, next2 + 1, cur2 - 1);
			addRes(cur2 - next2 - 1);
			cur2 = next2;
		}
	}

	mainQueue.push( {t1, t1[cur1].left, t2, t2[cur2].left, a, cur1 - 1} );
}

int main() {
	init();

	while (!mainQueue.empty()) {
		Node *t1 = mainQueue.front().t1;
		int r1 = mainQueue.front().r1;
		Node *t2 = mainQueue.front().t2;
		int r2 = mainQueue.front().r2;
		int a = mainQueue.front().a;
		int b = mainQueue.front().b;
		mainQueue.pop();
		solve(t1, r1, t2, r2, a, b);
	}

	printf("%d\n", res);

	return 0;
}