      MODULE intQsortMod
! 整数型のクイックソート
! precisionで整数の精度を定める．
! インデックスの小さいほうが値も小さい(昇順)．

! 参考
! https://t.doujin-constant.net/post/143374686394/fortranをより使いこなしたquick-sort

      implicit none
      integer,parameter,private :: precision = 4
      
      interface qSort
        module procedure :: intQsort
      end interface
      
      contains
      pure recursive function intQsort( a ) result( r )
        implicit none
        integer(precision), intent(in) :: a(:)
        integer(precision) :: r( size(a) )
        integer(precision) :: sample(3), len, i,j
        
        len = size( a(:) )
        if( len<=1 )then
          r = a
        else
          !select pivot
          sample(1:3) = [a(1), a(len/2), a(len)]
          do i = 1,2
            do j = 2,3
              if( sample(i)>sample(j) )then
                len = sample(i) !len is buffer
                sample(i) = sample(j)
                sample(j) = len
              end if
            end do
          end do
          
          r = [ qSort( pack(a(:), a(:)< sample(2) ) ), &
                       pack(a(:), a(:)==sample(2) )  , &
                qSort( pack(a(:), a(:)> sample(2) ) ) ]
        end if
      end function intQsort
      END MODULE intQsortMod


      module piyoMod
      implicit none
      integer :: n,numA=0
      integer :: mkdArray(5*10**5)=0
      
      
      contains
      !cnt 新しく作る桁
      recursive subroutine mkA( cnt,array,flag )
        implicit none
        integer,intent(in) :: cnt,array(:)
        integer :: a(size(array))
        logical,intent(in) :: flag(:)
        logical :: f(size(flag))
        integer :: i
        
        if( cnt>n )then
          numA = numA + 1
          do i = 1,n
            mkdArray(numA) = mkdArray(numA) + array(i)*10**(i-1)
          end do
          return
        else
          a = array(:)
          f = flag(:)
          do i = 1,n
            if( flag(i) ) cycle
            a(cnt) = i
            f(i) = .true.
            call mkA( cnt+1,a,f )
            f(i) = .false.
          end do
        end if
      end subroutine
      
      end module
      
      PROGRAM countOrder
      use piyoMod
      use intQsortMod
      IMPLICIT NONE
      integer :: valP=0,valQ=0,indP,indQ,i
      integer,allocatable :: p(:),q(:),dummy(:)
      logical,allocatable :: flag(:)
      
      read*,n
      allocate( p(n),q(n),dummy(n), flag(n) )
      read*,p(n:1:-1)
      read*,q(n:1:-1)
      
      
      
      flag = .false.
      call mkA( 1,dummy,flag )
      
      mkdArray(1:numA) = qsort( mkdArray(1:numA) )
      
      
      
      do i = 1,n
        valP = valP + p(i)*10**(i-1)
        valQ = valQ + q(i)*10**(i-1)
      end do
      
      do i = 1,numA
        if( mkdArray(i)==valP )then
          indP = i
        end if
        if( mkdArray(i)==valQ )then
          indQ = i
        end if
      end do
      
      print*,abs(indP - indQ)
      
      
!      print*,mkdArray(1:numA)
      
      
      
      END PROGRAM