#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, m, x, cnt = 0;
  bool check = false;
  long long int ans = 2;
  cin >> n >> m;
  int a[n + 1][m + 1];
  memset(a, 0, sizeof(a));
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 0; j < x; j++) {
      a[i][j] = 1;
    }
    if (x < m) {
      a[i][x] = 2;
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> x;
    for (int j = 0; j < x; j++) {
      if (a[j][i] == 2) {
        check = true;
      }
      a[j][i] = 1;
    }
    if (x < n) {
      if (a[x][i] == 1) {
        check = true;
      } else
        a[x][i] = 2;
    }
  }
  if (check) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == 0) {
        int flag = 0;
        for (int k = j; k >= 0; k--) {
          if (a[i][k] == 0) {
            flag = 1;
            break;
          }
        }
        if (flag) {
          flag = 0;
          for (int k = i; k >= 0; k--) {
            if (a[k][j] == 0) {
              flag = 1;
              break;
            }
          }
        }
        if (flag) {
          cnt++;
        }
      }
    }
  }
  if (cnt == 0) {
    cout << 1 << endl;
  } else if (cnt == 1) {
    cout << 2 << endl;
  } else {
    for (int i = 1; i < cnt; i++) {
      ans = ans * 2;
      ans %= 1000000007;
    }
    cout << ans << endl;
  }
}
