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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <bits/stdc++.h>
using namespace std;

typedef long long int LL;

const LL M = 1000000007LL;

struct BST {
  int root;
  BST* parent;
  BST* left;
  BST* right;

  BST(int _root, BST* _parent) {
    root = _root; parent = _parent; left = right = nullptr;
  }

  BST(int _root, BST* _parent, BST* _left, BST* _right) {
    root = _root; parent = _parent; left = _left; right = _right;
  }

  void construct(const vector<vector<int>>& child) {
    for (int x : child[root]) {
      BST* child_node = new BST(x, this);
      if (x < root) left = child_node;
      if (x > root) right = child_node;
      child_node->construct(child);
    }
  }

  BST(const vector<int>& seq) {
    vector<vector<int>> child(seq.size() + 1);
    for (int i = 0; i < seq.size(); i++)
      child[max(0, seq[i])].push_back(i + 1);
    *this = BST(child[0][0], nullptr);
    construct(child);
  }

  void debug_print(bool from_root = false) {
    if (from_root) {
      BST* node = this;
      while (node->parent != nullptr) node = node->parent;
      node->debug_print();
      cerr << "\n";
      return;
    }

    cerr << "BST(root = " << root;
    cerr << ", parent = ";
    if (parent != nullptr) cerr << parent->root; else cerr << "nullptr";
    cerr << ", left = ";
    if (left != nullptr) cerr << left->root; else cerr << "nullptr";
    cerr << ", right = ";
    if (right != nullptr) cerr << right->root; else cerr << "nullptr";
    cerr << ")\n";
    if (left != nullptr) left->debug_print();
    if (right != nullptr) right->debug_print();
  }

  void left_rotate() {
    BST* v = this;
    BST* w = right;
    //cerr << "running left rotate on " << v->root << "\n";
    assert(w->left == nullptr);

    if (parent != nullptr) {
      if ((parent->right != nullptr) && (parent->right->root == v->root))
        v->parent->right = w;
      else
        v->parent->left = w;
    }
    w->parent = v->parent;
    v->parent = w;
    w->left = v;
    v->right = nullptr;

    //debug_print(true);
  }

  void right_rotate() {
    BST* v = this;
    BST* w = left;
    //cerr << "running right rotate on " << v->root << "\n";
    assert(w->right == nullptr);

    if (parent != nullptr) {
      if ((parent->right != nullptr) && (parent->right->root == v->root))
        v->parent->right = w;
      else
        v->parent->left = w;
    }
    w->parent = v->parent;
    v->parent = w;
    w->right = v;
    v->left = nullptr;

    //debug_print(true);
  }

  BST* find(int k) {
    if (k < root) return (left != nullptr) ? left->find(k) : nullptr;
    if (k > root) return (right != nullptr) ? right->find(k) : nullptr;
    return this;
  }

  LL pathify(int min_root, int max_root) {
    LL result = 0;

    if ((left != nullptr) && (root > min_root))
      result += left->pathify(min(root, min_root), max(root, max_root));
    if ((right != nullptr) && (root < max_root))
      result += right->pathify(min(root, min_root), max(root, max_root));

    bool shift_left = (parent == nullptr) || (root < parent->root);
    BST* go_node = this;
    if (shift_left) {
      while (go_node->right != nullptr) {
        go_node->left_rotate(); result++; go_node = go_node->parent;
      }
    }
    else {
      while (go_node->left != nullptr) {
        go_node->right_rotate(); result++; go_node = go_node->parent;
      }
    }
    return result;
  }

  LL transform(BST* other) {
    //cerr << "transforming my_root " << root << " into other_root " << other->root << "\n";

    LL result = 0;
    BST* go_node = this;
    if (other->root < root) {
      //cerr << "running pathify (" << other->root << ", " << root << ")\n";
      result += left->pathify(other->root, root);
      while (go_node->root != other->root) {
        go_node->right_rotate(); result++; go_node = go_node->parent;
      }
    }
    else if (other->root > root) {
      //cerr << "running pathify (" << root << ", " << other->root << ")\n";
      result += right->pathify(root, other->root);
      while (go_node->root != other->root) {
        go_node->left_rotate(); result++; go_node = go_node->parent;
      }
    }
    //cerr << "now root " << other->root << " is satisfied!\n";
    if (other->left != nullptr) {
      //cerr << "we need to satisfy " << other->left->root << "\n";
      if (go_node->left != nullptr)
        result += go_node->left->transform(other->left);
      else {
        assert(false);
      }
    }
    if (other->right != nullptr) {
      //cerr << "we need to satisfy " << other->right->root << "\n";
      if (go_node->right != nullptr)
        result += go_node->right->transform(other->right);
      else {
        assert(false);
      }
    }
    return result;
  }
};






int main() {
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> seq1(n); for (int i = 0; i < n; i++) cin >> seq1[i];
  vector<int> seq2(n); for (int i = 0; i < n; i++) cin >> seq2[i];

  BST tree1(seq1);
  BST tree2(seq2);

  /*
  cerr << "tree1:\n";
  tree1.debug_print();
  cerr << "tree2:\n";
  tree2.debug_print();
  cerr << "\n\n";
  */

  cout << tree1.transform(&tree2) % M << "\n";

  /*
  cerr << "final state of tree1:\n";
  BST* t1 = &tree1;
  while (t1->parent != nullptr) t1 = t1->parent;
  t1->debug_print();
  */

  return 0;
}