program main
      implicit none
      integer :: n, i, cnt, np, nq
      integer, allocatable :: p(:), q(:), x(:)
      logical, allocatable :: used(:)

      read *, n
      allocate( p(n) )
      allocate( q(n) )
      read *, p
      read *, q

      allocate( x(n) )
      allocate( used(n) )

      cnt = 0
      x = 1
      used(:) = .false.
      call DFS(1,n) 

      print *, abs(np-nq)
      
contains
recursive subroutine DFS(pos,v)
      implicit none
      integer, intent(in) :: pos,v
      integer :: i, j, k

      if( pos == v+1) then
         cnt = cnt + 1
         k = 0

         do j = 1, n
           if ( x(j) == p(j) ) then
             k = k + 1
           else
             exit
           end if
         end do
         if ( k == n ) np = cnt 

         k = 0
         do j = 1, n
           if ( x(j) == q(j) ) then
             k = k + 1
           else
             exit
           end if
         end do
         if ( k == n ) nq = cnt 

         return
      end if

      do i = 1, v
        if ( .not. used(i) ) then
          x(pos) = i
          used(i) = .true.
          call DFS(pos+1,v)
          used(i) = .false.
        end if
      end do

end subroutine DFS      

end program main
