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)
  m = 1_8
  if (t(1).gt.a(1).or.t(n).lt.a(n)) m = 0_8
  do i = 2, n-1
    if (t(i).ne.t(i-1).and.a(i).ne.a(i+1)) then
      if (t(i).ne.a(i)) m = 0_8
    else if (t(i).ne.t(i-1).and.a(i).eq.a(i+1)) then
      if (t(i).gt.a(i)) m = 0_8
    else if (t(i).eq.t(i-1).and.a(i).ne.a(i+1)) then
      if (t(i).lt.a(i)) m = 0_8
    else
      m = mod(m*min(t(i),a(i)),md)
    end if
  end do
  write(*,'(i0)') m
  stop
end program two_alpinists