object Main {
  def main(args: Array[String]): Unit = {
    var n = io.StdIn.readLine().toInt
    val t = io.StdIn.readLine().split(' ').map(_.toInt)
    val a = io.StdIn.readLine().split(' ').map(_.toInt)

    val h = scala.collection.mutable.ListBuffer.empty[Int]

    if (t(0) > a(0)) {
      println(0)
      return
    } else {
      if (n > 1 && a(0) > a(1) && t(0) != a(0)) {
        println(0)
        return
      } else {
        h += t(0)
      }
    }

    for (i <- 1 to (n - 2)) {
      val ht = if (t(i - 1) < t(i)) t(i) else 0
      val ha = if (a(i) > a(i + 1)) a(i) else 0
      if (ht == 0) {
        h += ha
      } else if (ha == 0) {
        h += ht
      } else if (ht == ha) {
        h += ha
      } else {
        println(0)
        return
      }
    }

    if (t(n - 1) < a(n - 1)) {
      println(0)
      return
    } else {
      if (n > 1 && t(n - 2) < t(n - 1) && t(n - 1) != a(n - 1)) {
        println(0)
        return
      } else {
        h += a(n - 1)
      }
    }

    val candidate = scala.collection.mutable.ListBuffer.empty[Int]
    var j = 0
    while (j < n) {
      if (h(j) > 0) {
        j += 1
      } else {
        var k = j
        while (h(k) == 0) {
          k += 1
        }
        for (n <- 1 to (k - j)) {
          candidate += Math.min(h(j - 1), h(k))
        }
        j = k
      }
    }

    val p = Math.pow(10, 9).toLong + 7

    val res = candidate.foldLeft(1L) { (acc, h) =>
      (acc * (h.toLong % p)) % p
    }

    println(res)
  }

}

