#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e9;
const long long INFL = 1LL << 60;
const int mod = 1000000007;
struct mint {
  long long x;
  mint(long long x = 0) : x(x % mod) {}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod - a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint& operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};
void dump(const vector<pair<int, int>>& a) {
  for (auto v : a) {
    cerr << v.first << " " << v.second << endl;
  }
}
void dump(const vector<int>& a) {
  for (auto v : a) {
    cerr << v << " ";
  }
  cerr << endl;
}
int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> a(h, vector<int>(w, 1));
  for (int i = 0; i < h; ++i) {
    int r;
    cin >> r;
    for (int j = 0; j <= r; j++) {
      if (j >= w) {
        break;
      }
      a[i][j] = 0;
      if (j == r) {
        a[i][j] = 2;
      }
    }
  }
  for (int i = 0; i < w; ++i) {
    int c;
    cin >> c;
    for (int j = 0; j <= c; j++) {
      if (j >= h) {
        break;
      }
      if (j != c && a[j][i] == 2) {
        cout << 0 << endl;
        return 0;
      } else if (j == c && a[j][i] == 0) {
        cout << 0 << endl;
        return 0;
      }
      a[j][i] = 0;
    }
  }
  long long num = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (a[i][j] == 1) {
        num++;
      }
    }
  }
  mint x = 2;
  mint ans = x.pow(num);
  cout << ans.x << endl;
  return 0;
}
