#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
  a %= 1000000007;
  long long res = 1;
  while (b > 0) {
    if (b & 1) {
      res = res * a % 1000000007;
    }
    a = a * a % 1000000007;
    b >>= 1;
  }
  return res;
}
int main() {
  int h, w;
  cin >> h >> w;
  int arr[h][w];
  memset(arr, 0, sizeof(arr));
  int r, c, flag = 0, count = 0;
  for (int i = 0; i < h; i++) {
    cin >> r;
    if (r == 0) {
      if (arr[i][r] == -1) {
        cout << "0"
             << "\n";
        break;
      } else if (arr[i][r] == 0) {
        arr[i][r] = 1;
        count++;
      }
    }
    for (int j = 0; j < r; j++) {
      if (r < w) {
        if (arr[i][r] == -1) {
          flag = 1;
          cout << "0"
               << "\n";
          break;
        } else if (arr[i][r] == 0) {
          arr[i][r] = 1;
          count++;
        }
      }
      if (arr[i][j] == 1) {
        flag = 1;
        cout << "0"
             << "\n";
        break;
      } else if (arr[i][j] == 0) {
        arr[i][j] = -1;
        count++;
      }
    }
    if (flag == 1) break;
  }
  for (int i = 0; i < w; i++) {
    cin >> c;
    if (c == 0) {
      if (arr[c][i] == -1) {
        cout << "0"
             << "\n";
        flag = 1;
        break;
      } else if (arr[c][i] == 0) {
        arr[c][i] = 1;
        count++;
      }
    }
    for (int j = 0; j < c; j++) {
      if (c < h) {
        if (arr[c][i] == -1) {
          flag = 1;
          cout << "0"
               << "\n";
          break;
        } else if (arr[c][i] == 0) {
          arr[c][i] = 1;
          count++;
        }
      }
      if (arr[j][i] == 1) {
        flag = 1;
        cout << "0"
             << "\n";
        break;
      } else if (arr[j][i] == 0) {
        arr[j][i] = -1;
        count++;
      }
    }
    if (flag == 1) break;
  }
  if (flag == 0) {
    long long ans =
        ((h % 1000000007 * w % 1000000007) % 1000000007 - count % 1000000007) %
        1000000007;
    cout << power(2, ans) % 1000000007 << "\n";
  }
}
