#include const int Mod = 1000000007; long long div_mod(long long x, long long y, long long z) { if (x % y == 0) return x / y; else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y; } long long pow_mod(int n, int k, int Mod) { long long N, ans = 1; for (N = n; k > 0; k >>= 1, N = N * N % Mod) if (k & 1) ans = ans * N % Mod; return ans; } int recursion(int a, int n) { if (n == 0) return 0; else if (n == 1) return a; int tmp = recursion(a, n / 2); if (n % 2 == 0) return tmp * (pow_mod(10, n / 2, 27) + 1) % 27; else return (tmp * (pow_mod(10, n / 2, 27) + 1) * 10 + a) % 27; } int main() { int i, j, N, c[10]; scanf("%d", &N); for (i = 1; i <= 9; i++) scanf("%d", &(c[i])); for (i = 1; i <= 9; i++) if (c[i] == N) break; if (i <= 9) { printf("%lld\n", div_mod(pow_mod(10, N, Mod) - 1, 9, Mod) * i % Mod); fflush(stdout); return 0; } int ans; long long sum = 0; if (c[4] + c[8] == N) ans = 4; else if (c[2] + c[4] + c[6] + c[8] == N) ans = 2; else ans = 1; for (i = 1; i <= 9; i++) sum += (long long)i * c[i]; if (sum % 9 == 0) ans *= 9; else if (sum % 3 == 0) ans *= 3; if (c[1] + c[4] + c[7] == N) { if ((recursion(1, c[1]) + recursion(4, c[4]) * pow_mod(10, c[1], 27) + recursion(7, c[7]) * pow_mod(10, c[1] + c[4], 27)) % 27 == 0) ans *= 3; } else if (c[2] + c[5] + c[8] == N) { if ((recursion(2, c[2]) + recursion(5, c[5]) * pow_mod(10, c[2], 27) + recursion(8, c[8]) * pow_mod(10, c[2] + c[5], 27)) % 27 == 0) ans *= 3; } else if (c[3] + c[6] + c[9] == N) { if ((recursion(3, c[3]) + recursion(6, c[6]) * pow_mod(10, c[3], 27) + recursion(9, c[9]) * pow_mod(10, c[3] + c[6], 27)) % 27 == 0) ans *= 3; } else if (c[4] + c[7] == N) { if ((recursion(4, c[4]) + recursion(7, c[7]) * pow_mod(10, c[4], 27)) % 27 == 0) ans *= 3; } else if (c[5] + c[8] == N) { if ((recursion(5, c[5]) + recursion(8, c[8]) * pow_mod(10, c[5], 27)) % 27 == 0) ans *= 3; } else if (c[6] + c[9] == N) { if ((recursion(6, c[6]) + recursion(9, c[9]) * pow_mod(10, c[6], 27)) % 27 == 0) ans *= 3; } if (c[1] + c[8] == N) { if ((div_mod(pow_mod(10 % 7, c[1], 7) - 1, 9 % 7, 7) + div_mod(pow_mod(10 % 7, c[8], 7) - 1, 9 % 7, 7) * 8 * pow_mod(10 % 7, c[1], 7)) % 7 == 0) ans *= 7; } else if (c[2] + c[9] == N) { if ((div_mod(pow_mod(10 % 7, c[2], 7) - 1, 9 % 7, 7) * 2 + div_mod(pow_mod(10 % 7, c[9], 7) - 1, 9 % 7, 7) * 9 * pow_mod(10 % 7, c[2], 7)) % 7 == 0) ans *= 7; } printf("%d\n", ans); fflush(stdout); return 0; }