from collections import Counter

import sys

input=sys.stdin.readline





def solve():

    N, M = list(map(int, input().split()))

    *A, = list(map(int, input().split()))

    *B, = list(map(int, input().split()))

    c = Counter()

    MOD = 10**9+7

    used = set()



    for i in range(N):

        for j in range(M):

            if A[i]==B[j]:

                if A[i] in used:

                    print((0))

                    return

                else:

                    used.add(A[i])

            c[min(A[i], B[j])] += 1



    cd = 0

    ans = 1

    bef = 0

    for i, j in sorted(c.items()):

        cd += (i - bef)

        if i not in used:

            ans *= j

        cd -= 1

        for k in range(j-1):

            ans *= (cd-k)

            ans %= MOD

        cd -= j-1

        bef = i

    print((ans%MOD))





if __name__ == "__main__":

    solve()

from collections import Counter

import sys

input=sys.stdin.readline





def solve():

    N, M = list(map(int, input().split()))

    *A, = list(map(int, input().split()))

    *B, = list(map(int, input().split()))

    c = Counter()

    MOD = 10**9+7

    used = set()



    for i in A:

        for j in B:

            if i==j:

                if i in used:

                    print((0))

                    return

                else:

                    used.add(i)

            c[min(i, j)] += 1



    v, ans, bef = 0, 1, 0

    for i, j in sorted(c.items()):

        ans *= 1 if i in used else j

        v += i-bef-1

        for k in range(j-1):

            ans *= v-k

            ans %= MOD

        v -= j-1

        bef = i

    print(ans)





if __name__ == "__main__":

    solve()

from collections import Counter
import sys

input = sys.stdin.readline


def solve():
    N, M = list(map(int, input().split()))
    (*A,) = list(map(int, input().split()))
    (*B,) = list(map(int, input().split()))
    c = Counter()
    MOD = 10**9 + 7
    used = set()
    for i in range(N):
        for j in range(M):
            if A[i] == B[j]:
                if A[i] in used:
                    print((0))
                    return
                else:
                    used.add(A[i])
            c[min(A[i], B[j])] += 1
    cd = 0
    ans = 1
    bef = 0
    for i, j in sorted(c.items()):
        cd += i - bef
        if i not in used:
            ans *= j
        cd -= 1
        for k in range(j - 1):
            ans *= cd - k
            ans %= MOD
        cd -= j - 1
        bef = i
    print((ans % MOD))


if __name__ == "__main__":
    solve()

from collections import Counter
import sys

input = sys.stdin.readline


def solve():
    N, M = list(map(int, input().split()))
    (*A,) = list(map(int, input().split()))
    (*B,) = list(map(int, input().split()))
    c = Counter()
    MOD = 10**9 + 7
    used = set()
    for i in A:
        for j in B:
            if i == j:
                if i in used:
                    print((0))
                    return
                else:
                    used.add(i)
            c[min(i, j)] += 1
    v, ans, bef = 0, 1, 0
    for i, j in sorted(c.items()):
        ans *= 1 if i in used else j
        v += i - bef - 1
        for k in range(j - 1):
            ans *= v - k
            ans %= MOD
        v -= j - 1
        bef = i
    print(ans)


if __name__ == "__main__":
    solve()

-    for i in range(N): -        for j in range(M): -            if A[i] == B[j]: -                if A[i] in used: +    for i in A: +        for j in B: +            if i == j: +                if i in used: -                    used.add(A[i]) -            c[min(A[i], B[j])] += 1 -    cd = 0 -    ans = 1 -    bef = 0 +                    used.add(i) +            c[min(i, j)] += 1 +    v, ans, bef = 0, 1, 0 -        cd += i - bef -        if i not in used: -            ans *= j -        cd -= 1 +        ans *= 1 if i in used else j +        v += i - bef - 1 -            ans *= cd - k +            ans *= v - k -        cd -= j - 1 +        v -= j - 1 -    print((ans % MOD)) +    print(ans)