# Original solution
from collections import defaultdict
def main():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    s = defaultdict(list)
    t = defaultdict(list)
    for i, x in enumerate(a):
        s[x].append(i)
    for i, x in enumerate(b):
        t[x].append(i)
    N = n * m
    w = 0
    ans = 1
    u = [[1] * m for _ in range(n)]
    for i in range(N, 0, -1):
        if len(s[i]) > 1 or len(t[i]) > 1:
            print(0)
            return
        if len(s[i]) == 1 and len(t[i]) == 1:
            x = s[i][0]
            y = t[i][0]
            for j in range(m):
                if b[j] > i:
                    w += u[x][j]
                    u[x][j] = 0
            for j in range(n):
                if a[j] > i:
                    w += u[j][y]
                    u[j][y] = 0
        elif len(s[i]) == 1:
            x = s[i][0]
            c = sum(b[j] > i for j in range(m))
            ans = ans * c % 1000000007
            w -= 1
            for j in range(m):
                if b[j] > i:
                    w += u[x][j]
                    u[x][j] = 0
        elif len(t[i]) == 1:
            y = t[i][0]
            c = sum(a[j] > i for j in range(n))
            ans = ans * c % 1000000007
            w -= 1
            for j in range(n):
                if a[j] > i:
                    w += u[j][y]
                    u[j][y] = 0
        else:
            if w <= 0:
                print(0)
                return
            ans = ans * w % 1000000007
            w -= 1
    print(ans)
main()


# Optimized solution
from collections import defaultdict
def main():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    s = defaultdict(list)
    t = defaultdict(list)
    for i, x in enumerate(a):
        s[x].append(i)
    for i, x in enumerate(b):
        t[x].append(i)
    N = n * m
    w = 0
    ans = 1
    u = [[1] * m for _ in range(n)]
    for i in range(N, 0, -1):
        if len(s[i]) > 1 or len(t[i]) > 1:
            print(0)
            return
        if len(s[i]) == 1 and len(t[i]) == 1:
            x = s[i][0]
            y = t[i][0]
            for j in range(m):
                if b[j] > i:
                    w += u[x][j]
                    u[x][j] = 0
            for j in range(n):
                if a[j] > i:
                    w += u[j][y]
                    u[j][y] = 0
        elif len(s[i]) == 1:
            x = s[i][0]
            c = 0
            for j in range(m):
                if b[j] > i:
                    c += 1
                    w += u[x][j]
                    u[x][j] = 0
            w -= 1
            ans = ans * c % 1000000007
        elif len(t[i]) == 1:
            y = t[i][0]
            c = 0
            for j in range(n):
                if a[j] > i:
                    c += 1
                    w += u[j][y]
                    u[j][y] = 0
            w -= 1
            ans = ans * c % 1000000007
        else:
            if w <= 0:
                print(0)
                return
            ans = ans * w % 1000000007
            w -= 1
    print(ans)
main()