program count_order
  implicit none
  integer :: n, p(8) = 0, q(8) = 0, i
  integer :: a = 0, b = 0
  integer :: x(8) = (/1,2,3,4,5,6,7,8/)
  read(*,*) n
  read(*,*) p(1:n)
  read(*,*) q(1:n)
  call perm(x(1:n),[integer::])
  write(*,'(i0)') abs(a-b)
contains
  recursive subroutine perm(lin, lout)
    integer, intent(in) :: lin(:), lout(:)
    integer :: i
    if (size(lin) == 0) then
      a = a+cmp(p,lout)
      b = b+cmp(q,lout)
    else
      do i = 1, size(lin)
        call perm(pack(lin, lin /= lin(i)), [lout, lin(i)])
      end do
    end if
  end
  integer function cmp(x,y)
    integer, intent(in) :: x(:), y(:)
    integer :: i
    cmp = 0
    do i = 1, n
      if (x(i) > y(i)) then
        cmp = 1
        return
      else if (x(i) < y(i)) then
        return
      end if
    end do
  end
end program count_order