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

class Main{
    final static long MOD = 1000000007;
    FastScanner sc = new FastScanner();

    public static void main(String[] args) {
        new Main().solve();
    }

    void solve(){

        Set<Integer> set = new TreeSet<Integer>();
        Set<Integer> set2 = new TreeSet<Integer>();
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        int count_a = 1;
        int count_b = 1;

        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
            set.add(i + 1);
        }

        for(int i = 0; i < n; i++){
            b[i] = sc.nextInt();
            set2.add(i + 1);
        }

        int i = 0;
        while(!set.isEmpty()){
            int tmp = -1;
            int j = 0;
            for(int x : set){
                j++;
                if(x == a[i]){
                    count_a += (j - 1) * factorial(set.size() - 1);
                    tmp = x;
                    break;
                }
            }
            set.remove(tmp);
            i++;
        }

        i = 0;
        while(!set2.isEmpty()){
            int tmp = -1;
            int j = 0;
            for(int x : set2){
                j++;
                if(x == b[i]){
                    count_b += (j - 1) * factorial(set2.size() - 1);
                    tmp = x;
                    break;
                }
            }
            set2.remove(tmp);
            i++;
        }

        System.out.println(Math.abs(count_a - count_b));

    }

    public long factorial(int n) {
        long fact = 1;
        for (int i = 2; i <= n; i++) {
            fact = fact * i;
        }
        return fact;
    }

    long gcd(long a, long b){ // return aとbの最大公約数
        if(b == 0){
          return a;
        }
        return gcd(b, a % b);
    }

    long pow_mod(long a, long r){ // return a^r (mod MOD)
        long sum = 1;
        while(r > 0){
            if((r & 1) == 1){
                sum *= a;
                sum %= MOD;
            }
            a *= a;
            a %= MOD;
            r >>= 1;
        }
        return sum;
    }

    boolean is_prime(long a){
        if(a <= 1) return false;
        for(int i = 2; i*i <= a; i++){
            if(a % i == 0) return false;
        }
        return true;
    }

}



class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte(){
        if(ptr < buflen){
            return true;
        }else{
            ptr = 0;
            try{
                buflen = in.read(buffer);
            }catch(IOException e){
                e.printStackTrace();
            }
            if(buflen <= 0){
                return false;
            }
        }
        return true;
    }
    private int readByte(){ if(hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c){ return 33 <= c && c <= 126;}
    public boolean hasNext(){ while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next(){
        if(!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong(){
        if(!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        if(b < '0' || '9' < b){
            throw new NumberFormatException();
        }
        while(true){
            if('0' <= b && b <= '9'){
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt(){
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble(){ return Double.parseDouble(next());}
}


Measured memory_bytes: 20564