#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 1000 + 5;
int grid[maxn][maxn];
long long modpow(long long base, long long power) {
  long long res = 1;
  while (power) {
    if (power & 1) res = res * base % mod;
    base = base * base % mod;
    power >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  bool flag = true;
  int tmp;
  for (int i = 0; i < h; ++i) {
    cin >> tmp;
    for (int j = 0; j < tmp; ++j) grid[i][j] = 1;
    grid[i][tmp] = -1;
  }
  for (int i = 0; i < w && flag; ++i) {
    cin >> tmp;
    for (int j = 0; j < tmp && flag; ++j) {
      if (grid[j][i] == -1) {
        flag = false;
      } else
        grid[j][i] = 1;
    }
    if (grid[tmp][i] == 1)
      flag = false;
    else
      grid[tmp][i] = -1;
  }
  if (!flag) {
    cout << "0\n";
    return 0;
  }
  int cnt = 0;
  for (int i = 1; i < h; ++i)
    for (int j = 1; j < w; ++j)
      if (!grid[i][j]) ++cnt;
  cout << modpow(2LL, (long long)cnt) << '\n';
  return 0;
}
