import java.io.BufferedReader import java.io.InputStream import java.io.InputStreamReader import java.io.PrintWriter import java.util.* fun PrintWriter.solve(sc: FastScanner) { val n = sc.nextInt() val a = Array(n) { sc.nextInt() } val M = 1000000007 if (a.contains(0)) { println(-1) } else if (a.any { it >= 4 }) { println(M) } else { var v = 1L for (i in 0 until n) { if (a[i] == 2) v *= 4 else if (a[i] == 3) v *= 729 if (v > M) { println(M) return } } println(M % v) } } fun main() { val writer = PrintWriter(System.out, false) writer.solve(FastScanner(System.`in`)) writer.flush() } class FastScanner(s: InputStream) { private var st = StringTokenizer("") private val br = BufferedReader(InputStreamReader(s)) fun next(): String { while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) return st.nextToken() } fun nextInt() = next().toInt() fun nextLong() = next().toLong() fun nextLine() = br.readLine() fun nextDouble() = next().toDouble() fun ready() = br.ready() }