Scanf.scanf "%d %d" (fun n m ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let b = Array.init m (fun _ -> Scanf.scanf " %d" (fun b -> b)) in
    Array.sort compare a;
    Array.sort compare b;

    let ( *@) a b = (a * b) mod 1_000_000_007 in

    let dup x =
        let n = Array.length x in
        let rec loop i =
            if i = n then false else
                if x.(i) = x.(i - 1) then true else loop (i + 1)
        in
        loop 1
    in

    let upper_bound arr v =
        let n = Array.length arr in
        let rec loop l r =
            if l = r then l else
                let m = (l + r) / 2 in
                if arr.(m) < v then loop (m + 1) r else loop l m
        in
        loop 0 n
    in

    let rec loop i acc =
        if i = 0 then acc else
            let r = upper_bound a i in
            let c = upper_bound b i in
            let mr = r < n && a.(r) = i in
            let mc = c < m && b.(c) = i in
            match mr, mc with
            | true,  true  -> loop (i - 1) acc
            | true,  false -> loop (i - 1) (acc *@ (m - c))
            | false, true  -> loop (i - 1) (acc *@ (n - r))
            | false, false -> loop (i - 1) (acc *@ ((m - c) * (n - r) - (n * m - i)))
    in
    Printf.printf "%d\n" @@ if dup a || dup b then 0 else loop (n * m) 1
)