import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;


void main()
{
	auto n = readln.chomp.to!int;
  auto t = readln.chomp.split.map!(to!int);
  auto a = readln.chomp.split.map!(to!int);

  auto res1 = new int[](n);
  res1[0] = t[0];
  foreach (i; 1..n) {
    if (t[i] - t[i-1]) {
      res1[i] = t[i];
    } else {
      res1[i] = 1;
    }
  }
  auto res2 = new int[](n);
  res2[$-1] = a[$-1];
  foreach_reverse (i; 0..n-1) {
    if (a[i] - a[i+1]) {
      res2[i] = a[i];
    } else {
      res2[i] = 1;
    }
  }

  BigInt sum = 1;
  foreach (i; 0..n) {
    auto diff = min(t[i], a[i]) - max(res1[i], res2[i]) + 1;
    if (diff < 0) diff = 0;
    sum *= diff;
  }
  writeln(sum % 1_000_000_007);
}
