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
#include <cstdio>
#include <vector>

const int max_n = 500010;
const int mod = 1000000007;

int n;
int root_a, root_b;
int T_A[max_n][2];
int T_B[max_n][2];


long long compute_cost(int curr, int exp);
std::pair<long long, int> extend_right(int curr);
long long compute_cost_right_seg(int curr, int begin, int end, int exp);

std::pair<long long, int> extend_left(int curr) {
    int cnt = 1;
    long long cost = 0;
    if (T_A[curr][0] != -1) {
        std::pair<long long, int> r1 = extend_left(T_A[curr][0]);
        cnt += r1.second;
        cost += r1.first;
    }
    if (T_A[curr][1] != -1) {
        std::pair<long long, int> r2 = extend_right(T_A[curr][1]);
        cnt += r2.second;
        cost += r2.first;
        cost += r2.second;
    }
    return std::make_pair(cost % mod, cnt);
}

std::pair<long long, int> extend_right(int curr) {
    int cnt = 1;
    long long cost = 0;
    if (T_A[curr][0] != -1) {
        std::pair<long long, int> r1 = extend_left(T_A[curr][0]);
        cnt += r1.second;
        cost += r1.first;
        cost += r1.second;
    }
    if (T_A[curr][1] != -1) {
        std::pair<long long, int> r2 = extend_right(T_A[curr][1]);
        cnt += r2.second;
        cost += r2.first;
    }
    return std::make_pair(cost % mod, cnt);
}

long long compute_cost_left_seg(int curr, int begin, int end, int exp) {
//    printf ("compute_cost_left_seg %d %d %d %d\n", curr, begin, end, exp);
    if (exp == end) {
        if (begin == end) return compute_cost(curr, T_B[exp][0]);
        else return compute_cost_left_seg(curr, begin, end - 1, T_B[exp][0]);
    } else if (begin < exp) {
        long long ret = end - exp;
        ret += compute_cost_right_seg(-1, exp + 1, end, T_B[exp][1]);
        ret += compute_cost_left_seg(curr, begin, exp - 1, T_B[exp][0]);
        return ret % mod;
    } else if (begin == exp) {
        long long ret = end - exp;
        ret += compute_cost_right_seg(-1, exp + 1, end, T_B[exp][1]);
        ret += compute_cost(curr, T_B[exp][0]);
        return ret % mod;        
    } else {
        long long ret = compute_cost_right_seg(-1, exp + 1, end, T_B[exp][1]);        
        int first_right = curr;
        if (T_A[first_right][1] != -1) {
            std::pair<long long, int> r1 = extend_right(T_A[first_right][1]);
            ret += r1.second + r1.first;
        }
        while (first_right > exp) {
            first_right = T_A[first_right][0];
            if (T_A[first_right][1] != -1) {
                std::pair<long long, int> r1 = extend_right(T_A[first_right][1]);
                ret += r1.second + r1.first;
            }
        }
        ret += end - exp;
        if (exp == first_right) ret += compute_cost(T_A[first_right][0], T_B[exp][0]);
        else ret += compute_cost_left_seg(T_A[first_right][0], first_right, exp - 1, T_B[exp][0]);

        return ret % mod;
    }
}

long long compute_cost_right_seg(int curr, int begin, int end, int exp) {
//    printf ("compute_cost_right_seg %d %d %d %d\n", curr, begin, end, exp);
    if (exp == begin) {
        if (begin == end) return compute_cost(curr, T_B[exp][1]);
        else return compute_cost_right_seg(curr, begin + 1, end, T_B[exp][1]);
    } else if (exp < end) {
        long long ret = exp - begin;
        ret += compute_cost_left_seg(-1, begin, exp - 1, T_B[exp][0]);
        ret += compute_cost_right_seg(curr, exp + 1, end, T_B[exp][1]);
        return ret % mod;
    } else if (end == exp) {
        long long ret = exp - begin;
        ret += compute_cost_left_seg(-1, begin, exp - 1, T_B[exp][0]);
        ret += compute_cost(curr, T_B[exp][1]);
        return ret % mod;        
    } else {
        long long ret = compute_cost_left_seg(-1, begin, exp - 1, T_B[exp][0]);
//        printf("TST_1 %lld\n", ret);
        int first_left = curr;
        if (T_A[first_left][0] != -1) {
            std::pair<long long, int> r1 = extend_left(T_A[first_left][0]);
            ret += r1.second + r1.first;
        }
//        printf("TST_2 %lld\n", ret);
        while (first_left < exp) {
            first_left = T_A[first_left][1];
            if (T_A[first_left][0] != -1) {
                std::pair<long long, int> r1 = extend_left(T_A[first_left][0]);
                ret += r1.second + r1.first;
            }
        }
//        printf("TST_3 %lld\n", ret);
        ret += exp - begin;
        if (exp == first_left) ret += compute_cost(T_A[first_left][1], T_B[exp][1]);
        else ret += compute_cost_right_seg(T_A[first_left][1], exp + 1, first_left, T_B[exp][1]);

        return ret % mod;
    }
}

long long compute_cost(int curr, int exp) {
//    printf ("compute_cost: %d %d\n", curr, exp);
    // empty tree.
    if (curr == -1) return 0;

    long long ret = 0;
    if (curr < exp) {
        ret += compute_cost_left_seg(T_A[curr][0], curr, exp - 1, T_B[exp][0]);
        int first_left = curr;
        while (first_left < exp) {
            first_left = T_A[first_left][1];
            if (T_A[first_left][0] != -1) {
                std::pair<long long, int> r1 = extend_left(T_A[first_left][0]);
                ret += r1.second + r1.first;
            }
        }
        if (first_left == exp) ret += compute_cost(T_A[first_left][1], T_B[exp][1]);
        else ret += compute_cost_right_seg(T_A[first_left][1], exp + 1, first_left, T_B[exp][1]);
        ret += exp - curr;
    } else if (curr > exp) {
        ret += compute_cost_right_seg(T_A[curr][1], exp + 1, curr, T_B[exp][1]);
//        printf ("CC_1: %lld\n", ret);
        int first_right = curr;
        while (first_right > exp) {
            first_right = T_A[first_right][0];
            if (T_A[first_right][1] != -1) {
                std::pair<long long, int> r1 = extend_right(T_A[first_right][1]);
                ret += r1.second + r1.first;
            }
        }
        if (first_right == exp) ret += compute_cost(T_A[first_right][0], T_B[exp][0]);
        else ret += compute_cost_left_seg(T_A[first_right][0], first_right, exp - 1, T_B[exp][0]);
        ret += curr - exp;
    } else {  // curr == exp
        ret += compute_cost(T_A[curr][0], T_B[exp][0]);
        ret += compute_cost(T_A[curr][1], T_B[exp][1]);
    }
    return ret % mod;
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        T_A[i][0] = T_A[i][1] = T_B[i][0] = T_B[i][1] = -1;
    }
    for (int i = 1; i <= n; ++i) {
        int in;
        scanf("%d", &in);
        if (in == -1) root_a = i;
        else T_A[in][in < i] = i;
    }
    for (int i = 1; i <= n; ++i) {
        int in;
        scanf("%d", &in);
        if (in == -1) root_b = i;
        else T_B[in][in < i] = i;
    }
//    printf ("%d %d\n", root_a, root_b);
//    for (int i = 1; i <= n; ++i) printf("%d : %d %d\n", i, T_A[i][0], T_A[i][1]);
//    for (int i = 1; i <= n; ++i) printf("%d : %d %d\n", i, T_B[i][0], T_B[i][1]);
//    return 0;
    printf("%lld\n", compute_cost(root_a, root_b));
}