using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
using Pair = System.Tuple<int, int>;
using Edge = System.Tuple<int, int, long>;
//using Debug;
//using static System.Globalization.CultureInfo;

class Program
{
    static void Main(string[] args)
    {
        //Solve();
        WriteLine(Solve());
    }
    static long Solve()
    {
        var nm = Input.ar;
        var a = Input.ar;
        var b = Input.ar;
        var seta = new HashSet<int>();
        var setb = new HashSet<int>();
        for (var i = 0; i < nm[0]; i++)
            seta.Add(a[i]);
        for(var i = 0; i < nm[1]; i++)
            setb.Add(b[i]);
        if (seta.Count != nm[0] || setb.Count != nm[1])
            return 0;
        Array.Sort(a);
        Array.Sort(b);
        var res = 1L;
        for(var i = nm[0] * nm[1]; i >= 1; i--)
        {
            var isa = seta.Contains(i);
            var isb = setb.Contains(i);
            var cta = nm[0]-Search.UpperBound(a, i);
            var ctb = nm[1]-Search.UpperBound(b, i);
            if (isa && isb) continue;
            else if (isb)
                res = res * cta % Input.MOD;
            else if (isa)
                res = res * ctb % Input.MOD;
            else res = (res * ((long)cta * ctb - nm[0] * nm[1] + i)) % Input.MOD;
        }
        return res;
    }
}

public class Input
{
    public static string read => ReadLine();
    public  static int[] ar => Array.ConvertAll(read.Split(' '), int.Parse);
    public  static int num => ToInt32(read);
    public static long[] arL => Array.ConvertAll(read.Split(' '), long.Parse);
    public  static long numL => ToInt64(read);
    public static char[][] gred(int h) 
        => Enumerable.Repeat(0, h).Select(v => read.ToCharArray()).ToArray();
    public static int[][] ar2D(int num)
        => Enumerable.Repeat(0, num).Select(v => ar).ToArray();
    public static long[][] arL2D(int num)
        => Enumerable.Repeat(0, num).Select(v => arL).ToArray();
    public const long Inf = (long)1e18;
    public const double eps = 1e-6;
    public  const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}

public class Search
{
    public static int UpperBound<T>(T[] array,T value)
        where T:IComparable<T>
    {
        var low = 0;
        var high = array.Length;
        while (high > low)
        {
            var mid = (high + low) / 2;
            if (array[mid].CompareTo(value) == 1) high = mid;
            else low = mid + 1;
        }
        return low;
    }

    public static int LowerBound<T>(T[] array,T value)
        where T : IComparable<T>
    {
        var low = 0;
        var high = array.Length;
        while (high > low)
        {
            var mid = (high + low) / 2;
            if (array[mid].CompareTo(value) != -1) high = mid;
            else low = mid + 1;
        }
        return low;
    }

    public static T Max<T>(params T[] array)
        => array.Max();
    public static T Min<T>(params T[] array)
        => array.Min();
    public static bool IsInside(int h, int w, int[] hw)
        => 0 <= h && h < hw[0] && 0 <= w && w < hw[1];
    public static bool IsPrime(long num)
    {
        if (num % 2 == 0 || num == 1) return false;
        for (var i = 3; i * i <= num; i += 2)
            if (num % i == 0) return false;
        return true;
    }
}


Measured memory_bytes: 13536