program sample
    implicit none
  
    integer(8) :: i,j,m,n,s,t,xi,yi,b,c
   
    integer(8),allocatable :: p(:),q(:),a(:)
  
    read(*,*) n
    allocate(p(n),q(n),a(n))

    read(*,*)p
    read(*,*)q
    do i=1,n
        a(i)=0
    end do

    do i=1,n
        s=p(i)
        call index(a,n,s,m)
        p(i)=m-1
    end do

    do i=1,n
        a(i)=0
    end do
    do i=1,n
        s=q(i)
        call index(a,n,s,m)
        q(i)=m-1
    end do
    s=0
    do i=1,n-1
        s=(s+(p(i)-q(i)))*(n-i)
    enddo
  
    write(*,*) abs(s)
  
    stop
    contains
    subroutine index(a,n,s,m)
        integer(8)::a(n)
        integer(8)::n,s,i,m
        m=s
        do i=1,s-1
            if (a(i)==1)then
                m=m-1
            end if
        end do
        a(s)=1

    end subroutine index

 end program sample
  

