fun main(args: Array<String>) {
    val n = readLine()!!.split(" ").map { it.toInt() }[0]
    val p = readLine()!!.split(" ").map { it.toInt() }
    val q = readLine()!!.split(" ").map { it.toInt() }
    var a = 0
    var b = 0
    var count = 0

    fun dfs(myList: MutableList<Int>) {

        val m = myList.size
        if (n == m) {
            count++
            if (myList == p) {
                a = count
            }
            if (myList == q) {
                b = count
            }
            return
        }
        for (i in 1 until n+1){
            var flag = 0
            if (m > 0) {
                for (j in 0 until m) {
                    if (myList[j] == i) {
                        flag = 1
                        break
                    }
                }
            }
            if (flag == 1) continue
            myList.add(i)
            dfs(myList)
            myList.removeAt(m)
        }
    }

    dfs(mutableListOf())
    if (a > b) println("${a - b}")
    else println("${b - a}")
}

