def devisors(n): d = [] a = 1 while a * a < n: if n % a == 0: d.append(a) d.append(n // a) a += 1 if a * a == n: d.append(a) return d MOD = 10 ** 9 + 7 n = int(input()) c = [0] + list(map(int, input().split())) for i in range(10): if c[i] == n: print(i * (pow(10, n, MOD) - 1) * pow(9, MOD - 2, MOD) % MOD) exit(0) s = 0 for i in range(9): s += i * c[i] ans = 1 if c[4] + c[8] == n: ans *= 4 elif c[0] + c[2] + c[4] + c[6] + c[8] == n: ans *= 2 if c[0] + c[5] == n: ans *= 5 if s % 9 == 0: ans *= 9 elif s % 3 == 0: ans *= 3 s = set(devisors(ans)) for i in range(1, 10): for j in range(1, i): if c[i] > 0 and c[j] > 0: if len(s) == 0: s = set(devisors(i * 10 + j - j * 10 - i)) else: s &= set(devisors(i * 10 + j - j * 10 - i)) print(max(s))