#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <cfloat>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

int64_t EuclidMod(int64_t v, int64_t m){
  if(0 <= v && v < m){
    return v;
  }else if(-m <= v && v < 0){
    return v + m;
  }else{
    int64_t result = v % m;
    if(result < 0){
      result += m;
    }
    return result;
  }
}

int64_t ModInv(int64_t a, int64_t m){
  int64_t b = m, u = 1, v = 0;
  while(b){
    int64_t t = a / b;
    a -= t * b;
    std::swap(a, b);
    u -= t * v;
    std::swap(u, v);
  }
  EuclidMod(u, m);
  return u;
}

// return base^exponent (MOD. mod).
int64_t RepeatedPowMod(int64_t base, int64_t exponent, int64_t mod) {
  if (exponent == 0)
    return 1;
  else if (exponent % 2 == 0) {
    int64_t root = RepeatedPowMod(base, exponent / 2, mod);
    return (root * root) % mod;
  } else {
    return (base * RepeatedPowMod(base, exponent - 1, mod)) % mod;
  }
}

template<int64_t mod>
class ModInt{
  int64_t value_;

public:
  explicit ModInt(const int64_t x = 0): value_(x % mod){
  };

  ModInt& operator=(const ModInt& another){
    value_=another.value_;
    return *this;
  }

  ModInt& operator^=(const ModInt& another){
    value_=RepeatedPowMod(value_,another.value_,mod);
    return *this;
  }

  ModInt& operator+=(const ModInt& another){
    value_ += another.value_;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt& operator-=(const ModInt& another){
    value_ -= another.value_;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt& operator*=(const ModInt& another){
    value_ *= another.value_;
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt& operator/=(const ModInt& another){
    value_ = value_ * ModInv(another.value_, mod);
    value_ = EuclidMod(value_, mod);
    return *this;
  }

  ModInt& operator++(){
    value_ += 1;
    value_=EuclidMod(value_,mod);
    return *this;
  }

  ModInt operator++(int){
    ModInt tmp = *this;
    this->value_+=1;
    this->value_=EuclidMod(this->value_,mod);
    return tmp;
  }

  ModInt& operator--(){
    value_ -= 1;
    value_=EuclidMod(value_,mod);
    return *this;
  }

  ModInt operator--(int){
    ModInt tmp=*this;
    this->value_-=1;
    this->value_=EuclidMod(this->value_,mod);
    return tmp;
  }



  int64_t GetValue() const{
    return value_;
  }

};

template<int64_t mod>
ModInt<mod>
operator+(const ModInt<mod>& left, const ModInt<mod>& right){
  return ModInt<mod>(left) += right;
}

template<int64_t mod>
ModInt<mod>
operator-(const ModInt<mod>& left, const ModInt<mod>& right){
  return ModInt<mod>(left) -= right;
}

template<int64_t mod>
ModInt<mod>
operator/(const ModInt<mod>& left, const ModInt<mod>& right){
  return ModInt<mod>(left) /= right;
}

template<int64_t mod>
ModInt<mod>
operator*(const ModInt<mod>& left, const ModInt<mod>& right){
  return ModInt<mod>(left) *= right;
}

template<int64_t mod>
ModInt<mod>
operator^(const ModInt<mod>& left, const ModInt<mod>& right){
  return ModInt<mod>(left) ^= right;
}

template<int64_t mod>
std::ostream& operator<<(std::ostream& stream, const ModInt<mod>& mod_int){
  stream << mod_int.GetValue();
  return stream;
}

int main(){
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n,m;
  cin>>n>>m;
  std::vector<int> a(n),b(m),limit(n*m+1);
  std::vector<int> appear_count(n*m+1);
  for(int i=0;i<n;i++){
    cin>>a[i];
    appear_count[a[i]]++;
  }
  for(int j=0;j<m;j++){
    cin>>b[j];
    appear_count[b[j]]++;
  }
  for(auto va:a){
    for(auto vb:b){
      limit[std::min(va,vb)]++;
    }
  }

  std::vector<int> place_pos(n*m+1);
  place_pos[n*m]=limit[n*m];
  for(int i=n*m-1;i>=1;i--){
    place_pos[i]=place_pos[i+1]+limit[i];
  }




  const int64_t MOD=1000000007;

  ModInt<MOD> result(1);
  for(int x=n*m;x>=1;x--){
    if(appear_count[x]==1){
      int64_t pos=0;
      bool calced=false;
      for(int r=0;r<n;r++){
        if(a[r]==x){
          for(int c=0;c<m;c++){
            if(b[c]>=x){
              pos++;
            }
          }
          calced=true;
          result*=ModInt<MOD>(pos);
          break;
        }
      }
      if(calced) continue;
      for(int c=0;c<m;c++){
        if(b[c]==x){
          for(int r=0;r<n;r++){
            if(a[r]>=x){
              pos++;
            }
          }
          result*=ModInt<MOD>(pos);
          break;
        }
      }
    }else if(appear_count[x]==2){
      continue;
    }else if(appear_count[x]>=3){
      cout<<0<<endl;
      return 0;
    }else{
      int64_t pos=(place_pos[x]-(n*m-x));
      if(pos<=0){
        cout<<0<<endl;
        return 0;
      }
      result*=ModInt<MOD>(pos);
    }




  }

  cout<<result<<endl;


  return 0;
}

Measured memory_bytes: 12032