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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;

namespace AtCoder
{
	class Program
	{
		public static void Main()
		{
#if TextInput
			using(var reader = new StreamReader(Debug.SampleTextPath)) 
			{
				Console.SetIn(reader);
#endif
				var N = IO.ReadInt();
				_buf = new int[N - 1];
				var P = IO.ReadIntArray();
				var a = _GetIndex(P);
				var Q = IO.ReadIntArray();
				var b = _GetIndex(Q);
				IO.Write(Math.Abs(a - b));
#if TextInput
			}
#endif
		}
		private static int _GetIndex(int[] iCheckNumbers)
		{
			var numbers = Enumerable.Range(1 , iCheckNumbers.Length).ToList();
			numbers.Remove(iCheckNumbers[0]);
			_counter = 0;
			_Rec(0 , numbers , string.Join("_" , iCheckNumbers.Skip(1)));

			var p = (int)Util.Factorial(iCheckNumbers.Length - 1);
			return (p * (iCheckNumbers[0] - 1)) + _counter;
		}
		private static bool _Rec(int iNestIndex , List<int> iRemainNumbers , string iCheckNumberText)
		{
			if(iRemainNumbers.Count == 0)
			{
				var checkNumberText = string.Join("_" , _buf);
				var isCheck = (checkNumberText == iCheckNumberText);
				if(!isCheck){ ++ _counter; }
				return isCheck;
			}

			var num = iRemainNumbers.Count;
			for(var iIndex = 0 ; iIndex < num ; ++ iIndex)
			{
				var number = iRemainNumbers[iIndex];
				_buf[iNestIndex] = number;
				iRemainNumbers.RemoveAt(iIndex);

				var rec = _Rec(iNestIndex + 1 , iRemainNumbers , iCheckNumberText);
				if(rec) { return true; }
				iRemainNumbers.Insert(iIndex , number);
			}

			return false;
		}
		private static int[] _buf;
		private static int _counter;
	}

	public static class IO
	{
		public static string   Read()								 { return Console.ReadLine(); }
		public static string[] ReadArray(char iSplitter = ' ')		 { return Read().Split(iSplitter); }
		public static int	   ReadInt()							 { return int.Parse(Read()); }
		public static int[]	   ReadIntArray(char iSplitter = ' ')	 { return ReadArray().Select(iText => int.Parse(iText)).ToArray(); }
		public static long	   ReadLong()							 { return long.Parse(Read()); }
		public static long[]   ReadLongArray(char iSplitter = ' ')	 { return ReadArray().Select(iText => long.Parse(iText)).ToArray(); }
		public static double   ReadDouble()							 { return double.Parse(Read()); }
		public static double[] ReadDoubleArray(char iSplitter = ' ') { return ReadArray().Select(iText => double.Parse(iText)).ToArray(); }
		public static char	   ReadChar()							 { return Read()[0]; }
		public static char[]   ReadCharArray()						 { return Read().ToCharArray(); }

		public static void Write(string iValue)	 { Console.WriteLine(iValue); }
		public static void Write(int iValue)	 { Console.WriteLine(iValue); }
		public static void Write(long iValue)	 { Console.WriteLine(iValue); }
		public static void Write(double iValue)  { Console.WriteLine(iValue); }
		public static void Write(char iValue)	 { Console.WriteLine(iValue); }
		public static void Write(decimal iValue) { Console.WriteLine(iValue); }
		public static void Write(bool iValue)	 { Console.WriteLine(iValue ? "Yes" : "No"); }
		public static void Write<Type>(IEnumerable<Type> iValue , string iJoinWord = " ") { Console.WriteLine(string.Join(iJoinWord , iValue)); }
	}

	public static class Util
	{
		public static long Gcd(long i0 , long i1) { return (i0 > i1) ? _Gcd(i0 , i1) : _Gcd(i1 , i0); }
		public static long Lcm(long i0 , long i1) { return (i0 * i1) / Gcd(i0 , i1); }
		public static long ModPow(long iBaseNumber , long iPow , long iModNumber)
		{
			var num = 1L;
			while(iPow != 0)
			{
				if((iPow & 0x1) != 0)
				{
					num = (num * iBaseNumber) % iModNumber;
				}
				iBaseNumber = (iBaseNumber * iBaseNumber) % iModNumber;
				iPow >>= 1;
			}
			return num;
		}
		public static long CombinationNum(int iLeftNumber , int iRightNumber)
		{
			if(iRightNumber == 0) { return 1; }
			if(iLeftNumber == 0)  { return 0; }
			return iLeftNumber * CombinationNum(iLeftNumber - 1 , iRightNumber - 1) / iRightNumber;
		}
		public static long Factorial(long iNumber)
		{
			if(iNumber == 0) { return 1L; }
			return iNumber * Factorial(iNumber - 1);
		}
		private static long _Gcd(long i0 , long i1)	{ return (i1 == 0) ? i0 : _Gcd(i1 , i0 % i1); }
		private static int[] _indexBuf;
	}

	public class UnionFind
	{
		public int[] Parents;
		public int[] RootNums;
		public UnionFind(int iRootNum)
		{
			Parents = Enumerable.Range(0 , iRootNum).ToArray();
			RootNums = new int[iRootNum];
		}
		public void Normalize()
		{
			for(var iIndex = 0 ; iIndex < Parents.Length ; ++ iIndex)
			{
				var rootIndex = _GetRoot(iIndex);
				++ RootNums[rootIndex];
				Parents[iIndex] = rootIndex;
			}
		}
		public void Union(int iNumber0 , int iNumber1)
		{
			var rootNumber0 = _GetRoot(iNumber0);
			var rootNumber1 = _GetRoot(iNumber1);
			if(rootNumber0 < rootNumber1)
			{
				Parents[rootNumber1] = rootNumber0;
			}
			else
			{
				Parents[rootNumber0] = rootNumber1;
			}
		}
		public int _GetRoot(int iNumber)
		{
			return (Parents[iNumber] == iNumber) ? iNumber : _GetRoot(Parents[iNumber]);
		}
	}

	public class CumulativeSum
	{
		public static CumulativeSum Create(IEnumerable<long> iNumbers)
		{
			var numbers = iNumbers.ToArray();
			var cumulativeSum = new CumulativeSum
			{
				Numbers = numbers,
				SumNumbers = new long[numbers.Length + 1]
			};
			var sumNumber = 0L;
			for(var iIndex = 0 ; iIndex < numbers.Length ; ++ iIndex)
			{
				sumNumber += numbers[iIndex];
				cumulativeSum.SumNumbers[iIndex + 1] = sumNumber;
			}
			return cumulativeSum;
		}

		public long[] Numbers { get; private set; }
		public long[] SumNumbers { get; private set; }

		public int GetMaxTotalIndex(int iAreaNum)
		{
			var maxIndex = -1;
			var maxNumber = 0L;
			var num = Numbers.Length - iAreaNum + 1;
			for(var iIndex = 0 ; iIndex < num ; ++ iIndex)
			{
				var checkNumber = SumNumbers[iAreaNum + iIndex] - SumNumbers[iIndex];
				if(maxNumber < checkNumber) 
				{
					maxIndex = iIndex;
					maxNumber = checkNumber;
				}
			}
			return maxIndex;
		}
	}
}