#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    int r, c;
    cin >> r >> c;
    int row[r], col[c];
    for (int i = 0; i < r; i++) cin >> row[i];
    for (int i = 0; i < c; i++) cin >> col[i];
    vector<vector<int> > a(r, vector<int>(c, 0));
    long long cnt = 0;
    bool flag = true;
    for (int i = 0; i < r; i++) {
      if (row[i] == 0) a[i][0] = 1;
      for (int j = 0; j < row[i]; j++) {
        a[i][j] = 2;
        if (j == row[i] - 1 && j < c - 1) a[i][j + 1] = -1;
      }
    }
    for (int i = 0; i < c; i++) {
      if (col[i] == 0) {
        if (a[0][i] != 2)
          a[0][i] = 1;
        else
          flag = false;
      }
      for (int j = 0; j < col[i]; j++) {
        if (a[j][i] == 0 || a[j][i] == 2)
          a[j][i] = 2;
        else {
          flag = false;
          break;
        }
        if (j == col[i] - 1 && j < r - 1) {
          if (a[j + 1][i] != 2)
            a[j + 1][i] = -1;
          else {
            flag = false;
            break;
          }
        }
      }
      if (!flag) break;
    }
    if (flag) {
      for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++)
          if (a[i][j] == 0) cnt++;
      cnt = binpow(2, cnt, mod);
    }
    cout << cnt << "\n";
  }
  return 0;
}
