# frozen_string_literal: true L = 10**9 + 7 class Integer def fact (1..self).reduce(:*) end end def solve if A.any?(&:zero?) -1 elsif A.max >= 4 L else L % (A.reduce(1) do |result, item| x = result * item**item.fact x > L ? (return L) : x end) end end N = gets.to_i A = gets.split.map(&:to_i) puts solve