#include <bits/stdc++.h>
const int N = 1010, P = 1e9 + 7;
int f[N][N];
int pwr(int a, int b) {
  int r = 1;
  for (; b; a = 1ll * a * a % P, b >>= 1) b& 1 ? r = 1ll * r * a % P : 0;
  return r;
}
int main() {
  int n, m, ans = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= (n); ++i) {
    int x;
    scanf("%d", &x);
    for (int j = 1; j <= (x); ++j) f[i][j] = 1;
    f[i][x + 1] = -1;
  }
  for (int j = 1; j <= (m); ++j) {
    int x;
    scanf("%d", &x);
    for (int i = 1; i <= (x); ++i) {
      if (f[i][j] == -1) {
        puts("0");
        return 0;
      }
      f[i][j] = 1;
    }
    if (f[x + 1][j] == 1) {
      puts("0");
      return 0;
    }
    f[x + 1][j] = -1;
  }
  for (int i = 1; i <= (n); ++i)
    for (int j = 1; j <= (m); ++j) ans += f[i][j] == 0;
  printf("%d", pwr(2, ans));
  return 0;
}
