Buggy python submission (verdict: Time Limit Exceeded):
import sys

# %%

n, m = list(map(int, sys.stdin.readline().split()))
ary = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
bry = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    ary.append(0)
    bry.append(0)

    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):

        if (num == ary[i]) & (num == bry[j]):
            i += 1
            j += 1

        elif (num == ary[i]) ^ (num == bry[j]):
            if num == ary[i]:
                cnt *= len(bry[:j])
                i += 1
            else:
                cnt *= len(ary[:i])
                j += 1

        else:
            na = len(ary[:i])
            nb = len(bry[:j])
            cnt *= (na * nb - (n * m - num))

        cnt = cnt % mod

    print(cnt)


Fixed submission:
import sys

# %%

n, m = list(map(int, sys.stdin.readline().split()))
ary = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
bry = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    ary.append(0)
    bry.append(0)

    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):

        if (num == ary[i]) and (num == bry[j]):
            i += 1
            j += 1

        elif (num == ary[i]) or (num == bry[j]):
            if num == ary[i]:
                cnt *= len(bry[:j])
                i += 1
            else:
                cnt *= len(ary[:i])
                j += 1

        else:
            # na = len(ary[:i])
            # nb = len(bry[:j])
            cnt *= (i * j - (n * m - num))

        cnt = cnt % mod

    print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

# %%

n, m = list(map(int, sys.stdin.readline().split()))
ary = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
bry = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    ary.append(0)
    bry.append(0)

    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):

        if (num == ary[i]) & (num == bry[j]):
            i += 1
            j += 1

        elif (num == ary[i]) ^ (num == bry[j]):
            if num == ary[i]:
                cnt *= len(bry[:j])
                i += 1
            else:
                cnt *= len(ary[:i])
                j += 1

        else:
            na = len(ary[:i])
            nb = len(bry[:j])
            cnt *= (na * nb - (n * m - num))

        cnt = cnt % mod

    print(cnt % mod)


Fixed submission:
import sys

# %%

n, m = list(map(int, sys.stdin.readline().split()))
ary = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
bry = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    ary.append(0)
    bry.append(0)

    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):

        if (num == ary[i]) and (num == bry[j]):
            i += 1
            j += 1

        elif (num == ary[i]) or (num == bry[j]):
            if num == ary[i]:
                cnt *= len(bry[:j])
                i += 1
            else:
                cnt *= len(ary[:i])
                j += 1

        else:
            # na = len(ary[:i])
            # nb = len(bry[:j])
            cnt *= (i * j - (n * m - num))

        cnt = cnt % mod

    print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
# %%

n, m = list(map(int, input().split()))
ary = sorted(list(map(int, input().split())), reverse=True)
bry = sorted(list(map(int, input().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    ary.append(0)
    bry.append(0)

    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):

        if (num == ary[i]) & (num == bry[j]):
            i += 1
            j += 1

        elif (num == ary[i]) ^ (num == bry[j]):
            if num == ary[i]:
                cnt *= len(bry[:j])
                i += 1
            else:
                cnt *= len(ary[:i])
                j += 1

        else:
            na = len(ary[:i])
            nb = len(bry[:j])
            cnt *= (na * nb - (n * m - num))

        cnt = cnt % mod

    print(cnt % mod)


Fixed submission:
import sys

# %%

n, m = list(map(int, sys.stdin.readline().split()))
ary = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
bry = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    ary.append(0)
    bry.append(0)

    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):

        if (num == ary[i]) and (num == bry[j]):
            i += 1
            j += 1

        elif (num == ary[i]) or (num == bry[j]):
            if num == ary[i]:
                cnt *= len(bry[:j])
                i += 1
            else:
                cnt *= len(ary[:i])
                j += 1

        else:
            # na = len(ary[:i])
            # nb = len(bry[:j])
            cnt *= (i * j - (n * m - num))

        cnt = cnt % mod

    print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
# %%

n, m = list(map(int, input().split()))
ary = sorted(list(map(int, input().split())), reverse=True)
bry = sorted(list(map(int, input().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):
        if (num in ary[i:]) & (num in bry[j:]):
            i += 1
            j += 1
        elif (num in ary[i:]) ^ (num in bry[j:]):
            if num in ary[i:]:
                cnt *= len(bry[:j]) % mod
                i += 1
            else:
                cnt *= len(ary[:i]) % mod
                j += 1
        else:
            na = len(ary[:i])
            nb = len(bry[:j])
            cnt *= (na * nb - (n * m - num)) % mod

    print(cnt % mod)


Fixed submission:
import sys

# %%

n, m = list(map(int, sys.stdin.readline().split()))
ary = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
bry = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    ary.append(0)
    bry.append(0)

    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):

        if (num == ary[i]) and (num == bry[j]):
            i += 1
            j += 1

        elif (num == ary[i]) or (num == bry[j]):
            if num == ary[i]:
                cnt *= len(bry[:j])
                i += 1
            else:
                cnt *= len(ary[:i])
                j += 1

        else:
            # na = len(ary[:i])
            # nb = len(bry[:j])
            cnt *= (i * j - (n * m - num))

        cnt = cnt % mod

    print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
# %%

n, m = list(map(int, input().split()))
ary = sorted(list(map(int, input().split())), reverse=True)
bry = sorted(list(map(int, input().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):
        if (num in ary[i:]) & (num in bry[j:]):
            i += 1
            j += 1
        elif (num in ary[i:]) ^ (num in bry[j:]):
            if num in ary[i:]:
                cnt *= len([b for b in bry if num < b]) % mod
                i += 1
            else:
                cnt *= len([a for a in ary if num < a]) % mod
                j += 1
        else:
            nn = len([a for a in ary if num < a])
            nm = len([b for b in bry if num < b])
            cnt *= (nn * nm - (n * m - num)) % mod

    print(cnt % mod)


Fixed submission:
import sys

# %%

n, m = list(map(int, sys.stdin.readline().split()))
ary = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
bry = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    ary.append(0)
    bry.append(0)

    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):

        if (num == ary[i]) and (num == bry[j]):
            i += 1
            j += 1

        elif (num == ary[i]) or (num == bry[j]):
            if num == ary[i]:
                cnt *= len(bry[:j])
                i += 1
            else:
                cnt *= len(ary[:i])
                j += 1

        else:
            # na = len(ary[:i])
            # nb = len(bry[:j])
            cnt *= (i * j - (n * m - num))

        cnt = cnt % mod

    print(cnt)

Buggy python submission (verdict: Runtime Error):
# %%

n, m = list(map(int, input().split()))
ary = list(map(int, input().split())).sort(reverse=True)
bry = list(map(int, input().split())).sort(reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):
        if (num in ary[i:]) & (num in bry[j:]):
            i += 1
            j += 1
        elif (num in ary[i:]) ^ (num in bry[j:]):
            if num in ary[i:]:
                cnt *= len([b for b in bry if num < b]) % mod
                i += 1
            else:
                cnt *= len([a for a in ary if num < a]) % mod
                j += 1
        else:
            nn = len([a for a in ary if num < a])
            nm = len([b for b in bry if num < b])
            cnt *= (nn * nm - (n * m - num)) % mod

    print(cnt % mod)


Fixed submission:
import sys

# %%

n, m = list(map(int, sys.stdin.readline().split()))
ary = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
bry = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    ary.append(0)
    bry.append(0)

    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):

        if (num == ary[i]) and (num == bry[j]):
            i += 1
            j += 1

        elif (num == ary[i]) or (num == bry[j]):
            if num == ary[i]:
                cnt *= len(bry[:j])
                i += 1
            else:
                cnt *= len(ary[:i])
                j += 1

        else:
            # na = len(ary[:i])
            # nb = len(bry[:j])
            cnt *= (i * j - (n * m - num))

        cnt = cnt % mod

    print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
# %%

n, m = list(map(int, input().split()))
ary = list(map(int, input().split()))
bry = list(map(int, input().split()))
mod = 10 ** 9 + 7


# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    cnt = 1
    for i in range(n * m, 0, -1):
        if (i in ary) & (i in bry):
            pass
        elif (i in ary) ^ (i in bry):
            if i in ary:
                cnt *= len([b for b in bry if i < b]) % mod
            else:
                cnt *= len([a for a in ary if i < a]) % mod
        else:
            nn = len([a for a in ary if i < a])
            nm = len([b for b in bry if i < b])
            cnt *= (nn * nm - (n * m - i)) % mod

    print(cnt % mod)


Fixed submission:
import sys

# %%

n, m = list(map(int, sys.stdin.readline().split()))
ary = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
bry = sorted(list(map(int, sys.stdin.readline().split())), reverse=True)
mod = 10 ** 9 + 7

# %%

if len(set(ary)) != len(ary) or len(set(bry)) != len(bry):
    print(0)

else:
    ary.append(0)
    bry.append(0)

    cnt = 1
    i = 0
    j = 0
    for num in range(n * m, 0, -1):

        if (num == ary[i]) and (num == bry[j]):
            i += 1
            j += 1

        elif (num == ary[i]) or (num == bry[j]):
            if num == ary[i]:
                cnt *= len(bry[:j])
                i += 1
            else:
                cnt *= len(ary[:i])
                j += 1

        else:
            # na = len(ary[:i])
            # nb = len(bry[:j])
            cnt *= (i * j - (n * m - num))

        cnt = cnt % mod

    print(cnt)