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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUF_SIZE 30

int get_int(void) {
  int num;
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return 0;
  sscanf(line, "%d", &num);
#else
#error
#endif
  return num;
}


// <arr[0]> <arr[1]> .. <arr[size-1]>
int fget_array(int *arr, int size) {
#ifdef BUF_SIZE
    char line[BUF_SIZE];
    char *tmpbuf = line;
    int i;
    if(!fgets(line, BUF_SIZE, stdin)) return -1;
    for(i = 0; i < size; i++) {
        char *tmp = strtok(tmpbuf, " ");
        arr[i] = strtol(tmp, NULL, 10);
        tmpbuf = NULL;
    }
#else
#error
#endif
    return 0;
}

#define NUM_MAX 8
char used[NUM_MAX];
int p[NUM_MAX];
int idx = 0;

int perm1(int *arr, int pos, int size, int *comp, int *ans) {
    int i;
    if(pos == size) {
        // main func
        int flag = 1;
        idx++;
        for(i = 0; i < size; i++) {
            if(arr[p[i]] == comp[i]) continue;
            flag = 0; break;
        }
        if(flag) *ans = idx;
        return flag;
    }

    for(i = 0; i < size; i++) {
        if(used[i]) continue;
        used[i] = 1;
        p[pos] = i;
        int res = perm1(arr, pos+1, size, comp, ans);
        // cannot early return because used and p are global variables
        //if(res) return 1;
        used[i] = 0;
    }
    return 0;
}

int perm(int *comp, int size) {
    int ans = 0;
    idx = 0;
    int arr[NUM_MAX] = {1,2,3,4,5,6,7,8};
    // arr, 0, size (+ in: comp, out: ans)
    perm1(arr, 0, size, comp, &ans);
    return ans;
}

int main(void) {
    int size = get_int();
    int pidx, qidx;

    int ps[NUM_MAX];
    int qs[NUM_MAX];
    fget_array(ps, size);
    fget_array(qs, size);

    pidx = perm(ps, size);
    qidx = perm(qs, size);
#ifdef DEBUG
    printf("%d %d\n", pidx, qidx);
#endif

    int ans = pidx>qidx ? pidx-qidx : qidx-pidx;
    printf("%d\n", ans);

    return 0;
}