#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const long long mod = 1000000007;
int grid[maxn][maxn];
inline void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
}
int main() {
  fast();
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) grid[i][j] = 1;
    grid[i][x] = 2;
  }
  for (int j = 0; j < w; j++) {
    int x;
    cin >> x;
    for (int i = 0; i < x; i++) {
      if (grid[i][j] == 2) {
        cout << 0 << endl;
        return 0;
      }
      grid[i][j] = 1;
    }
    if (grid[x][j] == 1) {
      cout << 0 << endl;
      return 0;
    }
    grid[x][j] = 2;
  }
  int ans = 1;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == 0) {
        ans = ans * 2 % mod;
      }
    }
  cout << ans << endl;
  return 0;
}
