Problem:
Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0

Solution in Kotlin (1.3.71) (judge verdict: Accepted)

import kotlin.math.abs

fun main() {
    class Permutation<T:Comparable<T>> (private var li: List<T>){

        val size_li:Int
        val size_e:Int
        val sortedList: List<List<T>>
        val invMap: Map<List<T>, Int>
        private var is_setting:Boolean =false
        var now: List<T>
            set(value){
                if (is_setting){
                    field=value
                }else{
                    this.setting(value = value)
                }
            }
        var ind:Int
            set(value){
                if (is_setting){
                    field=value
                }else{
                    this.setting(index = value)
                }

            }

        init {
            this.is_setting=true
            this.now= listOf<T>()
            this.ind=-1
            this.size_li=this.fact(li.size)
            this.size_e=li.size
            this.sortedList=this.getList(li.sorted()).toList()
            this.invMap= this.getMap().toMap()
            this.setting(value = li)
        }

        private fun setting(index:Int?=null, value:List<T>?=null){
            this.is_setting=true
            if (index!==null) {this.ind= index; this.now=this.sortedList[index]}
            if (value!==null) {this.now= value; this.ind= this.invMap[value] ?: error("out of map")}
            this.is_setting=false
        }
        fun next():Boolean{
            this.ind =(this.ind+1)%this.size_li
            this.now=this.sortedList[this.ind]
            return this.ind != 0
        }
        fun prev():Boolean{
            this.ind=(this.ind-1+this.size_li)%this.size_li
            this.now=this.sortedList[this.ind]
            return this.ind!=this.size_li-1
        }
        private fun getList(li_before: List<T>, li_after: List<T> =listOf<T>(), tmp_ans: MutableList<List<T>> =mutableListOf<List<T>>()): MutableList<List<T>>{
            var ans:MutableList<List<T>> =mutableListOf<List<T>>()
            if (li_before.size<=1){
                tmp_ans.add(li_after+li_before)
                ans= tmp_ans
            }else{
                for (i in li_before.indices){
                    ans= this.getList(li_before.take(i)+li_before.takeLast(li_before.size-1-i), li_after+li_before[i],tmp_ans)
                }
            }
            return ans
        }
        private fun getMap():MutableMap<List<T>, Int>{
            var ans= mutableMapOf<List<T>, Int>()
            this.sortedList.forEachIndexed { index, e -> ans[e]=index }
            return ans
        }
        private fun fact(n:Int): Int{
            return if (n==0) 1 else n*this.fact(n-1)
        }
    }
    readLine()
    var p= readLine()!!.split(" ").map { it.toInt() }
    var q= readLine()!!.split(" ").map { it.toInt() }
    var perm=Permutation(p)
    var tmp=perm.ind
    perm.now=q
    println(abs(tmp-perm.ind))

}