#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <stdio.h>
#include <string.h>
#include <cassert>

using namespace std;

static const double EPS = 1e-9;
template<class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template<class T> void amin(T &a, T v) { if (a > v) a = v; }
template<class T> void amax(T &a, T v) { if (a < v) a = v; }
int ROUND(double x) { return (int)(x + 0.5); }
bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; }
bool ISEQUAL(double x, double y) { return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y))); }
double SQSUM(double x, double y) { return x * x + y * y; }
#define PI  (acos(-1))
#define ARRAY_NUM(a) (sizeof(a)/sizeof(a[0])) 
#define NG (-1)
#define BIG ((int)1e9+10)
#define BIGLL ((ll)4e18)
#define SZ(a) ((int)(a).size())
#define SQ(a) ((a)*(a))
typedef unsigned long long ull;
typedef long long ll;

enum { NUM_FACTORIAL = 1012345 };
ll f[NUM_FACTORIAL];
ll f_inv[NUM_FACTORIAL];
ll MOD = (ll)1e9 + 7LL;


// MODが素数以外でも使えるバージョン。階乗の逆元を求める。
ll extgcd(ll a, ll b, ll& x, ll& y)
{
	ll d = a;
	if (b != 0)
	{
		d = extgcd(b, a%b, y, x);
		y -= (a / b)*x;
	}
	else
	{
		x = 1;
		y = 0;
	}
	return d;
}

// 逆元を求める
ll mod_inverse(ll a, ll m)
{
	ll x, y;
	extgcd(a, m, x, y);
	return (m + x % m) % m;
}

void mod_factorial_init()
{
	f[0] = 1;
	for (int i = 1; i < NUM_FACTORIAL; ++i)
	{
		f[i] = f[i - 1] * i;
		f[i] %= MOD;
	}

	for (int i = 0; i < NUM_FACTORIAL; ++i)
	{
		f_inv[i] = mod_inverse(f[i], MOD);
	}
}


// 組み合わせを求める。事前計算O(n)で、0～nまでと範囲も広いので、MODつきならパスカルの三角形よりも良い。最有力。
ll mod_comb(ll n, ll k) {
	if (k > n || n<0 || k <0)
	{
		return 0;
	}

	return ((f[n] * f_inv[k]) % MOD * f_inv[n - k]) % MOD;
}


long long perm(int n, int r)
{
	return (f[n] * f_inv[n - r]) % MOD;
}

int main() 
{
	mod_factorial_init();
	int N, M;

	cin >> N >> M;
	vector <int> a(N), b(M);

	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
	}

	for (int i = 0; i < M; ++i)
	{
		cin >> b[i];
	}

	bool ok = true;

	sort(a.rbegin(), a.rend());
	sort(b.rbegin(), b.rend());

	for (int i = 0; i < SZ(a)-1; ++i)
	{
		if (a[i] == a[i + 1])
		{
			ok = false;
		}
	}

	for (int i = 0; i < SZ(b) - 1; ++i)
	{
		if (b[i] == b[i + 1])
		{
			ok = false;
		}
	}


	ll ans = 1;

	int y = N - 1;
	int x = M - 1;

	int used = 0;
	while (y >= 0 || x >= 0)
	{
		// BEGIN CUT HERE
	//	cerr << " y=" << y << " x=" << x << endl;
		// END CUT HERE

		if (y>=0 && x>=0 && a[y]==b[x])
		{
			ans *= perm(b[x] - 1 - used, x + y);
			ans %= MOD;
			used += x + y + 1;
			y--;
			x--;
		}
		else if (y<0 || a[y] > b[x])
		{
			// x を削る
			ans *= (y+1);
			ans %= MOD;
			ans *= perm(b[x] - 1 - used, y);
			ans %= MOD;
			used += y + 1;
			x--;
		}
		else if (x<0 || a[y] < b[x])
		{
			// y を削る
			ans *= (x + 1);
			ans %= MOD;
			ans *= perm(a[y] - 1 - used, x);
			ans %= MOD;
			used += x + 1;
			y--;
		}
		else
		{
			assert(0);
		}

		// BEGIN CUT HERE
//		cerr << " ans=" << ans << endl;
		// END CUT HERE


	}
	assert(used == N * M);

	if (ok)
	{
		cout << ans << endl;
	}
	else
	{
		cout << 0 << endl;
	}


	return 0;
}


Measured memory_bytes: 16128