#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<int> r, c;
  for (int i = 0; i < h; i++) {
    int x;
    cin >> x;
    r.push_back(x);
  }
  for (int i = 0; i < w; i++) {
    int x;
    cin >> x;
    c.push_back(x);
  }
  vector<vector<int>> m(h, vector<int>(w, -1));
  int ans = h * w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < r[i]; j++) {
      m[i][j] = 1;
      ans--;
    }
    if (r[i] < w) {
      m[i][r[i]] = 0;
      ans--;
    }
  }
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < c[i]; j++) {
      if (m[j][i] == 0) {
        cout << 0;
        return 0;
      }
      if (m[j][i] == -1) {
        m[j][i] = 1;
        ans--;
      }
    }
    if (c[i] < h) {
      if (m[c[i]][i] == 1) {
        cout << 0;
        return 0;
      }
      if (m[c[i]][i] == -1) {
        m[c[i]][i] = 0;
        ans--;
      }
    }
  }
  int fin = 1;
  while (ans > 0) {
    fin = ((fin % 1000000007) * 2) % 1000000007;
    ans--;
  }
  cout << fin;
  return 0;
}
