local n = io.read("*n")
local tmin, tmax = {}, {}
local t, a = {}, {}
local amin, amax = {}, {}

for i = 1, n do t[i] = io.read("*n") end
for i = 1, n do a[i] = io.read("*n") end
for i = 1, n do
  if i == 1 then
    tmin[i], tmax[i] = t[1], t[1]
  else
    if t[i - 1] < t[i] then
      tmin[i], tmax[i] = t[i], t[i]
    else
      tmin[i], tmax[i] = 1, tmax[i - 1]
    end
  end
end
for i = 1, n do amin[i], amax[i] = 0, 0 end
for i = n, 1, -1 do
  if i == n then
    amin[i], amax[i] = a[i], a[i]
  else
    if a[i + 1] < a[i] then
      amin[i], amax[i] = a[i], a[i]
    else
      amin[i], amax[i] = 1, amax[i + 1]
    end
  end
end
local ret = 1
local mmi, mma = math.min, math.max
for i = 1, n do
  local l = mma(tmin[i], amin[i])
  local r = mmi(tmax[i], amax[i])
  ret = (ret * mma(0, r - l + 1)) % 1000000007
end
print(ret)
