object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt
  val p = Seq.fill(n)(sc.nextInt)
  val q = Seq.fill(n)(sc.nextInt)
  
  def point(seq: Seq[Int], count: Int): Int = {
    if (seq.isEmpty) count
    else {
      val dropped = seq.drop(1)
      val plus = if (seq(0) == 1 || seq.length == 1) 0
        else (dropped.filter(_ < seq(0)).length) * (1 to dropped.length).reduce((a,b) => a*b)
      point(dropped, count + plus)
    }
  }
  
  val a = point(p, 0) + 1
  val b = point(q, 0) + 1
  println(math.abs(a - b))
}