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:

  L=[]

  for i in range(n):

    l=[]

    for j in range(m):

      l.append(0)

    L.append(l)

  ct=1;ctn=0;ctm=0

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

    if k in A and k in B:

      L[n-ctn-1][m-ctm-1]=k

      ctn+=1;ctm+=1

    elif k in A:

      ct*=ctm;ctn+=1

    elif k in B:

      ct*=ctn;ctm+=1

    else:

      ct*=(ctm*ctn-m*n+k)

    if ct==0:

      break

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

  print(ct)
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:

  L=[]

  l=[0 for j in range(m)]

  for i in range(n):

    L.append(l)

  ct=1;ctn=0;ctm=0

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

    if k in A and k in B:

      L[n-ctn-1][m-ctm-1]=k

      ctn+=1;ctm+=1

    elif k in A:

      ct*=ctm;ctn+=1

    elif k in B:

      ct*=ctn;ctm+=1

    else:

      ct*=(ctm*ctn-m*n+k)

    if ct==0:

      break

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

  print(ct)
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:
    L = []
    for i in range(n):
        l = []
        for j in range(m):
            l.append(0)
        L.append(l)
    ct = 1
    ctn = 0
    ctm = 0
    for k in range(m * n, 0, -1):
        if k in A and k in B:
            L[n - ctn - 1][m - ctm - 1] = k
            ctn += 1
            ctm += 1
        elif k in A:
            ct *= ctm
            ctn += 1
        elif k in B:
            ct *= ctn
            ctm += 1
        else:
            ct *= ctm * ctn - m * n + k
        if ct == 0:
            break
        ct = ct % (10**9 + 7)
    print(ct)

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:
    L = []
    l = [0 for j in range(m)]
    for i in range(n):
        L.append(l)
    ct = 1
    ctn = 0
    ctm = 0
    for k in range(m * n, 0, -1):
        if k in A and k in B:
            L[n - ctn - 1][m - ctm - 1] = k
            ctn += 1
            ctm += 1
        elif k in A:
            ct *= ctm
            ctn += 1
        elif k in B:
            ct *= ctn
            ctm += 1
        else:
            ct *= ctm * ctn - m * n + k
        if ct == 0:
            break
        ct = ct % (10**9 + 7)
    print(ct)

+    l = [0 for j in range(m)] -        l = [] -        for j in range(m): -            l.append(0)