using System;
using System.Linq;

namespace CF2016CC{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var N = sr.Next<int>();
            var T = sr.Next<long>(N);
            var A = sr.Next<long>(N);

            var tmax = T.Max();
            var amax = A.Max();

            if(T.Last() != A.First() || tmax != amax){
                Console.WriteLine(0);
                return;
            }

            var idx = -1;
            for(var i = 0; i < N; i++){
                if(T[i] == A[i] && T[i] == tmax){
                    idx = i;
                    break;
                }
            }

            if(idx == -1){
                Console.WriteLine(0);
                return;
            }

            var can = true;
            for(var i = 0; i <= idx; i++){
                if(T[i] > A[i]) can = false;
            }
            for(var i = idx; i < N; i++){
                if(T[i] < A[i]) can = false;
            }

            if(!can){
                Console.WriteLine(0);
                return;
            }

            var tt = new long[N];
            var ta = new long[N];
            var res = 1L;

            for(var i = 1; i < N; i++){
                if(T[i] == T[i - 1]){
                    tt[i] = T[i];
                } else{
                    tt[i] = 1;
                }
            }

            for(var i = 0; i < N - 1; i++){
                if(A[i] == A[i + 1]){
                    ta[i] = A[i];
                } else{
                    ta[i] = 1;
                }
            }

            for(var i = 1; i < N - 1; i++){
                if(i == idx) continue;
                res *= Math.Min(tt[i], ta[i]);
                res %= 1000000007;
            }
            Console.WriteLine(res);
            //---------------------------------
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];
        private readonly System.IO.StreamReader _sr = new System.IO.StreamReader(Console.OpenStandardInput());

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = _sr.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

        public T[] Next<T>(int x){
            var ret = new T[x];
            for(var i = 0; i < x; ++i) ret[i] = Next<T>();
            return ret;
        }

        public T[][] Next<T>(int y, int x){
            var ret = new T[y][];
            for(var i = 0; i < y; ++i) ret[i] = Next<T>(x);
            return ret;
        }
    }
}

Measured memory_bytes: 25440