program test 
implicit none
  integer,allocatable::p(:),q(:)
  integer :: n,i,ans,a,b,k,l,h,s
  read*,n
  allocate(p(n))
  allocate(q(n))
  read(*,*)(p(i),i=1,N)
  read(*,*)(q(i),i=1,N)
  s=1
  k=0
  l=0
  s=1
  do i=1,n
    a=0
    b=0
    	do h=1,n-i+1
        	if(p(h)<p(n-i+1))then
            	a=a+1
            else 
            	a=a
            end if
            if(q(h)<q(n-i+1))then
            	b=b+1
            else 
            	b=b
            end if
        end do
	k=(p(n-i+1)-a-1)*s+k
    l=(q(n-i+1)-b-1)*s+l
    s=s*i
  end do
  ans=abs(k-l)
  print*,ans
end program test