归并排序+离散化+映射
//归并排序+离散化+映射
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int a[N], b[N], p[N], c[N];
int n;
const int mod = 99999997;
//离散化
void work(int a[]) {
for (int i = 1; i <= n; i++)p[i] = i;
sort(p + 1, p + n + 1, [&](int x, int y) {
return a[x] < a[y];
});
for (int i = 1; i <= n; i++)a[p[i]] = i;
}
ll merge(int l, int r) {
if (l >= r)return 0;
int mid = l + r >> 1;
ll res = (merge(l, mid) + merge(mid + 1, r)) % mod;
int k = 0, i = l, j = mid + 1;
while (i <= mid && j <= r) {
if (b[i] <= b[j])p[k++] = b[i++];
else {
p[k++] = b[j++];
res = (res + mid - i + 1) % mod;
}
}
while (i <= mid)p[k++] = b[i++];
while (j <= r)p[k++] = b[j++];
for (int i = l, j = 0; i <= r; i++, j++)b[i] = p[j];
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = 1; i <= n; i++)cin >> b[i];
work(a), work(b);
for (int i = 1; i <= n; i++)c[a[i]] = i;
for (int i = 1; i <= n; i++)b[i] = c[b[i]];
cout << merge(1, n);
return 0;
}