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
// nie dziala no ale chuj

#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;

void rot_left(int &r, int v, vector<pair<int, int>> &adj, vector<int> &parent) {
  adj[v].first = r;
  adj[r].second = -1;
  parent[v] = parent[r];
  if (parent[v] != -1)
    adj[parent[v]].first = v;
  parent[r] = v;
  r = v;
}

void rot_right(int &r, int v, vector<pair<int, int>> &adj, vector<int> &parent) {
  adj[v].second = r;
  adj[r].first = -1;
  parent[v] = parent[r];
  if (parent[v] != -1)
    adj[parent[v]].second = v;
  parent[r] = v;
  r = v;
}

int flattenl(int v, vector<pair<int, int>> &adj, vector<int> &parent);
int flattenr(int v, vector<pair<int, int>> &adj, vector<int> &parent);

int flattenl(int v, vector<pair<int, int>> &adj, vector<int> &parent) {
  int cnt = 0;
  if (adj[v].first != -1) {
    cnt += flattenr(adj[v].first, adj, parent);
  }
  if (adj[v].second != -1) {
    cnt += flattenl(adj[v].second, adj, parent);
  }

  while (adj[v].second != -1) {
    rot_left(v, adj[v].second, adj, parent);
    ++cnt;
  }
  return cnt%mod;
}

int flattenr(int v, vector<pair<int, int>> &adj, vector<int> &parent) {
  int cnt = 0;
  if (adj[v].first != -1) {
    cnt += flattenl(adj[v].first, adj, parent);
  }
  if (adj[v].second != -1) {
    cnt += flattenr(adj[v].second, adj, parent);
  }

  while (adj[v].first != -1) {
    rot_right(v, adj[v].first, adj, parent);
    ++cnt;
  }
  return cnt%mod;
}

int rot(int r1, int r0, vector<vector<pair<int, int>>> &adj, vector<vector<int>> &parent) {
  if (r1 == -1 || r0 == -1 || adj[1][r1] == make_pair(-1, -1))
    return 0;
  if (r0 == r1) {
    return (rot(adj[1][r1].first, adj[0][r0].first, adj, parent)
          + rot(adj[1][r1].second, adj[0][r0].second, adj, parent)) % mod;
  }

  int i = adj[1][r1].first, cnt = 0, j = -1;
  while (r0 < i) {
    int j = adj[1][i].first;
    if (adj[1][i].second != -1)
      cnt += flattenr(adj[1][i].second, adj[1], parent[1]);
    while (adj[1][i].second != -1) {
      rot_left(i, adj[1][i].second, adj[1], parent[1]);
      ++cnt;
    }
    i = j;
  }
  i = adj[1][r1].second;

  
  while (i < r0) {
    int j = adj[1][i].second;
    if (j == -1)
      j = i;
    if (adj[1][i].first != -1)
      cnt += flattenl(adj[1][i].first, adj[1], parent[1]);
    while (adj[1][i].first != -1) {
      rot_right(i, adj[1][i].first, adj[1], parent[1]);
      ++cnt;
    }
    i = j;
  }


  if (r0 < r1) {
    while (parent[1][r0] != -1) {
      int l = adj[1][r1].first;
      adj[1][l].second = r1;
      adj[1][r1].first = -1;
      parent[1][r1] = l;
      parent[1][l] = -1;
      r1 = l;
      ++cnt;
    }
  }
  else {
    while (parent[1][r0] != -1) {
      int r = adj[1][r1].second;
      adj[1][r].first = r1;
      adj[1][r1].second = -1;
      parent[1][r1] = r;
      parent[1][r] = -1;
      r1 = r;
      ++cnt;
    }
  }
  
  return (cnt + rot(adj[1][r1].first, adj[0][r0].first, adj, parent)
              + rot(adj[1][r1].second, adj[0][r0].second, adj, parent)) % mod;
}

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

  int n; cin >> n;
  vector<vector<int>> parent(2, vector<int>(n));
  vector<vector<pair<int, int>>> adj(2, vector<pair<int, int>>(n, {-1, -1}));
  vector<int> r(2);
  for (int k = 0; k < 2; ++k) {
    for (int i = 0; i < n; ++i) {
      int a; cin >> a; --a;
      parent[k][i] = a;
      if (a == -2) {
        r[k] = i;
        parent[k][i] = -1;
      }
      else if (i < a)
        adj[k][a].first = i;
      else
        adj[k][a].second = i;
    }
  }

  cout << rot(r[1], r[0], adj, parent) % mod;
}