#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

// #define ANS  //for answer
#define CMP  //for compe
#ifdef CMP
using namespace std;
#else
#endif

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
#define nv(x) #x, (x)
#define ncv(x) #x, ":", (x)
#define ncvc(x) #x, ":", (x), ","
#define GETIN2MACRO(_1, _2, NAME, ...) NAME
#define GETIN3MACRO(_1, _2, _3, NAME, ...) NAME
#define GETIN4MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define GETIN5MACRO(_1, _2, _3, _4, _5, NAME, ...) NAME
#define GETIN6MACRO(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
#define GETIN7MACRO(_1, _2, _3, _4, _5, _6, _7, NAME, ...) NAME
#define INPUT1(n) \
  ll n;           \
  cin >> n
#define INPUT2(a, b) \
  ll a, b;           \
  cin >> a >> b
#define INPUT3(n, a, b) \
  ll n, a, b;           \
  cin >> n >> a >> b
#define INPUT4(n, m, a, b) \
  ll n, m, a, b;           \
  cin >> n >> m >> a >> b
#define INPUT(...)                                         \
  GETIN4MACRO(__VA_ARGS__, INPUT4, INPUT3, INPUT2, INPUT1) \
  (__VA_ARGS__)
#define INPUTARR(n, a) \
  ll a[n];             \
  REP(i, n) { cin >> a[i]; }
using ll = long long;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
void pPRINT(std::ostream& ost) {}
template <class T, class... U>
void pPRINT(std::ostream& ost, const T t, const U... u) {
  ost << t << " ";
  pPRINT(ost, u...);
}
template <class... T>
void PRINT(const T... t) { pPRINT(std::cout, t...); }
template <class... T>
void PRINTl(const T... t) {
  pPRINT(std::cout, t...);
  std::cout << "\n";
}
template <class T>
void PRINTa(const T* const t, const ll size) {
  REP(i, size)
  PRINT(t[i]);
  PRINTl();
}
template <class T>
void PRINTal(const T* const t, const ll size) {
  REP(i, size)
  PRINTl(i, ":", t[i]);
}
#ifndef DBG
#define DEBUG(...)
void init() {}
#else
#define DEBUG(...)                                                     \
  {                                                                    \
    PRINT("##DEBUG##", "file :", __FILE__, ", line :", __LINE__, "["); \
    PRINT(__VA_ARGS__);                                                \
    PRINTl("]");                                                       \
  }
void init() {
  DEBUG("INIT");
  cin.tie(0);
  ios::sync_with_stdio(false);
}
#endif
#ifdef ANS
#else
#endif

// search max depth
ll dfs(vector<vector<ll>>& tree, ll parent, ll now, ll depth) {
  ll d = depth;
  for (auto i : tree[now]) {
    if (i == parent) continue;
    d = max(d, dfs(tree, now, i, depth + 1));
  }
  return d;
}

void solve();

int main(int argc, char* argv[]) {
  DEBUG("MAIN");
  init();
  solve();

  return 0;
}

void solve() {
  DEBUG("SOLVE START");

  //solve
  INPUT(n);
  INPUTARR(n,p);
  INPUTARR(n,q);

  REP(i,n){
    p[i]--;q[i]--;
  }

  ll sp[n][n],sq[n][n];
  REP(i,n){
    REP(j,n){
     sp[i][j] = p[j]<i;
     sq[i][j] = q[j]<i;
    }
    FOR(j,1,n){
      sp[i][j]+=sp[i][j-1];
      sq[i][j]+=sq[i][j-1];
    }
  }
  ll fp[n-1],fq[n-1];
  REP(i,n-1){
    fp[i]=-sp[p[i]][i]+sp[p[i]][n-1];
    fq[i]=-sq[q[i]][i]+sq[q[i]][n-1];
  }
  #ifdef DBG
  PRINTa(p,n);
  PRINTa(fp,n-1);
  PRINTa(q,n);
  PRINTa(fq,n-1);
  #endif
  ll ans=0;
  REP(i,n-1){
    ans = (ans+fp[i]-fq[i])*(n-i-1);
  }
  PRINT(ans>0?ans:-ans);

  return;
}

Measured memory_bytes: 256