object Main extends App {

  val n = io.StdIn.readLine().toInt
  val all = (1 to n).toSet
  val p = io.StdIn.readLine().split(' ').map(_.toInt).toList
  val q = io.StdIn.readLine().split(' ').map(_.toInt).toList

  def fact(n: Int): BigInt = {
    def factAcc(n: Int, acc: BigInt): BigInt = if (n == 0)
      acc
    else
      factAcc(n - 1, acc * n)
    factAcc(n, 1)
  }

  def orderOf(p: List[Int], used: Set[Int], acc: BigInt): BigInt = p match {
    case Nil => acc + 1
    case x :: xs =>
      val less = ((1 until x).toSet -- used).size
      orderOf(xs, used + x, acc + less * fact((all -- used - x).size))
  }

  val answer = p.zip(q).span {
    case (pi, qi) => pi == qi
  } match {
    case (_, Nil) => 0
    case (same, diff) =>
      val used = same.map(_._1).toSet
      val orderP = orderOf(diff.map(_._1), used, 0)
      val orderQ = orderOf(diff.map(_._2), used, 0)
      math.abs((orderP - orderQ).toLong)
  }

  println(answer)
}
