program two_alpinists
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer(8) :: t(100000), a(100000), m
  integer :: n, i
  t = 0_8
  a = 0_8
  read(*,*) n
  read(*,*) t(1:n)
  read(*,*) a(1:n)
  if (t(n).ne.a(1)) then
    write(*,'(i0)') 0
    stop
  end if
  m = 1_8
  do i = 2, n-1
    if (t(i).gt.a(i).or.t(i).ne.t(i-1)) cycle
    m = mod(m*t(i),md)
  end do
  do i = n-1, 2, -1
    if (a(i).gt.t(i).or.a(i).ne.a(i+1)) cycle
    m = mod(m*a(i),md)
  end do
  write(*,'(i0)') m
  stop
end program two_alpinists