n,m=list(map(int,input().split()))

a=list(map(int,input().split()))

b=list(map(int,input().split()))

A=set(a);B=set(b)

if len(A)!=len(a) or len(B)!=len(b):

  print((0))

else:

  c=1;cn=0;cm=0

  for k in range(m*n,0,-1):

    if k in A and k in B:

      cn+=1;cm+=1

    elif k in A:

      c*=cm;cn+=1

    elif k in B:

      c*=cn;cm+=1

    else:

      c*=(cm*cn-m*n+k)

    if c==0:

      break

    c=c%(10**9+7)

  print(c)
n,m=list(map(int,input().split()))

a=list(map(int,input().split()))

b=list(map(int,input().split()))

A=set(a);B=set(b)

if len(A)!=len(a) or len(B)!=len(b):

  print((0))

else:

  c=1;cn=0;cm=0

  for k in range(m*n,0,-1):

    if k in A and k in B:

      cn+=1;cm+=1

    elif k in A:

      c*=cm;cn+=1

    elif k in B:

      c*=cn;cm+=1

    else:

      c*=cm*cn-m*n+k

    c=c%(10**9+7)

  print(c)
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = set(a)
B = set(b)
if len(A) != len(a) or len(B) != len(b):
    print((0))
else:
    c = 1
    cn = 0
    cm = 0
    for k in range(m * n, 0, -1):
        if k in A and k in B:
            cn += 1
            cm += 1
        elif k in A:
            c *= cm
            cn += 1
        elif k in B:
            c *= cn
            cm += 1
        else:
            c *= cm * cn - m * n + k
        if c == 0:
            break
        c = c % (10**9 + 7)
    print(c)

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = set(a)
B = set(b)
if len(A) != len(a) or len(B) != len(b):
    print((0))
else:
    c = 1
    cn = 0
    cm = 0
    for k in range(m * n, 0, -1):
        if k in A and k in B:
            cn += 1
            cm += 1
        elif k in A:
            c *= cm
            cn += 1
        elif k in B:
            c *= cn
            cm += 1
        else:
            c *= cm * cn - m * n + k
        c = c % (10**9 + 7)
    print(c)

-        if c == 0: -            break