Mod = 10**9+7
n,m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort.reverse
b = gets.split.map(&:to_i).sort.reverse
i = j = 0
ans = 1
(n*m).downto(1) do |num|
    while i+1 < n && num <= a[i+1]
        i += 1
    end
    while j+1 < m && num <= b[j+1]
        j += 1
    end
    if a[i] == num
        if b[j] != num
            ans *= j+1
        end
    elsif b[j] == num
        ans *= i+1
    else
        ans *= (i+1)*(j+1)-n*m+num
    end
    ans = 0 if ans <= 0
    ans %= Mod
end
ans = 0 if a.uniq.size < n || b.uniq.size < m
puts ans