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
#include<cstdio>
#include<algorithm>
#include<vector>
#define S 500007

using namespace std;
typedef long long ll;

ll mod = 1000000007;

int Ro[S],Ro2[S];
int L[S],L2[S];
int P[S],P2[S];

int roz[S];
int roz2[S];

ll odp = 0;


///Tutaj tylko policzymy koszt rozbicia, trzeba jeszcze zmienić graf
void rozbij(int x, int side){
    if(x == 0)
        return;
    rozbij(L[x],1);
    rozbij(P[x],0);
    if(side == 1){
        odp += roz[P[x]];
    }else{
        odp += roz[L[x]];
    }
}

void rozbij2(int x, int side){
    if(x == 0)
        return;
    rozbij2(L2[x],1);
    rozbij2(P2[x],0);
    if(side == 1){
        odp += roz2[P2[x]];
    }else{
        odp += roz2[L2[x]];
    }
}

vector < int > V;
void DFS(int x){
    roz[x] = 1;
    if(L[x] != 0){
        DFS(L[x]);
        roz[x] += roz[L[x]];
    }
    V.push_back(x);
    if(P[x] != 0){
        DFS(P[x]);
        roz[x] += roz[P[x]];
    }
}

void DFS2(int x){
    roz2[x] = 1;
    if(L2[x] != 0){
        DFS2(L2[x]);
        roz2[x] += roz2[L2[x]];
    }
    V.push_back(x);
    if(P2[x] != 0){
        DFS2(P2[x]);
        roz2[x] += roz2[P2[x]];
    }
}

void F(int root, int root2){
    if(root == 0)
        return;
    while(root != root2){
        odp %= mod;
        if(root > root2){
            ///Teraz chcemy przesunąć raz w lewo
            if(P[L[root]] == 0){
                int temp = root;
                root = L[root];
                P[root] = temp;
                L[temp] = 0;
                odp++;
            }else{
                DFS(P[L[root]]);
                rozbij(L[root],1);
                ///Trzeba tu modyfikować graf z wektora V
                int temp = L[root];
                P[temp] = 0;
                L[root] = V[V.size()-1];
                for(int i = V.size()-1; i >= 1;i--){
                    int v = V[i];
                    P[v] = 0;
                    L[v] = V[i-1];
                }
                P[V[0]] = 0;
                L[V[0]] = temp;
                V.clear();
            }
        }else{
            if(P2[L2[root2]] == 0){
                int temp = root2;
                root2 = L2[root2];
                P2[root2] = temp;
                L2[temp] = 0;
                odp++;
            }else{
                DFS2(P2[L2[root2]]);
                rozbij2(L2[root2],1);
                int temp = L2[root2];
                P2[temp] = 0;
                L2[root2] = V[V.size()-1];
                for(int i = V.size()-1; i >= 1;i--){
                    int v = V[i];
                    P2[v] = 0;
                    L2[v] = V[i-1];
                }
                P2[V[0]] = 0;
                L2[V[0]] = temp;
                V.clear();
            }
        }

    }
    F(L[root],L2[root2]);
    F(P[root],P2[root2]);
}

int main(void){
    int n;
    scanf("%d",&n);
    int root, root2;
    for(int i = 1; i <= n;i++){
        scanf("%d",&Ro[i]);
        if(Ro[i] == -1){
            root = i;
        }else if(i < Ro[i]){
            L[Ro[i]] = i;
        }else{
            P[Ro[i]] = i;
        }
    }
    for(int i = 1; i <= n;i++){
        scanf("%d",&Ro2[i]);
        if(Ro2[i] == -1){
            root2 = i;
        }else if(i < Ro2[i]){
            L2[Ro2[i]] = i;
        }else{
            P2[Ro2[i]] = i;
        }
    }
    F(root,root2);
    odp %= mod;
    printf("%lld",odp);



    return 0;
}