#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);
const int MX = 6e5 + 5;
long long mod = 1e9 + 7;
int main() {
  int r, c;
  cin >> r >> c;
  vector<vector<int>> v(r);
  for (int i = 0; i < r; i++) {
    v[i].resize(c, 2);
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) v[i][j] = 1;
    if (x < c) v[i][x] = 0;
  }
  for (int i = 0; i < c; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) {
      if (v[j][i] == 1 or v[j][i] == 2) {
        v[j][i] = 1;
      } else {
        cout << "0\n";
        return 0;
      }
    }
    if (x < r) {
      if (v[x][i] == 2 or v[x][i] == 0)
        v[x][i] = 0;
      else {
        cout << "0\n";
        return 0;
      }
    }
  }
  int res = 1;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      if (v[i][j] == 2) res = (res + res) % mod;
  cout << res << '\n';
  return 0;
}
