#include <bits/stdc++.h>
using namespace std;
template <typename _KTy, typename _Ty>
ostream& operator<<(ostream& o, const pair<_KTy, _Ty>& m) {
  o << "{" << m.first << ", " << m.second << "}";
  return o;
}
template <typename _KTy, typename _Ty>
ostream& operator<<(ostream& o, const map<_KTy, _Ty>& m) {
  if (m.empty()) {
    o << "{ }";
    return o;
  }
  o << "{" << *m.begin();
  for (auto itr = ++m.begin(); itr != m.end(); itr++) {
    o << ", " << *itr;
  }
  o << "}";
  return o;
}
template <typename _KTy, typename _Ty>
ostream& operator<<(ostream& o, const unordered_map<_KTy, _Ty>& m) {
  if (m.empty()) {
    o << "{ }";
    return o;
  }
  o << "{" << *m.begin();
  for (auto itr = ++m.begin(); itr != m.end(); itr++) {
    o << ", " << *itr;
  }
  o << "}";
  return o;
}
template <typename _Ty>
ostream& operator<<(ostream& o, const vector<_Ty>& v) {
  if (v.empty()) {
    o << "{ }";
    return o;
  }
  o << "{" << v.front();
  for (auto itr = ++v.begin(); itr != v.end(); itr++) {
    o << ", " << *itr;
  }
  o << "}";
  return o;
}
template <typename _Ty>
ostream& operator<<(ostream& o, const set<_Ty>& s) {
  if (s.empty()) {
    o << "{ }";
    return o;
  }
  o << "{" << *(s.begin());
  for (auto itr = ++s.begin(); itr != s.end(); itr++) {
    o << ", " << *itr;
  }
  o << "}";
  return o;
}
template <typename _Ty>
ostream& operator<<(ostream& o, const unordered_set<_Ty>& s) {
  if (s.empty()) {
    o << "{ }";
    return o;
  }
  o << "{" << *(s.begin());
  for (auto itr = ++s.begin(); itr != s.end(); itr++) {
    o << ", " << *itr;
  }
  o << "}";
  return o;
}
template <typename _Ty>
ostream& operator<<(ostream& o, const stack<_Ty>& s) {
  if (s.empty()) {
    o << "{ }";
    return o;
  }
  stack<_Ty> t(s);
  o << "{" << t.top();
  t.pop();
  while (!t.empty()) {
    o << ", " << t.top();
    t.pop();
  }
  o << "}";
  return o;
}
template <typename _Ty>
ostream& operator<<(ostream& o, const list<_Ty>& l) {
  if (l.empty()) {
    o << "{ }";
    return o;
  }
  o << "{" << l.front();
  for (auto itr = ++l.begin(); itr != l.end(); ++itr) {
    o << ", " << *itr;
  }
  o << "}";
  return o;
}
template <typename _KTy, typename _Ty>
istream& operator>>(istream& is, pair<_KTy, _Ty>& m) {
  is >> m.first >> m.second;
  return is;
}
template <typename _Ty>
istream& operator>>(istream& is, vector<_Ty>& v) {
  for (size_t i = 0; i < v.size(); i++) is >> v[i];
  return is;
}
namespace aux {
template <typename Ty, unsigned N, unsigned L>
struct tp {
  static void print(ostream& os, const Ty& v) {
    os << get<N>(v) << ", ";
    tp<Ty, N + 1, L>::print(os, v);
  }
};
template <typename Ty, unsigned N>
struct tp<Ty, N, N> {
  static void print(ostream& os, const Ty& v) { os << get<N>(v); }
};
}  // namespace aux
template <typename... Tys>
ostream& operator<<(ostream& os, const tuple<Tys...>& t) {
  os << "{";
  aux::tp<tuple<Tys...>, 0, sizeof...(Tys) - 1>::print(os, t);
  os << "}";
  return os;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T& val) {
  std::fill((T*)array, (T*)(array + N), val);
}
void dump_func() { cerr << endl; }
template <class Head, class... Tail>
void dump_func(Head&& head, Tail&&... tail) {
  cerr << head;
  if (sizeof...(Tail) == 0) {
    cerr << " ";
  } else {
    cerr << ", ";
  }
  dump_func(std::move(tail)...);
}
long long MOD = 1000000007;
long long modfac[1000100];
long long modinv[1000100];
template <typename Ty>
void ExEuclid(Ty a, Ty b, Ty& gcd, Ty& x, Ty& y) {
  Ty r1, r2, r3, x1, x2, x3, y1, y2, y3, q;
  r1 = a;
  x1 = 1;
  y1 = 0;
  r2 = b;
  x2 = 0;
  y2 = 1;
  while (r2 != 0) {
    r3 = r1 % r2;
    q = r1 / r2;
    x3 = x1 - q * x2;
    y3 = y1 - q * y2;
    r1 = r2;
    r2 = r3;
    x1 = x2;
    x2 = x3;
    y1 = y2;
    y2 = y3;
  }
  gcd = r1;
  x = x1;
  y = y1;
  if (gcd < 0) {
    gcd *= -1;
    x *= -1;
    y *= -1;
  };
}
template <typename Ty>
Ty invmod(Ty a, Ty m) {
  Ty gcd, x, q;
  ExEuclid(a, -m, gcd, x, q);
  if (gcd != 1) return -1;
  return x;
}
long long nPkModP(long long n, long long k) {
  return modfac[n] * modinv[k] % MOD;
}
bool isDuplicated(const vector<long long>& v) {
  vector<bool> flags(1000100, false);
  for (int i = 0; i < v.size(); i++) {
    if (flags[v[i]]) return true;
    flags[v[i]] = true;
  }
  return false;
}
vector<long long> cand;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, M;
  cin >> N >> M;
  vector<long long> A(N), B(M);
  cin >> A >> B;
  if (isDuplicated(A) || isDuplicated(B)) {
    cout << 0 << endl;
    return 0;
  }
  cand = vector<long long>(1000100, 1);
  cand[0] = 0;
  map<long long, long long> cnt;
  for (int i = (0); i < (N); ++i)
    for (int j = (0); j < (M); ++j) {
      if (A[i] == B[j])
        cand[A[i]] = 0;
      else
        cnt[min(A[i], B[j])]++;
    }
  for (int i = (0); i < (1000050); ++i) cand[i + 1] += cand[i];
  long long T = N * M;
  {
    modfac[0] = 1;
    long long x = 1;
    for (long long n = 1; n <= T; n++) {
      x *= n;
      x %= MOD;
      modfac[n] = x;
    }
  }
  {
    modinv[T] = (invmod(modfac[T], MOD) + MOD) % MOD;
    for (long long k = T - 1; k >= 0; k--) {
      modinv[k] = modinv[k + 1] * (k + 1) % MOD;
    }
  }
  long long ans = 1;
  long long numCandidates = 0;
  long long prev = 0;
  for (auto e : cnt) {
    long long ub = e.first;
    numCandidates += cand[ub] - cand[prev];
    prev = ub;
    long long numToAssign = e.second;
    if (numCandidates < numToAssign) {
      cout << 0 << endl;
      return 0;
    }
    ans *= modfac[numCandidates] * modinv[numCandidates - numToAssign] % MOD;
    ans %= MOD;
    numCandidates -= numToAssign;
  }
  cout << ans << endl;
  return 0;
}
