#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep2(i, m, n) for(int i=int(m); i<int(n); i++)
#define rep(i, n) rep2(i, 0, n)
#define all(a) a.begin(), a.end()
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
const int INF = 1<<30;
const ll INFll = 1ll<<62;
const ld EPS = 1e-10;
const int MOD = int(1e9)+7;
template<typename T> inline bool chmin(T& a, T b) {if(a>b) {a=b; return true;} return false;}
template<typename T> inline bool chmax(T& a, T b) {if(a<b) {a=b; return true;} return false;}


template<int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0) val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator - () const noexcept {
    return val ? MOD - val : 0;
  }
  constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp& operator += (const Fp& r) noexcept {
    val += r.val;
    if (val >= MOD) val -= MOD;
    return *this;
  }
  constexpr Fp& operator -= (const Fp& r) noexcept {
    val -= r.val;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr Fp& operator *= (const Fp& r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp& operator /= (const Fp& r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr bool operator == (const Fp& r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator != (const Fp& r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0) return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1) t = t * a;
    return t;
  }
};
using mint = Fp<MOD>;


mint perm(int a, int b) {
  if (a < b) return 0;
  mint res = 1;
  rep(i, b) res *= a - i;
  return res;
}


int main() {
  int n, m; cin >> n >> m;
  VV ab(2, V());
  rep(i, n) {
    int a; cin >> a;
    ab[0].push_back(a);
  }
  rep(i, m) {
    int a; cin >> a;
    ab[1].push_back(a);
  }
  sort(all(ab[0]));
  sort(all(ab[1]));

  rep(i, n-1) {
    if (ab[0][i] == ab[0][i+1]) {
      cout << 0 << endl;
      return 0;
    }
  }
  rep(i, m-1) {
    if (ab[1][i] == ab[1][i+1]) {
      cout << 0 << endl;
      return 0;
    }
  }

  map<int, pair<int, bool>> cnt;
  int x = 0;
  for (auto e : ab[x]) {
    int num = ab[1-x].end() - lower_bound(all(ab[1-x]), e);
    cnt[e] = make_pair(num, false);
  }
  x = 1;
  for (auto e : ab[x]) {
    int num = ab[1-x].end() - lower_bound(all(ab[1-x]), e);
    if (cnt.count(e) == 0) {
      cnt[e] = make_pair(num, false);
    }
    else {
      cnt[e] = make_pair(cnt[e].first + num - 1, true);
    }
  }

  mint ans = 1;
  int cum = 0;

  for (auto e : cnt) {
    int a; pair<int, bool> p;
    tie(a, p) = e;
    int num; bool dbl;
    tie(num, dbl) = p;

    ans *= perm(a-cum-1, num-1);
    if (!dbl) ans *= num;

    cum += num;
  }

  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 384