Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
def main():
    n, m = map(int, raw_input().split())
    a = map(int, raw_input().split())
    b = map(int, raw_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 xrange(n)]
    for i in xrange(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 xrange(m):
                if b[j] > i:
                    w += u[x][j]
                    u[x][j] = 0
            for j in xrange(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 xrange(m))
            ans = ans * c % 1000000007
            w -= 1
            for j in xrange(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 xrange(n))
            ans = ans * c % 1000000007
            w -= 1
            for j in xrange(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()


Fixed submission:
from collections import defaultdict
def main():
    n, m = map(int, raw_input().split())
    a = map(int, raw_input().split())
    b = map(int, raw_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 xrange(n)]
    for i in xrange(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 xrange(m):
                if b[j] > i:
                    w += u[x][j]
                    u[x][j] = 0
            for j in xrange(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 xrange(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 xrange(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()