#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
char arr[N][N];
const long long mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) arr[i][j] = 'Z';
  for (int i = 0; i < h; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) arr[i][j] = 'B';
    arr[i][x] = 'X';
  }
  for (int i = 0; i < w; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) {
      if (arr[j][i] != 'Z' && arr[j][i] != 'B') return cout << 0, 0;
      arr[j][i] = 'B';
    }
    if (x >= h) continue;
    if (arr[x][i] != 'Z' && arr[x][i] != 'X') return cout << 0, 0;
    arr[x][i] = 'X';
  }
  long long res = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (arr[i][j] == 'Z') {
        res *= 2ll;
        res %= mod;
      }
    }
  }
  cout << res;
  return 0;
}
