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
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define ULL unsigned long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\n'      // UWAGA
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i)
#define random_shuffle(V) shuffle(V, rng)

#define sim template<class n
sim, class s> ostream & operator << (ostream &p, pair<n, s> x)
{return p << "<" << x.f << ", " << x.s << ">";}
sim> auto operator << (ostream &p, n y) ->
typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type 
{int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";}
void dor() {cerr << endl;}
sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);}
sim, class s> void mini(n &p, s y) {if(p>y) p = y;}
sim, class s> void maxi(n &p, s y) {if(p<y) p = y;}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif 

#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "

mt19937_64 rng((unsigned long long)chrono::steady_clock::now().time_since_epoch().count() + 2077);

const int MXN = 500 * 1000 + 5;
const int mod = 1000 * 1000 * 1000 + 7;
int n;
int l[MXN];
int p[MXN];
int par[MXN];
int l2[MXN];
int p2[MXN];
int par2[MXN];
ULL hasz[MXN];
ULL hasz2[MXN];
ULL H[MXN];
int siz[MXN];
int siz2[MXN];
int siz3[MXN];
LL result = 0;


ULL calch(int v)
  {
  if(v == 0)return 0;
  hasz[v] = calch(l[v]) ^ calch(p[v]) ^ H[v];
  siz[v] = siz[l[v]] + siz[p[v]] + 1;
  return hasz[v];
  }
ULL calch2(int v)
  {
  if(v == 0)return 0;
  hasz2[v] = calch2(l2[v]) ^ calch2(p2[v]) ^ H[v];
  siz2[v] = siz2[l2[v]] + siz2[p2[v]] + 1;
  return hasz2[v];
  }

void process(int v)
  {
  if(v == 0)return;
  process(l[v]);
  process(p[v]);
  siz3[v] = 1 + siz3[l[v]] + siz3[p[v]];
  if(l[par[v]] == v) // jeśli jestem lewym synem, to dodaje koszt prawego poddrzewa
    result += siz3[p[v]];
  else
    result += siz3[l[v]];
  }
void process2(int v)
  {
  if(v == 0)return;
  process2(l2[v]);
  process2(p2[v]);
  siz3[v] = 1 + siz3[l2[v]] + siz3[p2[v]];
  if(l2[par2[v]] == v) // jeśli jestem lewym synem, to dodaje koszt prawego poddrzewa
    result += siz3[p2[v]];
  else
    result += siz3[l2[v]];
  }

void add_inside(int v, int a, int b)
  {
  if(v == 0)return;
  if(a < v && v <= b)result++;
  add_inside(l[v], a, b);
  add_inside(p[v], a, b);
  }

void rem_par(int i)
  {
  if(l[par[i]] == i)l[par[i]] = 0;
  else p[par[i]] = 0;
  par[i] = 0;
  }
void rem_par2(int i)
  {
  if(l2[par2[i]] == i)l2[par2[i]] = 0;
  else p2[par2[i]] = 0;
  par2[i] = 0;
  }

VPII roots;
void go(int root, int root2)
  {
  roots.PB(MP(root, root2));
  rem_par(root);
  rem_par2(root2);

  int r = l[root], r2 = l2[root2];
  while(r && r2)
    {
    if(hasz[r] == hasz2[r2]){go(r, r2);break;}
    if(siz[r] > siz2[r2])r = l[r];
    else r2 = l2[r2];
    }

  r = p[root]; r2 = p2[root2];
  while(r && r2)
    {
    if(hasz[r] == hasz2[r2]){go(r, r2);break;}
    if(siz[r] > siz2[r2])r = p[r];
    else r2 = p2[r2];
    }
  }

int main()
  {
  int root = -1, root2 = -1;
  scanf("%d", &n);
  FOR(i, 1, n)
    {
    int x; 
    scanf("%d", &x);
    if(x == -1)
      {
      root = i;
      continue;
      }
    par[i] = x;
    if(i < x)l[x] = i;
    else p[x] = i;
    }
  FOR(i, 1, n)
    {
    int x; 
    scanf("%d", &x);
    if(x == -1)
      {
      root2 = i;
      continue;
      }
    par2[i] = x;
    if(i < x)l2[x] = i;
    else p2[x] = i;
    }
  FOR(i, 1, n)H[i] = rng();
  calch(root);
  calch2(root2);
  go(root, root2);

  for(auto i: roots)
    {
    root = i.f;
    root2 = i.s;
    
    process(l[root]);
    process(p[root]);
    process2(l2[root2]);
    process2(p2[root2]);
    add_inside(root, min(root, root2), max(root, root2));
    }

  printf("%lld\n", result % mod);
  }