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

using namespace std;

int p = 1000000007;
int parent[500001];
int leftSon[500001];
int rightSon[500001];
int parent2[500001];
int leftSon2[500001];
int rightSon2[500001];
int root;
int root2;
queue<tuple<int,int,int>> unfoldingCostQueue;
queue<tuple<int,int,int>> unfoldingCostQueue2;
queue<pair<int,int>> rootsToBeSolved;

int findLeftRange(int currentLeftRange, int toBeFound, int* leftSons)
{
    while(currentLeftRange>toBeFound)
    {
        currentLeftRange = leftSons[currentLeftRange];
    }
    return currentLeftRange;
}

int findRightRange(int currentRightRange, int toBeFound, int* rightSons)
{
    while(currentRightRange<toBeFound)
    {
        currentRightRange = rightSons[currentRightRange];
    }
    return currentRightRange;
}

int UnfoldingCost(int a, int b, int current)
{
    int sum = 0;
    if(rightSon[current]!=0)
        unfoldingCostQueue.push(make_tuple(current, rightSon[current], rightSon[current]));
    if(leftSon[current]!=0)
        unfoldingCostQueue.push(make_tuple(leftSon[current], current, leftSon[current]));
    //cerr<<"unfolding "<<a<<" "<<b<<" "<<current<<" "<<sum<<" "<<b-a-1<<endl;
    return b - a - 1 + sum;
}

int UnfoldingCost2(int a, int b, int current)
{
    int sum = 0;
    if(rightSon2[current]!=0)
        unfoldingCostQueue2.push(make_tuple(current, rightSon2[current], rightSon2[current]));
    if(leftSon2[current]!=0)
        unfoldingCostQueue2.push(make_tuple(leftSon2[current], current, leftSon2[current]));
    //cerr<<"unfolding "<<a<<" "<<b<<" "<<current<<" "<<sum<<" "<<b-a-1<<endl;
    return b - a - 1 + sum;
}

pair<int,int> UnfoldingRange(int r,int r2)
{
    //ROOT UNFOLDING

    int currentLeftRange = r;
    int currentRightRange = r;
    int currentLeftRange2 = r2;
    int currentRightRange2 = r2;
    currentLeftRange = findLeftRange(currentLeftRange,currentLeftRange2,leftSon);
    currentLeftRange2 = findLeftRange(currentLeftRange2,currentLeftRange,leftSon2);
    currentRightRange = findRightRange(currentRightRange,currentRightRange2,rightSon);
    currentRightRange2 = findRightRange(currentRightRange2,currentRightRange,rightSon2);
    //cerr<<"left ranges:"<<currentLeftRange<<" "<<currentLeftRange2<<endl;
    //cerr<<"right ranges:"<<currentRightRange<<" "<<currentRightRange2<<endl;
    while(currentLeftRange!=currentLeftRange2)
    {
        currentLeftRange = findLeftRange(currentLeftRange,currentLeftRange2,leftSon);
        currentLeftRange2 = findLeftRange(currentLeftRange2,currentLeftRange,leftSon2);
    }
    while(currentRightRange!=currentRightRange2)
    {
        currentRightRange = findRightRange(currentRightRange,currentRightRange2,rightSon);
        currentRightRange2 = findRightRange(currentRightRange2,currentRightRange,rightSon2);
    }
    return make_pair(currentLeftRange,currentRightRange);
}

int main()
{

    //std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();

    int n;
    int a;

    //DATA READING

    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        if(a==-1)
            root = i;
        parent[i]=a;
        if(a>i)
        {
            leftSon[a]=i;
        }
        else
        {
            rightSon[a]=i;
        }
    }
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        if(a==-1)
            root2 = i;
        parent2[i]=a;
        if(a>i)
        {
            leftSon2[a]=i;
        }
        else
        {
            rightSon2[a]=i;
        }
    }
    
    int sum = 0;
    //cerr<<"roots "<<root<<" "<<root2<<endl;
    
    rootsToBeSolved.push(make_pair(root,root2));
    while(!rootsToBeSolved.empty())
    {
        pair<int, int> currentRoots = rootsToBeSolved.front();
        rootsToBeSolved.pop();
        pair<int, int> range = UnfoldingRange(currentRoots.first,currentRoots.second);
        //cerr<<"roots "<<currentRoots.first<<" "<<currentRoots.second<<", range: "<<range.first<<" "<<range.second<<endl;
        int current = currentRoots.first;
        //cerr<<"unfolding first root LEFT"<<endl;
        while(current>range.first)
        {
            current = leftSon[current];
            if(rightSon[current]!=0)
                sum = (sum + UnfoldingCost(current, rightSon[current],rightSon[current])) % p;
            sum = (sum + parent[current]-current-1) % p;
        }
        current = currentRoots.second;
        //cerr<<"unfolding second root LEFT"<<endl;
        while(current>range.first)
        {
            current = leftSon2[current];
            if(rightSon2[current]!=0)
                sum = (sum + UnfoldingCost2(current, rightSon2[current], rightSon2[current])) % p;
            sum = (sum + parent2[current]-current-1) % p;
        }
        current = currentRoots.first;
        //cerr<<"unfolding first root RIGHT"<<endl;

        while(current<range.second)
        {
            current = rightSon[current];
            if(leftSon[current]!=0)
                sum = (sum + UnfoldingCost(leftSon[current],current,leftSon[current])) % p;
            sum = (sum + current - parent[current]-1) % p;
        }
        current = currentRoots.second;
        //cerr<<"unfolding second root RIGHT"<<endl;
        while(current<range.second)
        {
            current = rightSon2[current];
            if(leftSon2[current]!=0)
                sum = (sum + UnfoldingCost2(leftSon2[current], current, leftSon2[current])) % p;
            sum = (sum + current - parent2[current]-1) % p;
        }

        sum+=abs(currentRoots.first-currentRoots.second);
        //cerr<<"current sum: "<<sum<<endl;
        if(range.first > 1)//not the left end of tree
        {
            if(leftSon[range.first]!=0 and leftSon2[range.first]!=0)
            {
                ////cerr<<leftSon[range.first]<<" "<<leftSon2[range.first]<<endl;
                rootsToBeSolved.push(make_pair(leftSon[range.first],leftSon2[range.first]));
            }
        }

        if(range.second < n)//not the right end of tree
        {
                
            if(rightSon[range.second]!=0 and rightSon2[range.second]!=0)
            {
                ////cerr<<rightSon[range.second]<<" "<<rightSon2[range.second]<<endl;
                rootsToBeSolved.push(make_pair(rightSon[range.second],rightSon2[range.second]));
            }
        }
    }
    while(!unfoldingCostQueue.empty())
    {
        int a,b,current;
        tie(a,b,current) = unfoldingCostQueue.front();
        unfoldingCostQueue.pop();
        sum = (sum + UnfoldingCost(a,b,current))%p;
    }
    while(!unfoldingCostQueue2.empty())
    {
        int a,b,current;
        tie(a,b,current) = unfoldingCostQueue2.front();
        unfoldingCostQueue2.pop();
        sum = (sum + UnfoldingCost2(a,b,current))%p;
    }
    cout<<sum<<endl;
    //std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();

    //std::cerr << "Time difference = " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "[ms]" << std::endl;
    return 0;
}