import scala.annotation.tailrec
import scala.collection.mutable

object Main extends App {

  import scala.io.StdIn._

  val mod = 1000000007
  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val state = Array.tabulate(n * m){_ ⇒ Free: State}
  for ((r, i) ← readLine.trim.split(' ').map(_.toInt).zipWithIndex){
    state(r - 1) = state(r - 1).addRow(i)
  }
  for ((c, i) ← readLine.trim.split(' ').map(_.toInt).zipWithIndex){
    state(c - 1) = state(c - 1).addColumn(i)
  }
  println(solve(state))
  def solve(state: Array[State]): Long = {
    val rowFree = Array.tabulate(n){_ ⇒ true}
    val columnFree = Array.tabulate(m){_ ⇒ true}
    @tailrec
    def sub(state: List[State], r: Int = 0, c: Int = 0, f: Int = 0, prev: Long = 1): Long = {
      state match {
        case Nil ⇒ prev
        case RC(row, column)::t if rowFree(row) && columnFree(column) ⇒
          rowFree(row) = false
          columnFree(column) = false
          sub(t, r + 1, c + 1, f + c + r, prev)
        case R(row)::t if rowFree(row) && c > 0 ⇒
          rowFree(row) = false
          sub(t, r + 1, c, f + c - 1, (c * prev) % mod)
        case C(column)::t if columnFree(column) && r > 0 ⇒
          columnFree(column) = false
          sub(t, r, c + 1, f + r - 1, (r * prev) % mod)
        case Free::t if f > 0 ⇒ sub(t, r, c, f - 1, (f * prev) % mod)
        case _ ⇒ 0L
      }
    }
    sub(state.reverse.toList)
  }

  sealed trait State {
    def addRow(row: Int): State = {
      this match {
        case C(c) ⇒ RC(row, c)
        case Free ⇒ R(row)
        case _ ⇒ Failure
      }
    }
    def addColumn(column: Int): State = {
      this match {
        case R(r) ⇒ RC(r, column)
        case Free ⇒ C(column)
        case _ ⇒ Failure
      }
    }
  }
  case class R(row: Int) extends State
  case class C(column: Int) extends State
  case class RC(row: Int, column: Int) extends State
  case object Free extends State
  case object Failure extends State
  implicit class Extension[T](value :T){
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}