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
#include <bits/stdc++.h>
#include <iostream>
#include <vector>

using namespace std;
#define PPP 1000000007
int N, parent;

struct Node {
    int v;
    Node *p = NULL;
    Node *l = NULL;
    Node *r = NULL;

    Node() : v(0), p(NULL), l(NULL), r(NULL) {}

    static Node *readCinAndGetRoot(vector<Node> &t) {
        Node *root = NULL;
        for (int i = 1; i <= N; ++i) {
            cin >> parent;
            t[i].v = i;
            if (parent == -1) {
                root = &t[i];
                continue;
            }
            t[i].p = &t[parent];
            if (parent > i)
                t[parent].l = &t[i];
            else if (parent < i)
                t[parent].r = &t[i];
            else
                throw "dupa";
        }
        return root;
    }
};

vector<Node> t1;
vector<Node> t2;
queue<pair<Node *, Node *>> bfsQ;

struct AfterCut {
    Node *subbiggerFromBigger = NULL;
    Node *subbiggerFromSmaller = NULL;
    Node *subsmallerFromBigger = NULL;
    Node *subsmallerFromSmaller = NULL;
};

long long howManyToV(Node *root) {
    queue<Node*> q;
    long long all = 0;
    q.push(root);
    while (!q.empty())
    {
        auto elem = q.front();
        q.pop();
        if (elem->l) {
            all += elem->v - elem->l->v - 1;
            q.emplace(elem->l);
        }
        if (elem->r) {
            all += elem->r->v - elem->v - 1;
            q.emplace(elem->r);
        }
        all %= PPP;
    }
    return all;
}


long long howManyToVAndShift(Node *root1, Node *root2) {
    if (!root1 || !root2) throw "dupa666";
    long long int rootDiffLen = root1->v - root2->v;
    if (rootDiffLen < 0) rootDiffLen = -rootDiffLen;
    return howManyToV(root1) % PPP + howManyToV(root2) % PPP + rootDiffLen;
}

AfterCut cut(Node *lRoot, Node *rRoot) {
    Node *smallerRoot, *biggerRoot, *actualInSmaller, *actualInBigger,
            *subbiggerFromBigger, *subbiggerFromSmaller, *subsmallerFromBigger,
            *subsmallerFromSmaller;
    int biggerToFind, smallerToFind;
    if (lRoot == NULL || rRoot == NULL)
        throw "dupa1";
    if (lRoot == rRoot)
        throw "dupa3";
    if (lRoot->v == rRoot->v) {
        if (lRoot->l != NULL) lRoot->l->p = NULL;
        if (lRoot->r != NULL) lRoot->r->p = NULL;
        if (rRoot->l != NULL) rRoot->l->p = NULL;
        if (rRoot->r != NULL) rRoot->r->p = NULL;
        auto ret = AfterCut{rRoot->r, lRoot->r, rRoot->l, lRoot->l};
        lRoot->l = NULL;
        lRoot->r = NULL;
        rRoot->l = NULL;
        rRoot->r = NULL;
        return ret;
    }

    actualInSmaller = smallerRoot = rRoot->v < lRoot->v ? rRoot : lRoot;
    actualInBigger = biggerRoot = rRoot->v > lRoot->v ? rRoot : lRoot;
    biggerToFind = biggerRoot->v;
    while (true)
    { // w prawo jazda do pierwszego wiekszego=
        while (actualInSmaller->v < biggerToFind) actualInSmaller = actualInSmaller->r; // jazda w prawo
        if (actualInSmaller->v == biggerToFind) break;
        biggerToFind = actualInSmaller->v; // poprawienie w wiekszym drzewie na prawo od korzenia musi byc, bo jest jeszcze wiekszy
        while (actualInBigger->v < biggerToFind) actualInBigger = actualInBigger->r;
        if (actualInBigger->v == biggerToFind) break;
        biggerToFind = actualInBigger->v;
    }

    subbiggerFromBigger = actualInBigger->r;
    subbiggerFromSmaller = actualInSmaller->r;
    // w prawo jazda do pierwszego wiekszego
    actualInSmaller = smallerRoot;// = rRoot->v < lRoot->v ? rRoot : lRoot;
    actualInBigger = biggerRoot;// = rRoot->v > lRoot->v ? rRoot : lRoot;
    smallerToFind = smallerRoot->v;
    while (true)
    { // w lewo jazda do pierwszego wiekszego=
        while (actualInBigger->v > smallerToFind) actualInBigger = actualInBigger->l; // jazda w lewo do mniejszych
        if (actualInBigger->v == smallerToFind) break;
        smallerToFind = actualInBigger->v; // poprawienie w wiekszym drzewie na lewo od korzenia musi byc, bo jest jeszcze miejszy
        while (actualInSmaller->v > smallerToFind) actualInSmaller = actualInSmaller->l;
        if (actualInSmaller->v == smallerToFind) break;
        smallerToFind = actualInSmaller->v;
    }
    subsmallerFromBigger = actualInBigger->l;
    subsmallerFromSmaller = actualInSmaller->l;
    Node *subbiggerFromBiggerParent = subbiggerFromBigger != NULL ? subbiggerFromBigger->p : NULL;
    Node *subbiggerFromSmallerParent = subbiggerFromSmaller != NULL ? subbiggerFromSmaller->p : NULL;
    Node *subsmallerFromBiggerParent = subsmallerFromBigger != NULL ? subsmallerFromBigger->p : NULL;
    Node *subsmallerFromSmallerParent = subsmallerFromSmaller != NULL ? subsmallerFromSmaller->p : NULL;
    if (subbiggerFromBiggerParent) subbiggerFromBigger->p = subbiggerFromBiggerParent->r = NULL;
    if (subbiggerFromSmallerParent) subbiggerFromSmaller->p = subbiggerFromSmallerParent->r = NULL;
    if (subsmallerFromBiggerParent) subsmallerFromBigger->p = subsmallerFromBiggerParent->l = NULL;
    if (subsmallerFromSmallerParent) subsmallerFromSmaller->p = subsmallerFromSmallerParent->l = NULL;
    return AfterCut{subbiggerFromBigger, subbiggerFromSmaller, subsmallerFromBigger, subsmallerFromSmaller};
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N;
    t1.resize(N + 1);
    t2.resize(N + 1);
    Node *root1 = Node::readCinAndGetRoot(t1);
    Node *root2 = Node::readCinAndGetRoot(t2);

    bfsQ.emplace(root1, root2);
    long long wholeSum = 0;
    while (!bfsQ.empty()) {
        auto nextPair = bfsQ.front();
        bfsQ.pop();
        auto afterCut = cut(nextPair.first, nextPair.second);
        wholeSum = (wholeSum + howManyToVAndShift(nextPair.first, nextPair.second)) % PPP;
        if (afterCut.subbiggerFromBigger != NULL) {
            if (afterCut.subbiggerFromSmaller == NULL) throw "dupa5";
            bfsQ.emplace(afterCut.subbiggerFromBigger, afterCut.subbiggerFromSmaller);
        }
        if (afterCut.subsmallerFromBigger != NULL) {
            if (afterCut.subsmallerFromSmaller == NULL) throw "dupa7";
            bfsQ.emplace(afterCut.subsmallerFromBigger, afterCut.subsmallerFromSmaller);
        }
    }
    cout << wholeSum << endl;
    return 0;
}