function exec(N, M, A, B) {
  const a_max = Math.max.apply(null, A);
  const b_max = Math.max.apply(null, B);
  const max = Math.max(a_max, b_max);

  const A_Set = new Set(A);
  const B_Set = new Set(B);

  const mod = 10 ** 9 + 7;

  const already_A = [];
  const already_B = [];

  let ans = 1;

  for (let i = max; i > 0; i--) {
    const a_exists = A_Set.has(i);
    const b_exists = B_Set.has(i);
    if (a_exists && b_exists) {
      already_A.push(i);
      already_B.push(i);
    } else if (a_exists) {
      already_A.push(i);
      ans = ans * already_B.length;
      ans = ans % mod;
    } else if (b_exists) {
      already_B.push(i);
      ans = ans * already_A.length;
      ans = ans % mod;
    } else {
      let tmp = ((already_A.length * already_B.length) % mod) - (N * M - i);
      if (tmp < 0) {
        tmp += mod;
      }
      ans = (ans * tmp) % mod;
    }
  }

  return ans % mod;
}

function main(input) {
  let array = input.split("\n");
  let NM_array = array[0].split(" ").map(item => parseInt(item));
  let A_array = array[1].split(" ").map(item => parseInt(item));
  let B_array = array[2].split(" ").map(item => parseInt(item));
  return exec(NM_array[0], NM_array[1], A_array, B_array);
}

const input = require("fs").readFileSync("/dev/stdin", "utf8");
console.log(main(input));
