### BUGGY CODE ###

import math

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

mod = 10 ** 9 + 7
ans = 1

a.sort(reverse = True)
b.sort(reverse = True)

counta = 0
countb = 0
count = 0

for i in range(n*m,0,-1):
    if a and b:
        if a[0] == i and b[0] == i:
            a_tmp = a.pop(0)
            b_tmp = b.pop(0)
            if a:
                if a[0] == a_tmp:
                    print(0)
                    exit()
            if b:
                if b[0] == b_tmp:
                    print(0)
                    exit()
            counta += 1
            countb += 1
        elif a[0] == i:
            a_tmp = a.pop(0)
            if a:
                if a[0] == a_tmp:
                    print(0)
                    exit()
            counta += 1
            ans *= countb
            ans %= mod
        elif b[0] == i:
            b_tmp = b.pop(0)
            if b:
                if b[0] == b_tmp:
                    print(0)
                    exit()
            countb += 1
            ans *= counta
            ans %= mod
        else:
            ans *= (counta * countb - count)
            ans %= mod
    elif a:
        if a[0] == i:
            a_tmp = a.pop(0)
            if a:
                if a[0] == a_tmp:
                    print(0)
                    exit()
            counta += 1
            ans *= countb
            ans %= mod
        else:
            ans *= (counta * countb - count)
            ans %= mod
    elif b:
        if b[0] == i:
            b_tmp = b.pop(0)
            if b:
                if b[0] == b_tmp:
                    print(0)
                    exit()
            countb += 1
            ans *= counta
            ans %= mod
        else:
            ans *= (counta * countb - count)
            ans %= mod
    count += 1
    if not a and not b:
        idx = i
        break

print(ans * math.factorial(idx-1) % mod)


### FIXED CODE ###

import math

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

mod = 10 ** 9 + 7
ans = 1

a.sort(reverse = True)
b.sort(reverse = True)

counta = 0
countb = 0
count = 0

for i in range(n*m,0,-1):
    if a and b:
        if a[0] == i and b[0] == i:
            a_tmp = a.pop(0)
            b_tmp = b.pop(0)
            if a:
                if a[0] == a_tmp:
                    print(0)
                    exit()
            if b:
                if b[0] == b_tmp:
                    print(0)
                    exit()
            counta += 1
            countb += 1
        elif a[0] == i:
            a_tmp = a.pop(0)
            if a:
                if a[0] == a_tmp:
                    print(0)
                    exit()
            counta += 1
            ans *= countb
            ans %= mod
        elif b[0] == i:
            b_tmp = b.pop(0)
            if b:
                if b[0] == b_tmp:
                    print(0)
                    exit()
            countb += 1
            ans *= counta
            ans %= mod
        else:
            ans *= (counta * countb - count)
            ans %= mod
    elif a:
        if a[0] == i:
            a_tmp = a.pop(0)
            if a:
                if a[0] == a_tmp:
                    print(0)
                    exit()
            counta += 1
            ans *= countb
            ans %= mod
        else:
            ans *= (counta * countb - count)
            ans %= mod
    elif b:
        if b[0] == i:
            b_tmp = b.pop(0)
            if b:
                if b[0] == b_tmp:
                    print(0)
                    exit()
            countb += 1
            ans *= counta
            ans %= mod
        else:
            ans *= (counta * countb - count)
            ans %= mod
    count += 1
    if not a and not b:
        idx = i
        break

for i in range(1,idx):
    ans *= i
    ans %= mod

print(ans)