import java.lang.Math

fun main(args: Array<String>) {
    val mod = 1000000007L
    val N = readInputLine().toInt()
    val T = readInputLine().split(" ").map { it.toLong() }.toLongArray()
    val A = readInputLine().split(" ").map { it.toLong() }.toLongArray()

    val Tmin = LongArray(N) { 1L }
    val Tmax = LongArray(N) { 1L }

    Tmin[0] = T[0]
    Tmax[0] = T[0]

    for (i in 1 until N) {
        Tmax[i] = Math.max(Tmax[i - 1], T[i])
        if (T[i - 1] != T[i]) {
            Tmin[i] = T[i]
        }
    }

    val Amin = LongArray(N) { 1L }
    val Amax = LongArray(N) { 1L }

    Amin[N - 1] = A[N - 1]
    Amax[N - 1] = A[N - 1]

    for (i in N - 2 downTo 0) {
        Amax[i] = Math.max(Amax[i + 1], A[i])
        if (A[i] != A[i + 1]) {
            Amin[i] = A[i]
        }
    }

    var ans = 1L
    for (i in 0 until N) {
        val min = Math.max(Tmin[i], Amin[i])
        val max = Math.min(Tmax[i], Amax[i])

        if (max < min) {
            println(0)
            return
        }

        ans *= max - min + 1L
        ans %= mod
    }

    println(ans)
}

fun readInputLine(): String {
    return readLine()!!
}
