program double_landscape
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer :: n, m, a(1000), b(1000), ac, bc, i
  logical :: au(1000000), bu(1000000)
  integer(8) :: x
  a = 0
  b = 0
  au = .false.
  bu = .false.
  read(*,*) n, m
  read(*,*) a(1:n)
  read(*,*) b(1:m)
  do i = 1, n
    if (au(a(i))) then
      write(*,'(i0)') 0
      stop
    end if
    au(a(i)) = .true.
  end do
  do i = 1, m
    if (bu(b(i))) then
      write(*,'(i0)') 0
      stop
    end if
    bu(b(i)) = .true.
  end do
  ac = 0
  bc = 0
  x = 1_8
  do i = n*m, 1, -1
    if (au(i).and.bu(i)) then
      ac = ac+1
      bc = bc+1
    else if (au(i)) then
      ac = ac+1
      x = mod(x*int(bc,8),md)
    else if (bu(i)) then
      bc = bc+1
      x = mod(x*int(ac,8),md)
    else
      x = mod(x*(int(ac,8)*int(bc,8)-int(n*m-i,8)),md)
    end if
  end do
  write(*,'(i0)') x
  stop
end program double_landscape