object Main extends App {
  import scala.io.StdIn._

  val n = readInt
  val t = readLine.trim.split(' ').map(_.toInt)
  val a = readLine.trim.split(' ').map(_.toInt)
  var isParadoxical = false
  val confirmed = Array.tabulate(n){_ ⇒ -1}
  confirmed(0) = t.head
  confirmed(n - 1) = a.last
  for (i ← t.indices.tail){
    if (t(i) != t(i - 1)){
      if (confirmed(i) != -1 && confirmed(i) != t(i)) isParadoxical = true
      confirmed(i) = t(i)
    }
  }
  for (i ← a.indices.reverse.tail){
    if (a(i) != a(i + 1)){
      if (confirmed(i) != -1 && confirmed(i) != a(i)) isParadoxical = true
      confirmed(i) = a(i)
    }
  }
  val mod = 1000000007L
  if (isParadoxical) println(0)
  else {
    var count = 1L
    val mountains = confirmed.indices.filter(i ⇒ confirmed(i) != -1).toArray
    for (i ← mountains.indices.tail){
      count *= expWithMod(math.min(confirmed(mountains(i)), confirmed(mountains(i - 1))), mountains(i) - mountains(i - 1) - 1)
      count %= mod
    }
    println(count)
  }
  def expWithMod(base: Long, exp: Int): Long = {
    exp match {
      case 0 ⇒ 1
      case 1 ⇒ base
      case _ ⇒ expWithMod(base, exp / 2).let(it ⇒ (it * it % mod) * expWithMod(base, exp % 2)) % mod
    }
  }

  implicit class Scope[T](val value: T) extends AnyVal {
    @inline def also(@inline func: T ⇒ Unit): T = {
      func(value)
      value
    }
    @inline def let[A](@inline func: T ⇒ A): A = func(value)
  }
}