program main

  implicit none

  integer :: a, N, i, j, k, pn, qn, tmp
  integer,allocatable :: P(:), Q(:)
  
  read(*,*) N
  allocate( P(N) )
  allocate( Q(N) )
  read(*,*) P
  read(*,*) Q
  
  pn = 0 
  qn = 0 

  do i = 1, N
    ! kaijou
    tmp = 1
    do j = 1, N - i
      tmp =  tmp * j  
    end do
    a = 0
    do k = 1, i 
      if ( P(k) < P(i) ) a = a + 1
    end do
    pn = pn + ( P(i) - a - 1 ) * tmp
  end do
    pn = pn + 1 

  do i = 1, N 
    ! kaijou
    tmp = 1
    do j = 1, N - i
      tmp =  tmp * j  
    end do
    a = 0
    do k = 1, i
      if ( Q(k) < Q(i) ) a = a + 1
    end do
    qn = qn + ( Q(i) - a - 1 ) * tmp
  end do
    qn = qn + 1

  tmp = pn - qn  
  print*, abs( tmp ), pn, qn
  
end program main
