#include <bits/stdc++.h>
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  std::uint64_t h, w;
  std::cin >> h >> w;
  std::vector<std::uint64_t> r, c;
  r.reserve(h);
  c.reserve(w);
  for (std::uint64_t i = 0; i < h; ++i) {
    std::uint64_t value;
    std::cin >> value;
    r.push_back(value);
  }
  for (std::uint64_t i = 0; i < w; ++i) {
    std::uint64_t value;
    std::cin >> value;
    c.push_back(value);
  }
  std::uint64_t ans = 1;
  for (std::uint64_t i = 0; i < h; ++i) {
    for (std::uint64_t j = 0; j < w; ++j) {
      if ((r[i] == j && c[j] > i) || (c[j] == i && r[i] > j)) {
        std::cout << 0;
        return 0;
      }
      if (r[i] < j && c[j] < i) {
        ans *= 2;
        ans %= 1000000007;
      }
    }
  }
  std::cout << ans;
  return 0;
}
