#include <bits/stdc++.h>
using namespace std;
int grid[1000][1000] = {0};
const int k = 1e9 + 7;
int main() {
  int h, w;
  cin >> h >> w;
  vector<int> r(h), c(w);
  for (int i = 0; i < h; i++) cin >> r[i];
  for (int i = 0; i < w; i++) cin >> c[i];
  for (int i = 0; i < h; i++) {
    int k = 0, cnt = r[i];
    if (r[i] == 0) {
      grid[i][k++] = -1;
      continue;
    }
    while (cnt) {
      grid[i][k++] = 1;
      cnt--;
    }
    if (k < w) grid[i][k++] = -1;
  }
  for (int i = 0; i < w; i++) {
    int k = 0, cnt = c[i];
    if (c[i] == 0) {
      if (grid[k][i] == 1) {
        cout << 0;
        return 0;
      }
      grid[k++][i] = -1;
      continue;
    }
    while (cnt) {
      if (grid[k][i] == -1) {
        cout << 0;
        return 0;
      }
      grid[k++][i] = 1;
      cnt--;
    }
    if (k < h) {
      if (grid[k][i] == 1) {
        cout << 0;
        return 0;
      } else
        grid[k][i] = -1;
    }
  }
  int cnt = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (grid[i][j] == 0) cnt++;
  int ans = 1;
  for (int i = 0; i < cnt; i++) ans = (ans * 2) % k;
  cout << ans;
  return 0;
}
