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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <iostream>
#include <queue>

using namespace std;


struct Node
{
    int left = -1;
    int right = -1;
    int parent = -1;
};

Node* tree;
int tree_top;
Node* pattern_tree;
vector<pair<int, int>> correct_nodes_order;
int pattern_tree_top;
int last_not_changed;


void read_tree(int tree_size)
{
    for(int i = 0; i < tree_size; i++)
    {
        int parent;
        cin >> parent;
        parent--;

        if(parent == -2)
        {
            tree_top = i;
            tree[i].parent = -1;
        }
        else
        {
            tree[i].parent = parent;
            if(parent > i)
                tree[parent].left = i;
            else
                tree[parent].right = i;
        }
    }
}

void read_pattern_tree(int tree_size)
{
    for(int i = 0; i < tree_size; i++)
    {
        int parent;
        cin >> parent;
        parent--;

        if(parent == -2)
        {
            pattern_tree_top = i;
            pattern_tree[i].parent = -1;
        }
        else
        {
            pattern_tree[i].parent = parent;
            if(parent > i)
                pattern_tree[parent].left = i;
            else
                pattern_tree[parent].right = i;
        }
    }
}

int get_layer_of_node(int node)
{
//    cout<<"finding layer of "<<node + 1<<" -> ";
    int current_node = tree_top;
    int current_layer = 0;

    while (current_node != node)
    {
        if(current_node > node)
            current_node = tree[current_node].left;
        else
            current_node = tree[current_node].right;
        current_layer++;
    }
//    cout<<current_layer<<endl;
    return current_layer;
}

int find_node_with_child_at_layer(int child_id, int layer)
{
    int current_node = tree_top;
    int current_layer = 0;

    while (current_layer < layer)
    {
        if(current_node > child_id)
            current_node = tree[current_node].left;
        else
            current_node = tree[current_node].right;
        current_layer++;
    }
    return current_node;
}


long long change_sub_tree_to_right_line(int node);
long long change_sub_tree_to_left_line(int node);


long long change_sub_tree_to_right_line(int node)
{
    long long result = 0;
    if(node == -1)
        return result;
    if(tree[node].left != -1)
    {
        result += change_sub_tree_to_left_line(tree[node].left);
        result %= 1000000007;

        result += node - tree[node].parent - 1;
        result %= 1000000007;
    }
    if(tree[node].right != -1)
    {
        result += change_sub_tree_to_right_line(tree[node].right);
        result %= 1000000007;
    }

    return result;

}

long long change_sub_tree_to_left_line(int node)
{
    long long result = 0;
    if(node == -1)
        return result;
    if(tree[node].left != -1)
    {
        result += change_sub_tree_to_left_line(tree[node].left);
        result %= 1000000007;
    }
    if(tree[node].right != -1)
    {
        result += change_sub_tree_to_right_line(tree[node].right);
        result %= 1000000007;

        result += tree[node].parent - node - 1;
        result %= 1000000007;
    }

    return result;
}

long long swap_two_nodes(int node_child, int node_parent)
{
    long long result = 0;
    if(node_child > node_parent)
    {

        int child = tree[node_parent].right;
        result += change_sub_tree_to_left_line(tree[child].left);
        result += child - tree[child].parent - 1;
        result %= 1000000007;
        while(child < node_child)
        {
            child = tree[child].right;
            if(tree[child].left != -1)
            {
                result += change_sub_tree_to_left_line(tree[child].left);
                result += child - tree[child].parent - 1;
                result %= 1000000007;
            }
        }
        last_not_changed = child;
        result += node_child - node_parent;
    }
    else
    {
        int child = tree[node_parent].left;
        result += change_sub_tree_to_right_line(tree[child].right);
//        cout<<"after changing to right line: "<<result<<endl;
        result += tree[child].parent - child - 1;
        result %= 1000000007;
        while(child > node_child)
        {
            child = tree[child].left;
            if(tree[child].right != -1)
            {
                result += change_sub_tree_to_right_line(tree[child].right);
                result += tree[child].parent - child - 1;
                result %= 1000000007;
            }
        }
        result += node_parent - node_child;
        last_not_changed = child;
    }
    result %= 1000000007;

//    cout<<"changing node_child( "<<node_child<<" ) with node_parent( "<<node_parent
//    <<" and result: "<<result<<endl;
    return result;
}

void change_tree(int from, int to, int new_top)
{
//    cout<<"from:"<<from<<" to:"<<to<<" new_top:"<<new_top<<endl;
    if(new_top < to)
    {
        tree[new_top].parent = tree[to].parent;
        if(tree[new_top].parent == -1)
            tree_top = new_top;
        else
        {
            if(tree[new_top].parent > new_top)
                tree[tree[new_top].parent].left = new_top;
            else
                tree[tree[new_top].parent].right = new_top;
        }

        //--------------------------------------------------------------
        if(from == new_top)
        {
            tree[new_top].right = new_top + 1;
        }
        else
        {
            tree[new_top].right = new_top + 1;
            tree[new_top].left = new_top - 1;
            tree[from].parent = from + 1;
            tree[from].right = -1;
            for(int i = from + 1; i < new_top; i++)
            {
                tree[i].right = -1;
                tree[i].left = i - 1;
                tree[i].parent = i + 1;
            }
        }

        for(int i = new_top + 1; i < to; i++)
        {
            tree[i].parent = i - 1;
            tree[i].left = -1;
            tree[i].right = i + 1;
        }
        tree[to].parent = to - 1;
        tree[to].left = -1;
    }
    else
    {
        tree[new_top].parent = tree[to].parent;
        if(tree[new_top].parent == -1)
            tree_top = new_top;
        else
        {
            if(tree[new_top].parent > new_top)
                tree[tree[new_top].parent].left = new_top;
            else
                tree[tree[new_top].parent].right = new_top;
        }

        tree[to].parent = to + 1;
        tree[to].right = -1;

        for(int i = to + 1; i < new_top; i++)
        {
            tree[i].right = -1;
            tree[i].left = i - 1;
            tree[i].parent = i + 1;
        }
        if(new_top != from)
        {
            tree[new_top].right = new_top + 1;
            tree[from].parent = from - 1;
            tree[from].left = -1;
        }


        tree[new_top].left = new_top - 1;

        for(int i = new_top + 1; i < from; i++)
        {
            tree[i].parent = i - 1;
            tree[i].left = -1;
            tree[i].right = i + 1;
        }
    }

//    cout<<"tree top: "<<tree_top + 1<<endl;
//    for(int i = 0; i < correct_nodes_order.size(); i++)
//    {
//        cout<<"node nr "<<i + 1<<" parent: "<<tree[i].parent + 1<<" left: "<<tree[i].left + 1<<" right: "<<tree[i].right + 1<<endl;
//    }

}

long long calculate()
{
    queue<pair<int, int>> queue;
    queue.push({pattern_tree_top, 0});

    while (!queue.empty())
    {
        pair<int, int> next = queue.front();
        queue.pop();
        correct_nodes_order.push_back(next);
        if(pattern_tree[next.first].left != -1)
        {
            queue.push({pattern_tree[next.first].left, next.second + 1});
        }
        if(pattern_tree[next.first].right != -1)
            queue.push({pattern_tree[next.first].right, next.second + 1});
    }

//    for(auto & i : correct_nodes_order)
//        cout<<"vertex: "<<i.first + 1 << " at layer "<<i.second<<endl;
//
    long long result = 0;
    for(auto & i : correct_nodes_order)
    {
//        cout<<i.first<<" : "<<i.second<<endl;
        if(i.second != get_layer_of_node(i.first))
        {
            int swapping_with = find_node_with_child_at_layer(i.first, i.second);
//            cout << "swaping " <<i.first + 1 << " with " << swapping_with + 1 << "     "<<endl;
            result += swap_two_nodes(i.first, swapping_with);
//            cout << "change tree from " <<last_not_changed + 1 << " to " << swapping_with + 1 << endl;
            change_tree(last_not_changed, swapping_with, i.first);
            result %= 1000000007;
//            cout<< "    result after swapping: "<<result<<endl;
        }
    }
    return result;
}


int main()
{
    ios_base::sync_with_stdio(false);
    int tree_size;
    cin>>tree_size;

    tree = new Node[tree_size];
    pattern_tree = new Node[tree_size];

    read_tree(tree_size);
    read_pattern_tree(tree_size);

//    cout<<"tree top: "<<tree_top<<endl;
//    for(int i = 0; i < tree_size; i++)
//    {
//        cout<<"node nr "<<i<<" parent: "<<tree[i].parent<<" left: "<<tree[i].left<<" right: "<<tree[i].right<<endl;
//    }

    cout<<calculate()<<endl;
    return 0;
}