import java.io.PrintWriter import scala.collection.mutable.* import scala.io.StdIn.* import scala.util.chaining.* import scala.math.* import scala.reflect.ClassTag import scala.util.* import scala.annotation.tailrec import scala.collection.mutable @tailrec def gcd(a: Int, b: Int): Int = b match case 0 => a case _ => gcd(b, a % b) def powMod(base: Int, exp: Int, mod: Int): Long = var result = 1L var b = base.toLong % mod var e = exp while e > 0 do if (e & 1) == 1 then result = result * b % mod b = b * b % mod e = e >> 1 result def calcRest(digitCount: Array[Int], mod: Int): Long = var r = 0L for i <- 1 to 9 if digitCount(i - 1) > 0 do val length = digitCount(i - 1) r = (r * powMod(10, length, mod)) % mod var value = 0L for d <- 29 to 0 by -1 do value = (value + value * powMod(10, length >> (d + 1), mod)) % mod if ((length >> d) & 1) == 1 then value = (value * 10 + i) % mod r = (r + value) % mod r @main def main = val n = readLine.toInt val digitCount = readLine.split(' ').map(_.toInt) val diff = for i <- 1 to 9 if digitCount(i - 1) > 0 j <- 1 until i if digitCount(j - 1) > 0 yield 9 * (i - j) val g = diff.fold(0)(gcd) val result = (1 to g).filter(g % _ == 0).findLast{ calcRest(digitCount, _) == 0 } result match case Some(d) => println(d) case None => println(calcRest(digitCount, 1000000007))