import bisect

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

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

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

x=10**9+7

if len(set(a))!=len(a) or len(set(b))!=len(b):

  print((0))

else:

  a.sort()

  b.sort()

  L=[]

  for i in range(n):

    l=[]

    for j in range(m):

      l.append(0)

    L.append(l)

  ct=1;ctn=0;ctm=0

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

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

    if k in A and k in B:

      L[bisect.bisect_left(a,k)][bisect.bisect_left(b,k)]=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%x

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

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

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

x=10**9+7

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%x

  print(ct)
import bisect

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = 10**9 + 7
if len(set(a)) != len(a) or len(set(b)) != len(b):
    print((0))
else:
    a.sort()
    b.sort()
    L = []
    for i in range(n):
        l = []
        for j in range(m):
            l.append(0)
        L.append(l)
    ct = 1
    ctn = 0
    ctm = 0
    A = set(a)
    B = set(b)
    for k in range(m * n, 0, -1):
        if k in A and k in B:
            L[bisect.bisect_left(a, k)][bisect.bisect_left(b, k)] = 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 % x
    print(ct)

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = 10**9 + 7
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 % x
    print(ct)

-import bisect - -if len(set(a)) != len(a) or len(set(b)) != len(b): +A = set(a) +B = set(b) +if len(A) != len(a) or len(B) != len(b): -    a.sort() -    b.sort() -    A = set(a) -    B = set(b) -            L[bisect.bisect_left(a, k)][bisect.bisect_left(b, k)] = k +            L[n - ctn - 1][m - ctm - 1] = k