import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)
  val MOD = 1e9.toLong + 7

  val N, M = in.next().toInt
  val A = Array.fill[Long](N+2)(Int.MaxValue)
  val B = Array.fill[Long](M+2)(Int.MaxValue)
  A(N+1) = -1
  B(M+1) = -1

  for(i <- 1 to N)
    A(i) = in.next().toLong
  for(i <- 1 to M)
    B(i) = in.next().toLong


  //
  if(A.toSet.size != A.length || B.toSet.size != B.length)
    println(0)
  else {
    // val dpx = new Array[Int](N+1)
    // val dpy = new Array[Int](M+1)

    val X = A.sorted.reverse
    val Y = B.sorted.reverse


    var y = 0; var x = 0
    var ans = 1L

    // X(x) Y(y)
    breakable {
      for (num <- (1 to N*M).reverse) {
        if(X(x+1) == num && Y(y+1) == num) {
          x += 1
          y += 1
        }
        else if(X(x+1) == num) {
          x += 1
          ans *= y
          ans %= MOD
        }
        else if(Y(y+1) == num) {
          y += 1
          ans *= x
          ans %= MOD
        }
        else {
          val index = (N*M - num) + 1  // 上から何番目かを表す
          if(index > x * y) { // 書き込めない場合
            ans = 0L
            break
          }
          else {
            ans *= (x * y) - (index - 1)
            ans %= MOD
          }
        }
      }
    }

    println(ans)
  }
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}

