import java.lang.Math

val MOD: Int = 1000000000 + 7
val MODL: Long = MOD.toLong()

var fact = mutableListOf<Int>()

fun main(args: Array<String>)
{
    val n = read().toInt()
    calcFact(n)
    val p = read().split(" ").map{it.toInt()}.toIntArray()
    val q = read().split(" ").map{it.toInt()}.toIntArray()

    var a = 1
    for (i in 0 until n) {
        a += (p[i] - 1) * fact[n-1-i]
        for (k in i+1 until n) {
            if (p[k] > p[i]) p[k] -= 1
        }
    }

    var b = 1
    for (i in 0 until n) {
        b += (q[i] - 1) * fact[n-1-i]
        for (k in i+1 until n) {
            if (q[k] > q[i]) q[k] -= 1
        }
    }

    println(Math.abs(a-b))
}

fun calcFact(n: Int): Int {
    if (n == 0) {
        fact.add(1)
        return 1
    }

    val f = n * calcFact(n-1)
    fact.add(f)
    return f
}

fun read(): String = readLine()!!
