program speedrun
implicit none
integer::N
integer,allocatable,dimension(:)::P,Q
integer,allocatable,dimension(:)::Perm,DID
integer::i
integer::a=0,b=0
read*,N
allocate(P(N),Q(N),Perm(0:N),DID(1:N))
read*,P
read*,Q
Perm=1
do i=1,N
    Perm(i)=Perm(i-1)*i
end do
DID=0
do i=1,N
    a=a+Perm(N-i)*(P(i)-sum(DID(1:P(i)))+1)
    DID(P(i))=1
end do
DID=0
do i=1,N
    b=b+Perm(N-i)*(Q(i)-sum(DID(1:Q(i)))+1)
    DID(Q(i))=1
end do
print"(i0)",abs(a-b)
end program speedrun