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
188
#if defined(EMBE_DEBUG) && !defined(NDEBUG)
#include "embe-debug.hpp"
#else
#define LOG_INDENT(...) do {} while (false)
#define LOG(...) do {} while (false)
#define DUMP(...) do {} while (false)
#endif

#include <cassert>
#include <vector>
#include <iostream>
using namespace std;

namespace {

int const mod = 1'000'000'007;

struct Tree {
  int n;

  int root;
  vector<int> parent;
  vector<int> left;
  vector<int> right;

  void read(int n_)
  {
    n = n_;
    parent = vector<int>(n, -1);
    left = vector<int>(n, -1);
    right = vector<int>(n, -1);
    for (int i = 0; i < n; ++i) {
      int x;
      cin >> x;
      if (x < 0) {
        root = i;
      } else {
        --x;
        parent[i] = x;
        if (i < x) left[x] = i;
        else right[x] = i;
      }
    }
  }

  friend ostream& operator<<(ostream& os, Tree const& t)
  {
    t.print(os, t.root);
    return os;
  }

  void print(ostream& os, int x) const
  {
    os << '(';
    if (x >= 0) {
      os << x;
      print(os, left[x]);
      print(os, right[x]);
    }
    os << ')';
  }
};

void rotate_left(Tree& t, int v)
{
  int w = t.right[v];
  assert(w >= 0);
  assert(t.left[w] < 0);
  if (t.parent[v] >= 0) {
    if (t.right[t.parent[v]] == v) t.right[t.parent[v]] = w;
    else t.left[t.parent[v]] = w;
  } else t.root = w;
  t.parent[w] = t.parent[v];
  t.parent[v] = w;
  t.left[w] = v;
  t.right[v] = -1;
}

void rotate_right(Tree& t, int v)
{
  int w = t.left[v];
  assert(w >= 0);
  assert(t.right[w] < 0);
  if (t.parent[v] >= 0) {
    if (t.right[t.parent[v]] == v) t.right[t.parent[v]] = w;
    else t.left[t.parent[v]] = w;
  } else t.root = w;
  t.parent[w] = t.parent[v];
  t.parent[v] = w;
  t.right[w] = v;
  t.left[v] = -1;
}

int flatten_right(Tree& t, int a, int b);
int flatten_left(Tree& t, int a, int b);

int flatten_right(Tree& t, int a, int b)
{
  if (a < 0) return 0;
  int x = t.right[a];
  if (x < 0) return 0;
  int res = 0;
  if (t.left[x] >= 0) {
    res += flatten_left(t, x, a + 1);
    res %= mod;
    res += x - a - 1;
    res %= mod;
    while (t.left[x] >= 0) {
      rotate_right(t, x);
      --x;
    }
  }
  if (x == b) return res;
  res += flatten_right(t, x, b);
  res %= mod;
  return res;
}

int flatten_left(Tree& t, int a, int b)
{
  if (a < 0) return 0;
  int x = t.left[a];
  if (x < 0) return 0;
  int res = 0;
  if (t.right[x] >= 0) {
    res += flatten_right(t, x, a - 1);
    res %= mod;
    res += a - x - 1;
    res %= mod;
    while (t.right[x] >= 0) {
      rotate_left(t, x);
      ++x;
    }
  }
  if (x == b) return res;
  res += flatten_left(t, x, b);
  res %= mod;
  return res;
}

int transform(Tree& t1, Tree& t2, int r1, int r2)
{
  if (r1 < 0 || r2 < 0) {
    assert(r1 < 0 && r2 < 0);
    return 0;
  }
  if (r1 > r2) return transform(t2, t1, r2, r1);
  int res = 0;
  if (r1 < r2) {
    res += flatten_right(t1, r1, r2);
    res %= mod;
    res += flatten_left(t2, r2, r1);
    res %= mod;
    res += r2 - r1;
    res %= mod;
  }
  res += transform(t1, t2, t1.left[r1], t2.left[r1]);
  res %= mod;
  res += transform(t1, t2, t1.right[r2], t2.right[r2]);
  res %= mod;
  return res;
}

int transform(Tree t1, Tree t2)
{
  return transform(t1, t2, t1.root, t2.root);
}

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;

  Tree t1;
  Tree t2;

  t1.read(n);
  t2.read(n);

  cout << transform(move(t1), move(t2)) << '\n';

  return 0;
}