#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<::std::uint_fast64_t mod>
class ModInt{
private:
	using value_type = ::std::uint_fast64_t;
	value_type n;
public:
	ModInt() : n(0) {}
	ModInt(value_type n_) : n(n_ % mod) {}
	ModInt(const ModInt& m) : n(m.n) {}

	explicit operator value_type() const { return n; }
	template<::std::uint_fast64_t mod_>
	friend ::std::ostream& operator<<(::std::ostream&, const ModInt<mod_>&);
	template<::std::uint_fast64_t mod_>
	friend ::std::istream& operator>>(::std::istream&, const ModInt<mod_>&);

	bool operator==(const ModInt& m) const { return n == m.n; }
	bool operator!=(const ModInt& m) const { return n != m.n; }
	ModInt& operator*=(const ModInt& m){ n = n * m.n % mod; return *this; }

	ModInt pow(value_type b) const{
		ModInt ans = 1, m = ModInt(*this);
		while(b){
			if(b & 1) ans *= m;
			m *= m;
			b >>= 1;
		}
		return ans;
	}

	ModInt inv() const { return (*this).pow(mod-2); }
	ModInt& operator+=(const ModInt& m){ n += m.n; n = (n < mod ? n : n - mod); return *this; }
	ModInt& operator-=(const ModInt& m){ n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
	ModInt& operator/=(const ModInt& m){ n = n * m.inv() % mod; return *this; }
	ModInt operator+(const ModInt& m) const { return ModInt(*this) += m; }
	ModInt operator-(const ModInt& m) const { return ModInt(*this) -= m; }
	ModInt operator*(const ModInt& m) const { return ModInt(*this) *= m; }
	ModInt operator/(const ModInt& m) const { return ModInt(*this) /= m; }
	ModInt& operator++(){ n += 1; return *this; }
	ModInt& operator--(){ n -= 1; return *this; }
	ModInt operator++(int){
		ModInt old(n);
		n += 1;
		return old;
	}
	ModInt operator--(int){
		ModInt old(n);
		n -= 1;
		return old;
	}
};

template<::std::uint_fast64_t mod>
::std::ostream& operator<<(::std::ostream& stream, const ModInt<mod>& m){
	stream << m.n;
	return stream;
}

template<::std::uint_fast64_t mod>
::std::istream& operator>>(::std::istream& stream, const ModInt<mod>& m){
	stream >> m.n;
	return stream;
}

template<::std::size_t size, ::std::uint_fast64_t mod=1000000007>
class Factorial{
private:
	using value_type = ModInt<mod>;
	::std::vector<value_type> fact, inv;
public:
	Factorial() : fact(size+1, 1), inv(size+1, 1){
		for(::std::size_t i = 1; i <= size; ++i){
			fact[i] = fact[i-1] * value_type(i);
			inv[i] = fact[i].inv();
		}
	}

	value_type comb(::std::int64_t a, ::std::int64_t b){
		assert(a >= b);
		assert(b >= 0);
		return fact[a]*inv[b]*inv[a-b];
	}

	value_type& operator[](::std::size_t k){ return fact[k]; }
};
const int64 mod = 1e9+7;

using mint = ModInt<mod>;

Factorial<1123456> f;
int32 ca[1123456] = {},cb[1123456] = {};

int main(void){
	int32 H, W;
	cin >> H >> W;
	vector<int64> a(H), b(W);
	vector<PLL> c(H+W);
	REP(i, H) cin >> a[i];
	REP(i, W) cin >> b[i];
	REP(i, H){ 
		ca[a[i]]++;
		if(ca[a[i]] > 1){ cout << 0 << endl; return 0; }
	}
	REP(i, W){ 
		cb[b[i]]++;
		if(cb[b[i]] > 1){ cout << 0 << endl; return 0; }
	}
	REP(i, H) c[i] = PLL(a[i], ca[a[i]]+cb[a[i]] < 2 ? 0 : 2);
	REP(i, W) c[i+H] = PLL(b[i], ca[b[i]]+cb[b[i]] < 2 ? 1 : 2);

	sort(all(c));
	c.erase(unique(all(c)), c.end());
	mint res = 1;
	int64 now = 1, rest = 0;
	int64 rH = H, rW = W;
	REP(i, c.size()){
		int64 n = c[i].fs;
		while(now <= n){
			rest += ca[now]+cb[now] == 0;
			now++;
		}
		if(c[i].sc == 2){
			if(rest < rH+rW-2){ cout << 0 << endl; return 0; }
			res *= f.comb(rest, rH-1)*f[rH-1];
			rest -= rH-1;
			res *= f.comb(rest, rW-1)*f[rW-1];
			rest -= rW-1;
			rH--; rW--;
		}else if(c[i].sc == 1){
			res *= rH;
			if(rest < rH-1){ cout << 0 << endl; return 0; }
			res *= f.comb(rest, rH-1)*f[rH-1];
			rest -= rH-1;
			rW--;
		}else{
			res *= rW;
			if(rest < rW-1){ cout << 0 << endl; return 0; }
			res *= f.comb(rest, rW-1)*f[rW-1];
			rest -= rW-1;
			rH--;
		}
	}
	cout << res << endl;
}


Measured memory_bytes: 22016