#include <bits/stdc++.h>
using namespace std;
void fill(const vector<int> &r, const vector<int> &c,
          vector<vector<bool> > &grid) {
  for (int i = 0; i < (int)r.size(); ++i) {
    for (int j = 0; j < r[i]; ++j) {
      grid[i][j] = true;
    }
  }
  for (int j = 0; j < (int)c.size(); ++j) {
    for (int i = 0; i < c[j]; ++i) {
      grid[i][j] = true;
    }
  }
}
bool check(const vector<int> &r, const vector<int> &c,
           const vector<vector<bool> > &grid) {
  for (int i = 0; i < (int)r.size(); ++i) {
    for (int j = 0; j < r[i]; ++j) {
      if (!grid[i][j]) return false;
    }
    if (r[i] < (int)grid[0].size() && grid[i][r[i]]) return false;
  }
  for (int j = 0; j < (int)c.size(); ++j) {
    for (int i = 0; i < (int)grid.size(); ++i) {
      if (!grid[i][j] && i < c[j]) return false;
    }
    if (c[j] < (int)grid.size() && grid[c[j]][j]) return false;
  }
  return true;
}
bool affect(const vector<vector<bool> > &grid, const int r, const int c) {
  bool flag1 = true;
  for (int row = r - 1; row > -1; --row) {
    if (!grid[row][c]) {
      flag1 = false;
      break;
    }
  }
  bool flag2 = true;
  for (int col = c - 1; col > -1; --col) {
    if (!grid[r][col]) {
      flag2 = false;
      break;
    }
  }
  if (!flag1 && !flag2) return false;
  return true;
}
int count(const vector<int> &r, const vector<int> &c,
          const vector<vector<bool> > &grid) {
  int cnt = 0;
  for (int i = 1; i < (int)grid.size(); ++i) {
    for (int j = 1; j < (int)grid[i].size(); ++j) {
      if (!grid[i][j] && !affect(grid, i, j)) ++cnt;
    }
  }
  return cnt;
}
long long getAns(int cnt) {
  long long ans = 1;
  for (int i = 0; i < cnt; ++i) {
    ans *= 2;
    ans %= 1000000007;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  vector<vector<bool> > grid(h, vector<bool>(w, false));
  vector<int> r(h, 0), c(w, 0);
  for (int i = 0; i < h; ++i) {
    cin >> r[i];
  }
  for (int i = 0; i < w; ++i) {
    cin >> c[i];
  }
  fill(r, c, grid);
  if (!check(r, c, grid))
    cout << 0 << '\n';
  else
    cout << getAns(count(r, c, grid)) << '\n';
  return 0;
}
