# coding:utf-8



import sys

import bisect



# from collections import Counter, defaultdict



INF = float('inf')

MOD = 10 ** 9 + 7





def LI(): return [int(x) for x in sys.stdin.readline().split()]

def LS(): return sys.stdin.readline().split()

def I(): return int(sys.stdin.readline())

def S(): return eval(input())





N, M = LI()

A = LI()

B = LI()



A.sort()

B.sort()



SA = set(A)

SB = set(B)





if N != len(SA) or M != len(SB):

    print((0))

    exit()



ans = 1

num = 0

aa = N

bb = M

for i in range(1, N * M + 1)[::-1]:

    if i in SA and i in SB:

        pass

    elif i in SB:

        while aa != 0 and 0 < i <= A[aa - 1]:

            aa -= 1

        ans *= N - aa

        ans %= MOD

    elif i in SA:

        while bb != 0 and 0 < i <= B[bb - 1]:

            bb -= 1

        ans *= M - bb

        ans %= MOD

    else:

        while aa != 0 and 0 < i <= A[aa - 1]:

            aa -= 1

        while bb != 0 and 0 < i <= B[bb - 1]:

            bb -= 1

        ans *= (M - bb) * (N - aa) - num

        ans %= MOD

    num += 1

    # print(i, ans, num)



print(ans)

# coding:utf-8



import sys

INF = float('inf')

MOD = 10 ** 9 + 7



def LI(): return [int(x) for x in sys.stdin.readline().split()]

def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]

def LS(): return sys.stdin.readline().split()

def II(): return int(sys.stdin.readline())

def SI(): return eval(input())





def main():

    h, w = LI()

    A = LI()

    A.sort()

    B = LI()

    B.sort()



    # A, B内では同じ値は存在できない

    if len(A) != len(set(A)) or len(B) != len(set(B)):

        return 0



    a_i, b_i = h - 1, w - 1

    if h * w > A[a_i] or h * w > B[b_i]:

        return 0



    res = 1

    for n in range(1, h * w + 1)[::-1]:

        while a_i > 0 and A[a_i - 1] >= n:

            a_i -= 1

        while b_i > 0 and B[b_i - 1] >= n:

            b_i -= 1



        if n == A[a_i] and n == B[b_i]:

            res *= 1

        elif n == A[a_i]:

            res *= w - b_i

        elif n == B[b_i]:

            res *= h - a_i

        else:

            res *= (h - a_i) * (w - b_i) - (h * w - n)



        res %= MOD



    return res





print((main()))

# coding:utf-8
import sys
import bisect

# from collections import Counter, defaultdict
INF = float("inf")
MOD = 10**9 + 7


def LI():
    return [int(x) for x in sys.stdin.readline().split()]


def LS():
    return sys.stdin.readline().split()


def I():
    return int(sys.stdin.readline())


def S():
    return eval(input())


N, M = LI()
A = LI()
B = LI()
A.sort()
B.sort()
SA = set(A)
SB = set(B)
if N != len(SA) or M != len(SB):
    print((0))
    exit()
ans = 1
num = 0
aa = N
bb = M
for i in range(1, N * M + 1)[::-1]:
    if i in SA and i in SB:
        pass
    elif i in SB:
        while aa != 0 and 0 < i <= A[aa - 1]:
            aa -= 1
        ans *= N - aa
        ans %= MOD
    elif i in SA:
        while bb != 0 and 0 < i <= B[bb - 1]:
            bb -= 1
        ans *= M - bb
        ans %= MOD
    else:
        while aa != 0 and 0 < i <= A[aa - 1]:
            aa -= 1
        while bb != 0 and 0 < i <= B[bb - 1]:
            bb -= 1
        ans *= (M - bb) * (N - aa) - num
        ans %= MOD
    num += 1
    # print(i, ans, num)
print(ans)

# coding:utf-8
import sys

INF = float("inf")
MOD = 10**9 + 7


def LI():
    return [int(x) for x in sys.stdin.readline().split()]


def LI_():
    return [int(x) - 1 for x in sys.stdin.readline().split()]


def LS():
    return sys.stdin.readline().split()


def II():
    return int(sys.stdin.readline())


def SI():
    return eval(input())


def main():
    h, w = LI()
    A = LI()
    A.sort()
    B = LI()
    B.sort()
    # A, B内では同じ値は存在できない
    if len(A) != len(set(A)) or len(B) != len(set(B)):
        return 0
    a_i, b_i = h - 1, w - 1
    if h * w > A[a_i] or h * w > B[b_i]:
        return 0
    res = 1
    for n in range(1, h * w + 1)[::-1]:
        while a_i > 0 and A[a_i - 1] >= n:
            a_i -= 1
        while b_i > 0 and B[b_i - 1] >= n:
            b_i -= 1
        if n == A[a_i] and n == B[b_i]:
            res *= 1
        elif n == A[a_i]:
            res *= w - b_i
        elif n == B[b_i]:
            res *= h - a_i
        else:
            res *= (h - a_i) * (w - b_i) - (h * w - n)
        res %= MOD
    return res


print((main()))

-import bisect -# from collections import Counter, defaultdict +def LI_(): +    return [int(x) - 1 for x in sys.stdin.readline().split()] + + -def I(): +def II(): -def S(): +def SI(): -N, M = LI() -A = LI() -B = LI() -A.sort() -B.sort() -SA = set(A) -SB = set(B) -if N != len(SA) or M != len(SB): -    print((0)) -    exit() -ans = 1 -num = 0 -aa = N -bb = M -for i in range(1, N * M + 1)[::-1]: -    if i in SA and i in SB: -        pass -    elif i in SB: -        while aa != 0 and 0 < i <= A[aa - 1]: -            aa -= 1 -        ans *= N - aa -        ans %= MOD -    elif i in SA: -        while bb != 0 and 0 < i <= B[bb - 1]: -            bb -= 1 -        ans *= M - bb -        ans %= MOD -    else: -        while aa != 0 and 0 < i <= A[aa - 1]: -            aa -= 1 -        while bb != 0 and 0 < i <= B[bb - 1]: -            bb -= 1 -        ans *= (M - bb) * (N - aa) - num -        ans %= MOD -    num += 1 -    # print(i, ans, num) -print(ans) +def main(): +    h, w = LI() +    A = LI() +    A.sort() +    B = LI() +    B.sort() +    # A, B内では同じ値は存在できない +    if len(A) != len(set(A)) or len(B) != len(set(B)): +        return 0 +    a_i, b_i = h - 1, w - 1 +    if h * w > A[a_i] or h * w > B[b_i]: +        return 0 +    res = 1 +    for n in range(1, h * w + 1)[::-1]: +        while a_i > 0 and A[a_i - 1] >= n: +            a_i -= 1 +        while b_i > 0 and B[b_i - 1] >= n: +            b_i -= 1 +        if n == A[a_i] and n == B[b_i]: +            res *= 1 +        elif n == A[a_i]: +            res *= w - b_i +        elif n == B[b_i]: +            res *= h - a_i +        else: +            res *= (h - a_i) * (w - b_i) - (h * w - n) +        res %= MOD +    return res + + +print((main()))