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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;

public class Main {

	static long m = (long) (1e9 + 7);
	static String arr[];
	static int count = 0;

	public static void main(String[] args) throws IOException {
		Scanner scn = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		int n = scn.nextInt();
		arr = new String[fact(n)];
		String a = "", b = "";
		int arr1[] = new int[n];
		int arr2[] = new int[n];
		for (int i = 0; i < n; i++) {
			arr1[i] = scn.nextInt();
			a += arr1[i];
		}
		for (int i = 0; i < n; i++) {
			arr2[i] = scn.nextInt();
			b += arr2[i];
		}
		String num = "";
		for (int i = 1; i <= n; i++) 
			num += i;
		StringBuilder s1 = new StringBuilder(num);
		permute(s1, 0, n - 1, arr);
		int k = fact(n);
		Arrays.sort(arr);
		int index1 = 0, index2 = 0;
		for (int i = 0; i < k; i++) {
			if (arr[i].compareTo(a) == 0)
				index1 = i + 1;
			if (arr[i].compareTo(b) == 0)
				index2 = i + 1;
		}
		System.out.println(Math.max(index2, index1) - Math.min(index1, index2));
	}

	static StringBuilder swap(int i1, int i2, StringBuilder s) {
		char temp1 = s.charAt(i1), temp2 = s.charAt(i2);
		s.setCharAt(i1, temp2);
		s.setCharAt(i2, temp1);
		return s;
	}

	static void permute(StringBuilder s, int l, int r, String arr[]) {
		if (l == r) {
			arr[count] = s.toString();
			count++;
		} else {
			for (int i = l; i <= r; i++) {
				s = swap(i, l, s);
				permute(s, l + 1, r, arr);
				s = swap(l, i, s);
			}
		}
	}

	static int fact(int a) {
		if (a == 1 || a == 0)
			return 1;
		return a * fact(a - 1);
	}

	static class Scanner {

		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
	}
}