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


using namespace std;


struct Tree {
    int root;
    vector <int> l, r, sub;

    Tree(int root, vector <int> l, vector <int> r): root(root), l(l), r(r) {
        sub = vector <int> (l.size(), 0);
        dfsSub(root);
    }

    int getSub(int x) const {
        return x == -1 ? 0 : sub[x];
    }

    void dfsSub(int v) {
        if (v == -1) {
            return ;
        }

        sub[v] = 1;

        for (int x : {l[v], r[v]}) if (x != -1) {
            dfsSub(x);
            sub[v] += sub[x];
        }
    }
};


Tree readTree(int n) {
    int root = -1;
    vector <int> l(n, -1), r(n, -1);

    for (int i = 0; i < n; i++) {
        int par;
        cin >> par;

        if (par == -1) {
            root = i;
        } else {
            par--;

            if (i < par) {
                l[par] = i;
            } else {
                r[par] = i;
            }
        }
    }

    return Tree(root, l, r);
}


struct Subtree {
    const Tree &t;
    int v, pathLeft, pathRight;

    int getSize() {
        return pathLeft + pathRight + t.getSub(v);
    }

    Subtree getLeft() {
        if (pathLeft) {
            return {t, v, pathLeft - 1, 0};
        } else if (pathRight) {
            return {t, -1, 0, 0};
        } else {
            return {t, t.l[v], 0, 0};
        }
    }

    Subtree getRight() {
        if (pathRight) {
            return {t, v, 0, pathRight - 1};
        } else if (pathLeft) {
            return {t, -1, 0, 0};
        } else {
            return {t, t.r[v], 0, 0};
        }
    }
};


Subtree getFullSubtree(const Tree &t) {
    return {t, t.root, 0, 0};
}


long long solve(Subtree a, Subtree b) {
    int minPathLeft = min(a.pathLeft, b.pathLeft);
    int minPathRight = min(a.pathRight, b.pathRight);

    a.pathLeft -= minPathLeft;
    b.pathLeft -= minPathLeft;

    a.pathRight -= minPathRight;
    b.pathRight -= minPathRight;

    if (a.getSize() <= 1) {
        return 0;
    }

    auto aLeft = a.getLeft();
    auto aRight = a.getRight();

    auto bLeft = b.getLeft();
    auto bRight = b.getRight();

    int diff = aLeft.getSize() - bLeft.getSize();

    if (diff > 0) {
        bLeft.pathLeft += diff;
        aRight.pathRight += diff;
    } else if (diff < 0) {
        aLeft.pathLeft -= diff;
        bRight.pathRight -= diff;
    }

    return abs(diff) + solve(aLeft, bLeft) + solve(aRight, bRight);
}


const int MOD = 1000 * 1000 * 1000 + 7;


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

    int n;
    cin >> n;

    auto a = readTree(n), b = readTree(n);
    long long ans = solve(getFullSubtree(a), getFullSubtree(b));

    ans %= MOD;

    cout << ans << '\n';

    return 0;
}