module Sort_Module
    implicit none
    public
    contains
    recursive subroutine mergesort(a, p, r, b)
        integer(8) a(:), b(:)
        integer(8) p, r
        integer(8) q
        if (p < r) then
            q = (p+r)/2
            call mergesort(a, p, q, b)
            call mergesort(a, q+1, r, b)
            call merge(a, p, q, r, b)
        end if
    end subroutine mergesort
    subroutine merge(a, p, q, r, b)
        integer(8) a(:), b(:)
        integer(8) p, r, q, i, j, k
        integer(8) t
        k = p;   i = p;   j = q+1
        do
            if (.not. (k <= r)) exit
            if (j > r) then
                t = a(i);   i = i+1
            else if (i > q) then
                t = a(j);   j = j+1
            else if (a(i) <= a(j)) then
                t = a(i);   i = i+1
            else
                t = a(j);   j = j+1
            end if
            b(k) = t;   k = k+1
        end do
        do i = p, r
            a(i) = b(i)  
        end do
    end subroutine merge
    subroutine sort(a)
      integer(8) a(:),b(ubound(a,1)-size(a)+1:ubound(a,1)),n,k
      n = ubound(a,1)
      k = ubound(a,1)-size(a)+1
      call mergesort(a,k,n,b)
    end subroutine sort
    function POSL(a,v) result(r)!find first i:a(i)>=v
        implicit none
        integer(8) :: a(:),l,r,idx,v
        r = ubound(a,1) + 1
        l = ubound(a,1)-size(a)
        do while(r-l>1)
            idx = l + (r-l)/2
            if(a(idx) >= v)then
                r = idx
            else
                l = idx
            endif
        enddo
    end function
    function POSU(a,v) result(r)!find first i:a(i)>v
        implicit none
        integer(8) :: a(:),l,r,idx,v
        r = ubound(a,1) + 1
        l = ubound(a,1)-size(a)
        do while(r-l>1)
            idx = l + (r-l)/2
            if(a(idx) > v)then
                r = idx
            else
                l = idx
            endif
        enddo
    end function
    function NUM(a,v) result(n)
        implicit none
        integer(8) :: a(:),v,n
        n = POSU(a,v) - POSL(a,v)
    end function
end module Sort_Module

program atcoder
    use Sort_Module
    implicit none
    integer(8) :: N,M,i,ans,k,l,x,y,z,MO
    integer(8),allocatable,dimension(:) :: A,B
    MO = 1000000007_8
    read*,N,M
    allocate(A(N),B(M))
    read(*,*)(A(i),i=1,N)
    read(*,*)(B(i),i=1,M)
    call sort(A)
    call sort(B)
    ans = 1_8
    do i = N*M,1_8,-1_8
        k = NUM(A,i)
        l = NUM(B,i)
        x = N - POSL(A,i) + 1_8
        y = M - POSL(B,i) + 1_8
        z = N*M - i
        if(k*l>=2)then
            write(*,'(I0)')0
            return
        else if(k*l==1)then
            continue
        else if(k==0.and.l==0)then
            ans = mod(ans*(x*y-z),MO)
        else if(l==1)then
            ans = mod(ans*x,MO)
        else if(k==1)then
            ans = mod(ans*y,MO)
        end if
    enddo
    write(*,'(I0)')ans
    stop 
contains
end program atcoder