import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;

const long MOD = 1_000_000_007;

void main(){
	int n = readln.chomp.to!int;
	long[] ts = readln.chomp.split.map!(to!long).array;
	long[] as = readln.chomp.split.map!(to!long).array;
	
	const long long0 = 0;
	ts = [long0] ~ ts ~ [long0];
	as = [long0] ~ as ~ [long0];
	
	BigInt ans;
	ans = BigInt(1);
	
	for(int i = 1; i <= n; i ++){
		long x;
		bool ft, fa;
		if(ts[i - 1] == ts[i]) ft = 1; else ft = 0;
		if(as[i] == as[i + 1]) fa = 1; else fa = 0;
		
		if(ft && fa){
			x = min(ts[i], as[i]);
			}
		if(ft && !fa){
			if(as[i] <= ts[i]) x = 1;
			else x = 0;
			}
		if(!ft && fa){
			if(ts[i] <= as[i]) x = 1;
			else x = 0;
			}
		if(!ft && !fa){
			if(ts[i] == as[i]) x = 1;
			else x = 0;
			}
		
		ans *= x;
		ans %= MOD;
		debug writeln(i, " ", ts[i], " ", as[i], " ", ft, " ", fa, " ", x, " ", ans);
		
		}
	
	ans.writeln;
	}


/*
tsは増える一方
i-1からiで増えたときはiはその高さが確定
増えなかったときはその高さ以下

asは減る一方
iからi+1で減るときはiはその高さが確定
減らなかったときはiはその高さ以下
*/