import java.util.*

fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val p = readLine()!!.split(" ").map { it.toInt() }.reversed()
    val q = readLine()!!.split(" ").map { it.toInt() }.reversed()
    val pn = calc(p)
    val qn = calc(q)
//    println(pn)
//    println(qn)
    println(Math.abs(pn - qn))

}

private fun calc(list: List<Int>): Long {
    var n = 0L
    val que = ArrayDeque(list)
    while (que.isNotEmpty()) {
        val i = que.removeLast()
        val size = que.filter { it < i }.size
        val f = size * factorial(que.size)
        // println("$i $que ($size * ${factorial(que.size)})=$f")
        n += f
    }
    return n + 1
}

private fun factorial(n: Int): Long {
    var sum = 1L
    for (i in (1..n)) {
        sum *= i
    }
    return sum
}
