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

using namespace std;

#define f first
#define s second

const int MAXN = 5e5 + 13;
const int MOD = 1e9 + 7;

int roz[MAXN][2];
pair<int, int> t[MAXN][2];
pair<int, int> dp[MAXN][2];
int ans = 0, ind;

int mymod(int &x) {
    if (x >= MOD)
        x -= MOD;
    return x;
}

void DFS(int x) {
    roz[x][ind] = 1;
    int son = t[x][ind].f;
    if (son != 0) {
        DFS(son);
        roz[x][ind] += roz[son][ind];
        dp[x][ind].f += dp[son][ind].f;
        mymod(dp[x][ind].f);
        dp[x][ind].s += dp[son][ind].f;
        mymod(dp[x][ind].s);
        dp[x][ind].s += roz[son][ind];
        mymod(dp[x][ind].s);
    }
    son = t[x][ind].s;
    if (son != 0) {
        DFS(son);
        roz[x][ind] += roz[son][ind];
        dp[x][ind].f += dp[son][ind].s;
        mymod(dp[x][ind].f);
        dp[x][ind].s += dp[son][ind].s;
        mymod(dp[x][ind].s);
        dp[x][ind].f += roz[son][ind];
        mymod(dp[x][ind].f);
    }
}

void licz(int x, int y) {
    if (x == 0 || y == 0)
        return;
    int tmp, rgt, pop;
    if (x > y) {
        tmp = t[x][0].f;
        while (tmp > y) {
            rgt = t[tmp][0].s;
            ans += dp[rgt][0].s;
            mymod(ans);
            ans += roz[rgt][0];
            mymod(ans);
            tmp = t[tmp][0].f;
        }
        rgt = t[tmp][0].s;
        ans += dp[rgt][0].s;
        mymod(ans);
        ans += roz[rgt][0];
        mymod(ans);
        ans += x - y;
        mymod(ans);
        pop = t[tmp][0].f;
        t[y][0].f = y - 1;
        for (int i = y - 1; i > tmp; i--) {
            t[i][0].f = i - 1;
            t[i][0].s = 0;
        }
        if (y != tmp)
            t[tmp][0].s = 0;
        t[tmp][0].f = pop;

        t[y][0].s = y + 1;
        for (int i = y + 1; i != x; i++) {
            t[i][0].f = 0;
            t[i][0].s = i + 1;
        }
        t[x][0].f = 0;
        licz(t[y][0].f, t[y][1].f);
        licz(t[y][0].s, t[y][1].s);
    }
    else if (x < y) {
        swap(x, y);
        tmp = t[x][1].f;
        while (tmp > y) {
            rgt = t[tmp][1].s;
            ans += dp[rgt][1].s;
            mymod(ans);
            ans += roz[rgt][1];
            mymod(ans);
            tmp = t[tmp][1].f;
        }
        rgt = t[tmp][1].s;
        ans += dp[rgt][1].s;
        mymod(ans);
        ans += roz[rgt][1];
        mymod(ans);
        ans += x - y;
        mymod(ans);
        pop = t[tmp][1].f;
        t[y][1].f = y - 1;
        for (int i = y - 1; i > tmp; i--) {
            t[i][1].f = i - 1;
            t[i][1].s = 0;
        }
        if (y != tmp)
            t[tmp][1].s = 0;
        t[tmp][1].f = pop;

        t[y][1].s = y + 1;
        for (int i = y + 1; i != x; i++) {
            t[i][1].f = 0;
            t[i][1].s = i + 1;
        }
        t[x][1].f = 0;
        licz(t[y][0].f, t[y][1].f);
        licz(t[y][0].s, t[y][1].s);
    }
    else {
        licz(t[x][0].f, t[y][1].f);
        licz(t[x][0].s, t[y][1].s);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, x;
    int pocz, kon;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        if (x == -1)
            pocz = i;
        else if (i > x)
            t[x][0].s = i;
        else
            t[x][0].f = i;
    }
    for (int i = 1; i <= n; i++) {
        cin >> x;
        if (x == -1)
            kon = i;
        else if (i > x)
            t[x][1].s = i;
        else
            t[x][1].f = i;
    }
    ind = 0;
    DFS(pocz);
    ind = 1;
    DFS(kon);
    licz(pocz, kon);
    cout << ans;
    return 0;
}