We start with a string $s$ consisting only of the digits $1$, $2$, or $3$. The length of $s$ is denoted by $|s|$. For each $i$ from $1$ to $|s|$, the $i$-th character of $s$ is denoted by $s_i$. 

There is one cursor. The cursor's location $\ell$ is denoted by an integer in $\{0, \ldots, |s|\}$, with the following meaning:   If $\ell = 0$, then the cursor is located before the first character of $s$.  If $\ell = |s|$, then the cursor is located right after the last character of $s$.  If $0 < \ell < |s|$, then the cursor is located between $s_\ell$ and $s_{\ell+1}$. 

We denote by $s_\text{left}$ the string to the left of the cursor and $s_\text{right}$ the string to the right of the cursor. 

We also have a string $c$, which we call our clipboard, which starts out as empty. There are three types of actions:  The Move action. Move the cursor one step to the right. This increments $\ell$ once.  The Cut action. Set $c \leftarrow s_\text{right}$, then set $s \leftarrow s_\text{left}$.  The Paste action. Append the value of $c$ to the end of the string $s$. Note that this doesn't modify $c$. 

The cursor initially starts at $\ell = 0$. Then, we perform the following procedure:  Perform the Move action once.  Perform the Cut action once.  Perform the Paste action $s_\ell$ times.  If $\ell = x$, stop. Otherwise, return to step 1. 

You're given the initial string $s$ and the integer $x$. What is the length of $s$ when the procedure stops? Since this value may be very large, only find it modulo $10^9 + 7$. 

It is guaranteed that $\ell \le |s|$ at any time.


-----Input-----

The first line of input contains a single integer $t$ ($1 \le t \le 1000$) denoting the number of test cases. The next lines contain descriptions of the test cases.

The first line of each test case contains a single integer $x$ ($1 \le x \le 10^6$). The second line of each test case consists of the initial string $s$ ($1 \le |s| \le 500$). It is guaranteed, that $s$ consists of the characters "1", "2", "3".

It is guaranteed that the sum of $x$ in a single file is at most $10^6$. It is guaranteed that in each test case before the procedure will stop it will be true that $\ell \le |s|$ at any time.


-----Output-----

For each test case, output a single line containing a single integer denoting the answer for that test case modulo $10^9 + 7$. 


-----Example-----
Input
4
5
231
7
2323
6
333
24
133321333

Output
25
1438
1101
686531475



-----Note-----

Let's illustrate what happens with the first test case. Initially, we have $s = $ 231. Initially, $\ell = 0$ and $c = \varepsilon$ (the empty string). The following things happen if we follow the procedure above:

  Step 1, Move once: we get $\ell = 1$.  Step 2, Cut once: we get $s = $ 2 and $c = $ 31.  Step 3, Paste $s_\ell = $ 2 times: we get $s = $ 23131.  Step 4: $\ell = 1 \not= x = 5$, so we return to step 1. 

  Step 1, Move once: we get $\ell = 2$.  Step 2, Cut once: we get $s = $ 23 and $c = $ 131.  Step 3, Paste $s_\ell = $ 3 times: we get $s = $ 23131131131.  Step 4: $\ell = 2 \not= x = 5$, so we return to step 1. 

  Step 1, Move once: we get $\ell = 3$.  Step 2, Cut once: we get $s = $ 231 and $c = $ 31131131.  Step 3, Paste $s_\ell = $ 1 time: we get $s = $ 23131131131.  Step 4: $\ell = 3 \not= x = 5$, so we return to step 1. 

  Step 1, Move once: we get $\ell = 4$.  Step 2, Cut once: we get $s = $ 2313 and $c = $ 1131131.  Step 3, Paste $s_\ell = $ 3 times: we get $s = $ 2313113113111311311131131.  Step 4: $\ell = 4 \not= x = 5$, so we return to step 1. 

  Step 1, Move once: we get $\ell = 5$.  Step 2, Cut once: we get $s = $ 23131 and $c = $ 13113111311311131131.  Step 3, Paste $s_\ell = $ 1 times: we get $s = $ 2313113113111311311131131.  Step 4: $\ell = 5 = x$, so we stop. 

At the end of the procedure, $s$ has length $25$.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Mathematics

Implementation


Solutions:
import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
mod = 10 ** 9 + 7
for _ in range(t):
	x = int(input())
	s = list(map(int, list(input())))
	ans = len(s)
	cnt = ans
	flg = True
	for i in range(x):
		ans += (s[i] - 1) * (ans - i - 1)
		if len(s) > x:
			flg = False
		if flg:
			l = len(s)
			for j in range((l - i - 1) * (s[i] - 1)):
				s.append(s[i + 1 + j % (l - i - 1)])
				cnt += 1
				if cnt > x:
					break
		ans %= mod
	print(ans)


import sys
mod = 10 ** 9 + 7
t = int(sys.stdin.readline())
for _ in range(t):
	x = int(sys.stdin.readline())
	s = [i for i in sys.stdin.readline()][:-1]
	length = len(s)
	for i in range(1, x + 1):
		if len(s) >= x:
			add = (length - i) % mod * ((int(s[i - 1]) - 1) % mod) % mod
			length = (length + add) % mod
		else:
			count = 0
			rep = int(s[i - 1]) - 1
			(start, end) = (i, len(s))
			for k in range(rep):
				for j in range(start, end):
					s.append(s[j])
			add = (length - i) % mod * ((int(s[i - 1]) - 1) % mod) % mod
			length = (length + add) % mod
	print(length)


t = int(input())
for __ in range(t):
	x = int(input())
	s = [c for c in input()]
	l = 0
	total_length = len(s)
	while l != x:
		l += 1
		count = int(s[l - 1]) - 1
		if total_length <= x:
			for k in range(count):
				for c in s[l:total_length]:
					if len(s) > x:
						break
					s.append(c)
		total_length += (total_length - l) * count
		total_length %= int(1000000000.0) + 7
	print(total_length)


from math import *
from collections import *
import sys
sys.setrecursionlimit(10 ** 9)
mod = 10 ** 9 + 7
t = int(input())
for y in range(t):
	x = int(input())
	s = input()
	l = len(s)
	key = 0
	for i in range(x):
		if s[i] == '1':
			continue
		if key == 0 and l < x:
			s += (int(s[i]) - 1) * s[i + 1:]
		l = i + 1 + (l - (i + 1)) * int(s[i])
		if l >= mod:
			key = 1
		l %= mod
	print(l)


import sys
import math
from collections import defaultdict, Counter, deque
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = pow(10, 9) + 7
t = int(input())
for i in range(t):
	x = int(input())
	s = list(map(int, input()))
	cur = 0
	tot = len(s) - 1
	while cur < x:
		tot = s[cur] * tot % mod
		if len(s) < x and s[cur] > 1:
			s += s[cur + 1:] * (s[cur] - 1)
		if tot == 0:
			tot = 10 ** 9 + 6
		else:
			tot -= 1
		cur += 1
	ans = (cur + tot + 1) % mod
	print(ans)


for _ in range(int(input())):
	x = int(input())
	s = input()
	i = 0
	while len(s) < x:
		s += s[i + 1:] * (int(s[i]) - 1)
		i += 1
	ans = len(s)
	for j in range(i, x):
		ans += (ans - j - 1) * (int(s[j]) - 1)
		ans %= 10 ** 9 + 7
	print(ans)


import sys, os.path
import math
from collections import defaultdict, deque
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
S = lambda : list(map(str, input()))

def main():
	(t,) = I()
	for t1 in range(t):
		(x,) = I()
		s2 = str(input()).rstrip('\n')
		s1 = []
		for i in range(len(s2)):
			if i > x:
				break
			s1.append(int(s2[i]))
		count1 = len(s2)
		count1 %= 10 ** 9 + 7
		for i in range(x):
			k = int(s1[i])
			count1 -= 1
			count1 %= 10 ** 9 + 7
			if len(s1) > x:
				count1 *= k
				count1 %= 10 ** 9 + 7
			else:
				if k == 1:
					continue
				X = s1[i + 1:]
				l1 = len(s1)
				for j in range(k - 1):
					if len(s1) < x:
						s1.extend(X)
				count1 *= k
				count1 %= 10 ** 9 + 7
		count1 += x
		count1 %= 10 ** 9 + 7
		print(count1)
main()


import sys
mod = 10 ** 9 + 7
for _ in range(int(input())):
	x = int(input())
	s = list(map(int, input()))
	ans = len(s)
	for i in range(1, x + 1):
		ans = (i + (ans - i) * s[i - 1]) % mod
		r = len(s)
		for _ in range(s[i - 1] - 1):
			if len(s) < x:
				s += s[i:r]
			else:
				break
	print(ans)


import sys

def input():
	return sys.stdin.readline().rstrip()
MOD = int(1000000000.0 + 7)

def slv():
	x = int(input())
	gen = list(input())
	gen = list(map(int, gen))
	S = gen.copy()
	ans = len(S)
	for op in range(x):
		if len(S) > x:
			rep = S[op]
			ans = (ans - (op + 1)) * rep + (op + 1)
			ans %= MOD
		else:
			rep = S[op]
			L = len(S)
			for _ in range(rep - 1):
				for i in range(op + 1, L):
					S.append(S[i])
			ans = len(S)
	print(ans % MOD)
	return

def main():
	t = int(input())
	for i in range(t):
		slv()
	return
main()


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(100000000)
	threading.stack_size(40960000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def main():
	for _ in range(ii()):
		x = ii()
		s = si()
		sl = -1
		l = 0
		n = len(s)
		tmp = ''
		tmp += s
		for i in range(x):
			sl += 1
			tmp += tmp[sl + 1:] * (int(tmp[sl]) - 1)
			if len(tmp) > x:
				break
		fl = len(s)
		sl = -1
		l = len(s)
		for i in range(1, x + 1):
			sl += 1
			l = (l + (int(tmp[sl]) - 1) * (l - i) % mod) % mod
		print(l)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)

def toord(c):
	return ord(c) - ord('a')

def lcm(a, b):
	return a * b // lcm(a, b)
mod = 10 ** 9 + 7
INF = float('inf')
from math import factorial, sqrt, ceil, floor, gcd
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	for _ in range(N()):
		n = N()
		s = list(map(int, input()))
		pos = 0
		res = len(s)
		while pos < n:
			pos += 1
			tms = s[pos - 1]
			c = res - pos
			res += c * (tms - 1)
			res %= mod
			if len(s) < n and tms > 1:
				s += s[pos:] * (tms - 1)
		print(res % mod)
main()


t = int(input())
for i in range(t):
	x = int(input())
	y = input()
	s = []
	for j in y:
		s.append(j)
	l = 0
	res = len(s)
	while l != x:
		if len(s) < x:
			le = len(s)
			for p in range(int(s[l]) - 1):
				for j in range(l + 1, le):
					s.append(s[j])
		res = (res + (res - l - 1) * (int(s[l]) - 1)) % (10 ** 9 + 7)
		l += 1
	print(res % (10 ** 9 + 7))


MOD = 10 ** 9 + 7

def main():
	x = int(input())
	s = input()
	ln = len(s)
	key = 0
	for l in range(x):
		num = int(s[l]) - 1
		if num == 0:
			continue
		if ln < x and (not key):
			s += s[l + 1:] * num
		r = (ln - l - 1) * num
		ln += r
		if ln >= MOD:
			key = 1
		ln %= MOD
	print(ln % MOD)
t = int(input())
for _ in range(t):
	main()


t = int(input())
for _ in range(t):
	mod = 1000000007
	x = int(input())
	r = input()
	n = len(r)
	s = []
	for i in range(n):
		s.append(r[i])
	f = True
	for i in range(1, x + 1):
		o = ord(s[i - 1]) - ord('0')
		if f:
			for j in range(1, o):
				for k in range(i, n):
					s.append(s[k])
			n = len(s)
		elif o == 2:
			n += n - i
			n %= mod
		elif o == 3:
			n += 2 * (n - i)
			n %= mod
		if n > x and f:
			f = False
	print(n)


import sys, math
MOD = 10 ** 9 + 7
for _ in range(int(input())):
	x = int(input())
	s = list(map(int, list(input())))
	ans = len(s)
	(l, lens) = (1, len(s))
	s = s + [0] * (x - len(s))
	for l in range(1, x + 1):
		for i in range(lens, min((lens - l) * s[l - 1] + l, len(s))):
			s[i] = s[l + i - lens]
		lens = min((lens - l) * s[l - 1] + l, len(s))
		if lens == len(s):
			break
	for l in range(1, x + 1):
		ans = ((ans - l) * s[l - 1] + l) % MOD
	print(ans)


for _ in range(int(input())):
	x = int(input())
	s = input()
	cnt = 0
	i = 0
	while len(s) < x:
		s += s[i + 1:] * (ord(s[i]) - 49)
		i += 1
	cnt = len(s) % 1000000007
	while i < x:
		cnt = (cnt + (cnt - 1 - i) * (ord(s[i]) - 49)) % 1000000007
		i += 1
	print(cnt)


INF = 10 ** 10

def main():
	print = out.append
	x = get_int()
	P = 10 ** 9 + 7
	li = list(map(int, input()))
	ptr = 0
	res = len(li)
	while ptr < x:
		ptr += 1
		if len(li) < x and li[ptr - 1] > 1:
			li += li[ptr:] * (li[ptr - 1] - 1)
		res -= 1
		res = res * li[ptr - 1] % P
	print((res + ptr) % P)
import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda : int(input())
get_list = lambda : list(map(int, input().split()))
[main() for _ in range(int(input()))]
print(*out, sep='\n')


n = int(input())
while n:
	x = int(input())
	a = input()
	i = 0
	while len(a) < x:
		a += a[i + 1:] * (int(a[i]) - 1)
		i += 1
	ans = len(a)
	for j in range(i, x):
		ans += (ans - 1 - j) * (int(a[j]) - 1)
		ans %= 10 ** 9 + 7
	print(ans)
	n -= 1


import sys
testcases = int(input())
MOD = 10 ** 9 + 7
for _ in range(testcases):
	x = int(input())
	initialstring = list(map(int, input()))
	initiallength = len(initialstring)
	slicecounter = 1
	while slicecounter <= x:
		numberatend = initialstring[slicecounter - 1]
		initiallength = (initiallength + (initiallength - slicecounter) * (initialstring[slicecounter - 1] - 1)) % MOD
		holdingvalue = len(initialstring)
		for _ in range(1, numberatend):
			if len(initialstring) < x:
				initialstring += initialstring[slicecounter:holdingvalue]
			else:
				break
		slicecounter += 1
	print(initiallength)


def solve(x, s):
	(len_, s) = (len(s), list(s))
	for e in range(1, x + 1):
		r = int(s[e - 1])
		if len(s) < x and r > 1:
			s += s[e:] * (r - 1)
		len_ = (e + (len_ - e) * r) % 1000000007
	return len_
for T in range(int(input())):
	print(solve(int(input()), input()))


import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
t = int(input())
for _ in range(t):
	x = int(input())
	s = input().rstrip('\n')
	X = []
	for (i, si) in enumerate(s):
		if i == x:
			break
		X.append(int(s[i]))
	ans = len(s)
	for i in range(x):
		ans = (ans + (ans - i - 1) * (X[i] - 1)) % mod
		if len(X) < x:
			if X[i] == 1:
				continue
			X_ = X[i + 1:]
			for j in range(X[i] - 1):
				if len(X) < x:
					X.extend(X_)
	print(ans)


from sys import stdin, stdout
T = int(stdin.readline())
out = []
limit = int(1000000000.0 + 7)
for _ in range(T):
	n = int(stdin.readline())
	s = list(map(int, stdin.readline()[:-1]))
	total = len(s)
	b = True
	for k in range(n):
		mult = s[k] - 1
		if mult == 0:
			continue
		suf = total - k - 1
		add = mult * suf
		_total = total + add
		if b:
			s.extend(mult * s[k + 1:])
			if _total >= n:
				b = False
		total = _total % limit
	out.append(str(total))
stdout.write('\n'.join(out))


import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
Q = int(input())
Query = []
for _ in range(Q):
	X = int(input())
	S = input().rstrip()
	Query.append((X, S))
for (X, S) in Query:
	L = len(S)
	for l in range(X):
		repeat = int(S[l]) - 1
		if repeat == 0:
			continue
		k = L - l - 1
		if k < 0:
			k += mod
		if len(S) < X:
			E = S[l + 1:]
		for _ in range(repeat):
			if len(S) < X:
				S += E
			L = (L + k) % mod
	print(L)


from collections import deque

def solve():
	n = int(input())
	t = [int(a) for a in input()]
	prawo = len(t) - 1
	kolejkaMnozen = deque([])
	idx = 0
	lMnozenia = 1
	for i in range(n):
		while idx >= len(t):
			if len(kolejkaMnozen) == 0:
				exit(666)
			if kolejkaMnozen[0] != 1:
				t.extend((kolejkaMnozen[0] - 1) * t[lMnozenia:])
			lMnozenia += 1
			kolejkaMnozen.popleft()
		mnoznik = t[idx]
		prawo = prawo * mnoznik
		prawo -= 1
		prawo %= 1000000007
		kolejkaMnozen.append(t[idx])
		idx += 1
	return (n + prawo + 1) % 1000000007
n = int(input())
for i in range(n):
	print(solve())


t = int(input())
for i in range(t):
	x = int(input())
	s = list(map(int, input()))
	ans = len(s)
	l = 1
	while l <= x:
		c = s[l - 1]
		ans = ((ans - l) * c + l) % (10 ** 9 + 7)
		r = len(s)
		for i in range(c - 1):
			if len(s) < x:
				s += s[l:r]
			else:
				break
		l += 1
	print(ans)


for _ in range(int(input())):
	x = int(input())
	s = input()
	l = 0
	while len(s) < x:
		s += s[l + 1:] * (int(s[l]) - 1)
		l += 1
	count = len(s)
	for k in range(l, x):
		count += (count - k - 1) * (int(s[k]) - 1)
		count %= 1000000007
	print(count)


import sys
input = sys.stdin.buffer.readline
mod = 10 ** 9 + 7
input = sys.stdin.readline
for i in range(int(input())):
	x = int(input())
	s = sys.stdin.readline().rstrip()
	p = len(s)
	for j in range(x):
		w = int(s[j])
		if w == 1:
			continue
		else:
			p *= w
			p -= (j + 1) * (w - 1)
		p %= mod
		if len(s) < x:
			s += s[j + 1:] * (w - 1)
	print(p)


t = int(input())
for i in range(t):
	x = int(input())
	st = input()
	ar = list(st)
	length = len(st)
	length1 = length
	b = False
	md = 10 ** 9 + 7
	for j in range(x):
		val = ord(ar[j]) - 48
		if val > 1 and b == False:
			for k in range(j + 1, length):
				length1 += 1
				ar.append(ar[k])
				if length1 >= x:
					b = True
					break
			if val == 3 and b == False:
				for k in range(j + 1, length):
					length1 += 1
					ar.append(ar[k])
					if length1 >= x:
						b = True
						break
		length = ((j + 1) % md + val % md * (length % md - (j + 1) % md) % md % md) % md
	print(length)


for _ in range(int(input())):
	x = int(input())
	s = [int(x) for x in input()]
	ans = len(s)
	m = 10 ** 9 + 7
	for l in range(1, x + 1):
		ans += (ans - l) * (s[l - 1] - 1)
		g = len(s)
		ans %= m
		p = len(s)
		for _ in range(s[l - 1] - 1):
			if len(s) < x:
				s += s[l:p]
			else:
				break
	print(ans)


ttt = int(input())
mod = 10 ** 9 + 7
s = '#' * (10 ** 6 + 1500)
s = list(s)
for bo in range(ttt):
	x = int(input())
	st = input()
	s[1:len(st) + 1] = list(st)
	siz = len(st)
	p = 0
	while siz < x:
		p += 1
		whe = siz
		for de4d in range(int(s[p]) - 1):
			for k in range(p + 1, whe + 1):
				siz += 1
				s[siz] = s[k]
				if siz > x:
					break
			if siz > x:
				break
		if siz > x:
			break
	ans = len(st)
	for i in range(1, x + 1):
		ans = i + (ans - i) * int(s[i])
		ans = ans % mod
	print(ans)


from collections import defaultdict

def getlist():
	return list(map(int, input().split()))
con = 10 ** 9 + 7

def main():
	t = int(input())
	for i in range(t):
		x = int(input())
		s = input()
		M = len(s)
		MM = M
		c = 0
		while MM < x:
			C = int(s[c])
			if C != 1:
				s += s[c + 1:] * (C - 1)
				MM += (MM - c - 1) * (C - 1)
			c += 1
		ans = M
		for i in range(x):
			ans = (i + 1 + (ans - i - 1) * int(s[i])) % con
		print(ans)
main()


def solve(x, s):
	size = len(s)
	l = 0
	mod = pow(10, 9) + 7
	while l != x:
		l += 1
		size += (size - l) * (int(s[l - 1]) - 1)
		size %= mod
		if len(s) <= x:
			s += s[l:] * (int(s[l - 1]) - 1)
	return size
for i in range(int(input())):
	x = int(input())
	s = input()
	print(solve(x, s))


n = int(input())
mod = 10 ** 9 + 7
for j in range(n):
	x = int(input())
	sleft = str(input())
	length = len(sleft)
	l = 0
	while len(sleft) < x:
		l += 1
		if sleft[l - 1] == '2':
			sright = sleft[l:]
			sleft += sright
		elif sleft[l - 1] == '3':
			sright = sleft[l:]
			sleft += sright * 2
		else:
			continue
	for l in range(1, x + 1):
		mult = int(sleft[l - 1])
		length = l + mult * ((length - l) % mod)
		length = length % mod
	print(length)


for _ in range(int(input())):
	x = int(input())
	s = list(map(int, input()))
	ans = len(s)
	for i in range(1, x + 1):
		ans = (i + (ans - i) * s[i - 1]) % 1000000007
		r = len(s)
		for _ in range(s[i - 1] - 1):
			if len(s) < x:
				s += s[i:r]
			else:
				break
	print(ans)


n = int(input())
import math

def getdb(s, x):
	l = 1
	length = len(s)
	newS = s + [0] * (3 * x - length)
	while length < x and l <= x:
		i = length - l
		j = length
		for k in range(newS[l - 1] - 1):
			newS[j:j + i] = newS[l:l + i]
			j += i
			length += i
		l += 1
	return newS[0:length]
for i in range(n):
	x = int(input())
	s = [int(x) for x in input()]
	dbli = getdb(s, x)
	tot = len(s)
	l = 1
	while l <= x:
		if tot <= l:
			tot += 10 ** 9 + 7
		tot = l + dbli[l - 1] * (tot - l)
		if math.log(tot) > 9.01:
			tot %= 10 ** 9 + 7
		l += 1
	print(tot % (10 ** 9 + 7))


import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 1000000007
for i in range(int(input())):
	n = int(input())
	s = input()
	l = len(s)
	for i in range(n):
		l = l % mod + (l - i - 1) % mod * (int(s[i]) - 1) % mod % mod
		l = l % mod
		if s[i] == '1':
			continue
		if len(s) < n:
			s = s + (int(s[i]) - 1) * s[i + 1:]
	print(l % mod)


t = int(input())
mod = 10 ** 9 + 7
for i in range(t):
	x = int(input())
	s = input()
	j = 0
	while len(s) < x:
		s += s[j + 1:] * (int(s[j]) - 1)
		j += 1
	length = len(s)
	for jj in range(j, x):
		length = (length + (length - jj - 1) * (int(s[jj]) - 1)) % mod
	print(length % mod)


from collections import deque
import sys
input = sys.stdin.readline
mod = 1000000007
t = int(input().strip())
for _ in range(t):
	x = int(input().strip())
	s = list(input().strip())
	n = 0
	l = len(s)
	total = l
	for i in range(0, x):
		n = int(s[i])
		if len(s) < x + 40:
			for k in range(n - 1):
				flag = 1
				s.extend(s[i + 1:l])
				if len(s) > x:
					flag = 0
					break
				if flag == 0:
					break
		total = (total + (n - 1) * (l - (i + 1)) % mod) % mod
		l = ((n - 1) * (l - (i + 1)) + l) % mod
	print(total)


t = int(input())
mod = 10 ** 9 + 7
for _ in range(t):
	x = int(input())
	s = input()
	i = 1
	while len(s) < x:
		s += s[i:] * (ord(s[i - 1]) - 49)
		i += 1
	l = len(s)
	while i <= x:
		l = (l + (l - i) * (ord(s[i - 1]) - 49)) % mod
		i += 1
	print(l)


for _ in range(int(input())):
	n = int(input())
	a = [0] * n
	tmp = input()
	ans = last = len(tmp)
	for (i, x) in enumerate(tmp):
		if i == n:
			last = n
			break
		a[i] = int(x)
	for i in range(n):
		ans = (ans - i - 1) * a[i] + i + 1
		ans %= 10 ** 9 + 7
		k = last
		for _ in range(a[i] - 1):
			for j in range(i + 1, last):
				if k == n:
					break
				a[k] = a[j]
				k += 1
		last = k
	print(ans % (10 ** 9 + 7))


for i in range(int(input())):
	x = int(input())
	s = input()
	ans = len(s)
	a = x
	for j in range(1, x + 1):
		if s[j - 1] == '1':
			pass
		elif s[j - 1] == '2':
			ans += ans - j
			ans %= 10 ** 9 + 7
			s = s + s[j:]
		elif s[j - 1] == '3':
			ans += 2 * (ans - j)
			ans %= 10 ** 9 + 7
			s = s + 2 * s[j:]
		if len(s) > x:
			a = j
			break
	for k in range(a + 1, x + 1):
		if s[k - 1] == '1':
			pass
		elif s[k - 1] == '2':
			ans += ans - k
			ans %= 10 ** 9 + 7
		elif s[k - 1] == '3':
			ans += 2 * (ans - k)
			ans %= 10 ** 9 + 7
	ans %= 10 ** 9 + 7
	print(ans)


import math, collections, sys
mod = 10 ** 9 + 7
for _ in range(int(input())):
	x = int(input())
	s = [i for i in input()]
	l = len(s)
	for i in range(1, x + 1):
		rep = int(s[i - 1]) - 1
		if len(s) < x:
			start = i
			end = len(s)
			for j in range(rep):
				for k in range(start, end):
					s.append(s[k])
		l = (l + (l - i) * rep) % mod
	print(l)


from collections import defaultdict as dc
import sys
import math
mod = 10 ** 9 + 7

def bs(a, x):
	i = bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return len(a)

def bs(a, b):
	l = a
	r = b + 1
	x = b
	ans = 0
	while l < r:
		mid = (l + r) // 2
		if x | mid > ans:
			ans = x | mid
			l = mid + 1
		else:
			r = mid
	return ans

def digit(n):
	a = []
	while n > 0:
		a.append(n % 10)
		n = n // 10
	return a

def inp():
	p = int(input())
	return p

def line():
	p = list(map(int, input().split()))
	return p

def ans(a, p, z, t):
	q = []
	for i in range(len(a)):
		if p[a[i]] == t and z > 0 and (p[a[i][::-1]] == 0):
			q.append(i + 1)
			z = z - 1
	return q
for i in range(inp()):
	x = inp()
	s = str(input())
	n = len(s)
	p = n
	for i in range(1, x + 1):
		if s[i - 1] != '1':
			p = (p % mod + (p - i) % mod * (int(s[i - 1]) - 1) % mod % mod) % mod
			if len(s) < x:
				z = s[i:]
				s = s + z * (int(s[i - 1]) - 1)
	print(p)


q = int(1000000000.0 + 7)
t = int(input())
for i in range(t):
	x = int(input())
	s = input()
	s = [int(p) for p in s]
	c = len(s)
	nul = len(s)
	if x > c:
		s = s + [0] * (x - c)
	for (j, sj) in enumerate(s):
		if j == x:
			break
		if sj > 1:
			c = (j + 1 + (c - (j + 1)) * sj) % q
			if nul < x:
				nul0 = nul
				for k in range(sj - 1):
					for l in range(j + 1, nul0):
						if nul == x:
							break
						s[nul] = s[l]
						nul = nul + 1
	print(c)


def main():
	TT = int(input())
	for _ in range(TT):
		x = int(input())
		s = [int(c) for c in input().strip()]
		ans = len(s)
		for l in range(0, x):
			ans += (ans - l - 1) * (s[l] - 1)
			ans %= 1000000007
			end = len(s)
			for _ in range(1, s[l]):
				if len(s) > x + 1:
					break
				for c in range(l + 1, end):
					if len(s) > x + 1:
						break
					s.append(s[c])
		print(ans)
main()


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop, heapify
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	ab = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			ab = ab * x % p
		y = y >> 1
		x = x * x % p
	return ab

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return [int(x) for x in input().split()]

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))
t = int(input())
for _ in range(t):
	x = int(input())
	s = list(input())
	l = 0
	ans = len(s)
	mod = 1000000007
	for i in range(len(s)):
		s[i] = int(s[i])
	for i in range(x):
		l += 1
		right = ans - l
		rep = s[l - 1]
		for j in range(rep - 1):
			te = l
			while len(s) <= x + 1 and te < ans:
				s.append(s[te])
				te += 1
		ans += (rep - 1) * (right % mod)
		ans %= mod
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	x = int(input())
	s = [k for k in input()]
	ls = len(s)
	(cur, length) = (1, len(s))
	curr = cur
	twothree = s.count('2') + s.count('3')
	while cur != len(s) and len(s) < x + 69:
		while len(s) == length + (length - cur) * (int(s[cur - 1]) - 1):
			if not twothree or cur == len(s):
				break
			if int(s[cur - 1]) > 1:
				twothree -= 1
			cur += 1
			length = len(s)
			curr = cur
		if not twothree or cur == len(s):
			break
		s += [s[curr]]
		if int(s[curr]) > 1:
			twothree += 1
		curr += 1
		if curr == length:
			curr = cur
	ans = ls
	mod = 10 ** 9 + 7
	for i in range(x):
		try:
			ans = (ans + (int(s[i]) - 1) * (ans - i - 1)) % mod
		except:
			break
	print(ans)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from math import sqrt, pi, ceil, log, inf, gcd, floor
from itertools import combinations

def main():
	mod = 10 ** 9 + 7
	for _ in range(int(input())):
		h = int(input())
		s = list(input().rstrip())
		(ans, j, f) = (len(s), 0, 1)
		while j < h:
			ln = int(s[j])
			if f and ans + (ln - 1) * (ans - j - 1) <= h:
				(x, z) = (j + 1, ans - j - 1)
				for i in range((ln - 1) * z):
					s.append(s[x])
					x += 1
					if x == ans:
						x = j + 1
			else:
				if f and ans < h:
					(x, z) = (j + 1, ans - j - 1)
					for i in range(h - ans):
						s.append(s[x])
						x += 1
						if x == ans:
							x = j + 1
				f = 0
			ans = (ans + (ln - 1) * (ans - j - 1)) % mod
			j += 1
		print(ans % mod)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin

def iin():
	return int(stdin.readline())

def lin():
	return list(map(int, stdin.readline().split()))
md = 10 ** 9 + 7

def main():
	t = iin()
	while t:
		t -= 1
		x = iin()
		s = list(input())
		l = len(s)
		ans = l
		i = 0
		while i < x:
			i += 1
			ch = int(s[i - 1])
			ans = (i + ch * (ans - i)) % md
			if l >= x:
				continue
			incr = l
			ss = []
			for _ in range(ch):
				for j in range(i, l):
					if incr < ans and incr < x:
						ss.append(s[j])
						incr += 1
					else:
						break
			l = incr
			for k in ss:
				s.append(k)
		print(ans % md)
main()


testcases = int(input())
answerlist = []
MOD = 10 ** 9 + 7
for _ in range(testcases):
	x = int(input())
	initialstring = list(map(int, input()))
	whilelength = len(initialstring)
	initiallength = len(initialstring)
	slicecounter = 1
	while x > whilelength:
		numberatend = initialstring[slicecounter - 1]
		lc = len(initialstring)
		for _ in range(numberatend - 1):
			initialstring += initialstring[slicecounter:lc]
		slicecounter += 1
		whilelength = len(initialstring)
		initiallength = whilelength
	for j in range(slicecounter - 1, x):
		initiallength = (initiallength + (initiallength - (j + 1)) * (initialstring[j] - 1)) % MOD
	answerlist.append(initiallength)
for k in answerlist:
	print(k)


import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = pow(10, 9) + 7

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nl():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def solve():
	x = int(inp())
	a = list(inp())
	for i in range(0, len(a)):
		a[i] = int(a[i])
	ans = len(a)
	p = 0
	f = True
	while ans < x:
		for j in range(a[p] - 1):
			for i in range(p + 1, int(ans)):
				a.append(a[i])
		ans += (a[p] - 1) * ((ans - p - 1 + mod) % mod)
		p += 1
	for j in range(p, x):
		ans += (a[j] - 1) * ((ans - j - 1 + mod) % mod)
		ans %= mod
	print(int(ans % mod))
testcase(int(inp()))


from sys import stdin
from sys import stdout
mod = 1000000007
for i in range(int(stdin.readline())):
	x = int(stdin.readline())
	s = input()
	ans = len(s)
	l = 1
	if len(s) < x:
		while l <= x:
			c = s[l:]
			if s[l - 1] == '2':
				s = s + c
				if len(s) >= x:
					break
			elif s[l - 1] == '3':
				s = s + c
				if len(s) >= x:
					break
				s = s + c
				if len(s) >= x:
					break
			l += 1
	l = 1
	while l <= x:
		cc = ans - l
		if s[l - 1] == '2':
			ans = (ans + cc) % mod
		elif s[l - 1] == '3':
			ans = (ans + cc) % mod
			ans = (ans + cc) % mod
		l += 1
	stdout.write(str(ans) + '\n')
	stdout.flush()


def solve(s, x):
	n = len(s)
	mod = 10 ** 9 + 7
	for i in range(x):
		n += (n - i - 1) * (int(s[i]) - 1)
		n %= mod
		if len(s) < x:
			s += s[i + 1:] * (int(s[i]) - 1)
	return n
for _ in range(int(input())):
	x = int(input())
	s = input()
	print(solve(s, x))


M = 1000000007
t = int(input())
while t:
	t += -1
	x = int(input())
	s = input()
	i = 0
	while len(s) < x:
		s += s[i + 1:] * (int(s[i]) - 1)
		i += 1
	n = len(s)
	for j in range(i, x):
		tmp = n - (j + 1)
		n = j + 1 + tmp * int(s[j])
		n = n % M
	print(n % M)


mod = pow(10, 9) + 7
for _ in range(int(input())):
	x = int(input())
	s = input()
	c = 1
	while len(s) < x:
		s += s[c:] * (int(s[c - 1]) - 1)
		c += 1
	l = len(s)
	for j in range(c - 1, x):
		l += (l - j - 1) * (int(s[j]) - 1)
		l %= mod
	print(l)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 10 ** 9 + 7

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve(case):
	x = int(inp())
	s = list(inp())
	for i in range(len(s)):
		s[i] = int(s[i])
	length = len(s)
	for k in range(0, x):
		if len(s) < x:
			length = (k + 1 + s[k] * (length - (k + 1))) % mod
			ll = len(s)
			for i in range(s[k] - 1):
				if len(s) < x:
					s += s[k + 1:ll]
				else:
					break
		else:
			length = (k + 1 + s[k] * (length - (k + 1))) % mod
	print(length)
testcase(int(inp()))


t = int(input())
a = [0] * 1000006
for iii in range(t):
	x = int(input())
	s = input()
	l = 0
	ln = len(s)
	full = False
	for i in range(ln):
		a[i + 1] = ord(s[i]) - ord('0')
	while True:
		l += 1
		sl = a[l]
		newlen = l + (ln - l) * sl
		if not full:
			for i in range(ln + 1, min(newlen, x) + 1):
				a[i] = a[i - (ln - l)]
			if newlen >= x:
				full = True
		ln = newlen
		if full:
			ln %= 1000000007
		if l == x:
			break
	print(ln % 1000000007)


for _ in range(int(input())):
	x = int(input())
	s = list(input())
	l = 0
	ans = len(s)
	while l < x:
		if ans >= x:
			break
		temp = len(s)
		ans += (int(s[l]) - 1) * (temp - l - 1)
		for i in range(int(s[l]) - 1):
			if len(s) > x:
				break
			for j in range(l + 1, temp):
				s.append(s[j])
				if len(s) > x:
					break
		l += 1
	while l < x:
		temp = ans
		ans += (int(s[l]) - 1) * (temp - l - 1)
		ans = ans % (10 ** 9 + 7)
		l += 1
	print(ans)


for _ in range(int(input())):
	x = int(input())
	s = input()
	l = 0
	while len(s) < x:
		s += s[l + 1:] * (int(s[l]) - 1)
		l += 1
	ls = len(s)
	for k in range(l, x):
		ls += (ls - k - 1) * (int(s[k]) - 1)
		ls %= 10 ** 9 + 7
	print(ls)


from sys import stdin, stdout
import math
import heapq
import collections
input = stdin.readline

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputstrings():
	return [x for x in input().split()]

def inputstringnum():
	return [ord(x) - ord('1') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	x = inputnum()
	s = inputstringnum()
	if s[len(s) - 1] == -39:
		s.pop()
	mod = 1000000007
	l = 0
	ln = len(s)
	while l < x:
		cp = s[l]
		l += 1
		if cp:
			if len(s) < x:
				add = s[l:]
				for j in range(cp):
					s += add
			ln += (ln - l) * cp
			ln %= mod
	ln += mod
	ln %= mod
	print(ln)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key, lru_cache
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip().split()]

def st():
	return str(input().rstrip())[2:-1]

def val():
	return int(input().rstrip())

def li2():
	return [str(i)[2:-1] for i in input().rstrip().split()]

def li3():
	return [int(str(i)[2:-1]) for i in input().rstrip()]
l = [0] * (10 ** 6 + 5)
inpu = []
for _ in range(val()):
	x = val()
	s = st()
	inpu.append([x, s])
for (x, s) in inpu:
	l[x] = -1
	orig = len(s)
	i = 0
	for j in range(len(s)):
		l[j] = int(s[j])
	i = cursor = 0
	cursor = len(s)
	up = len(s)
	while up < x:
		i += 1
		for j in range(int(l[i - 1] - 1) * (up - i)):
			l[up + j] = l[i + j]
			if up + j == x:
				break
		up = i + (up - i) * l[i - 1]
	ans = orig
	for j in range(x):
		ans = (ans - 1) * int(l[j]) % mod
	print((ans + x + mod) % mod)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
t = int(input())
mod = 10 ** 9 + 7
for _ in range(t):
	x = int(input())
	s = list(input())
	i = 0
	ans = len(s)
	while i < x:
		j = i + 1
		z = int(s[i]) - 1
		k = 0
		ha = len(s)
		if len(s) < x:
			while k != z:
				s.append(s[j])
				if j == ha - 1:
					k += 1
					j = i + 1
				else:
					j += 1
			ans = len(s)
		else:
			z1 = ans - j
			ans = ans + z1 * z
			ans %= mod
		i += 1
	print(ans % mod)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
MAX = 10 ** 9 + 7

def solve():
	x = int(input())
	s = [int(i) for i in input()]
	ans = len(s)
	c = ''
	l = 0
	while l < x:
		l += 1
		k = len(s)
		if k < x:
			for i in range(s[l - 1] - 1):
				for j in range(l, k):
					s.append(s[j])
		ans += (s[l - 1] - 1) * (ans - l)
		ans %= MAX
	print(ans)
t = int(input())
for _ in range(t):
	solve()


MOD = 10 ** 9 + 7
for _ in range(int(input())):
	x = int(input())
	s = input()
	a = []
	for i in s:
		a.append(i)
	ans = len(s)
	for l in range(1, x + 1):
		ch = ord(a[l - 1]) - ord('0')
		ans += (ans - l) * (ch - 1)
		ans %= MOD
		if len(a) < x:
			k = len(a)
			for i in range(1, ch):
				for j in range(l, k):
					a.append(a[j])
	print(ans)


def main():
	t = int(input())
	allans = []
	for _ in range(t):
		x = int(input())
		arr = [int(z) for z in input()]
		n = len(arr)
		j = 1
		for i in range(x):
			if j == n:
				break
			for yy in range(arr[i] - 1):
				if len(arr) > x:
					break
				for zz in range(i + 1, n):
					if len(arr) > x:
						break
					arr.append(arr[zz])
			n += (n - j + MOD) % MOD * (arr[i] - 1)
			j += 1
			n %= MOD
		allans.append(n)
	multiLineArrayPrint(allans)
	return
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultVal, dimensionArr):
	dv = defaultVal
	da = dimensionArr
	if len(da) == 1:
		return [dv for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(x, y):
	print('? {} {}'.format(x, y))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(ans))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
for _abc in range(1):
	main()


mod = 10 ** 9 + 7
for nt in range(int(input())):
	x = int(input())
	s = input()
	n = len(s)
	ans = n
	for i in range(x):
		if len(s) < x:
			s += (int(s[i]) - 1) * s[i + 1:]
			ans = len(s)
		else:
			ans += (int(s[i]) - 1) * (ans - i - 1)
		ans = ans % mod
	print(ans)


mod = 10 ** 9 + 7
for _ in range(int(input())):
	x = int(input())
	s = input()
	ln = len(s)
	i = 0
	while len(s) < x:
		if s[i] == '1':
			i += 1
			continue
		t = s[i + 1:]
		if s[i] == '2':
			s += t
		else:
			t += t
			s += t
		i += 1
	times = 0
	tp = 0
	for i in range(x):
		times = int(s[i])
		times -= 1
		tp = ln - i - 1
		tp += mod
		tp %= mod
		ln += times * tp
		ln %= mod
	print(ln % mod)


import sys, os.path
import math
from collections import defaultdict, deque
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
S = lambda : list(map(str, input()))

def main():
	(t,) = I()
	for t1 in range(t):
		(x,) = I()
		s1 = S()[:-1]
		l = 0
		count = 0
		count1 = len(s1)
		count1 %= 10 ** 9 + 7
		for i in range(x):
			k = int(s1[i])
			count1 -= 1
			count1 %= 10 ** 9 + 7
			if len(s1) > x:
				count1 *= k
				count1 %= 10 ** 9 + 7
			else:
				l1 = len(s1)
				for j in range(k - 1):
					for l in range(i + 1, l1):
						s1 += s1[l]
				count1 *= k
				count1 %= 10 ** 9 + 7
		count1 += x
		count1 %= 10 ** 9 + 7
		print(count1)
main()


t = int(input())
for _ in range(t):
	x = int(input())
	s = list(map(int, input()))
	i = 0
	while i < x and len(s) < x:
		count = s[i]
		if count > 1:
			s += s[i + 1:] * (count - 1)
		i += 1
	l = len(s)
	while i < x:
		count = s[i]
		if count > 1:
			l += (count - 1) * (l - i - 1)
			l %= 10 ** 9 + 7
		i += 1
	print(l)


for _ in range(int(input())):
	x = int(input())
	a = input()
	l = len(a)
	a = [i for i in a]
	ans = l
	p = 0
	m = 10 ** 9 + 7
	while ans < x:
		for j in range(int(a[p]) - 1):
			for i in range(p + 1, ans):
				a.append(a[i])
		ans += (int(a[p]) - 1) * ((ans - p - 1 + m) % m)
		p += 1
	for j in range(p, x):
		ans += (int(a[j]) - 1) * ((ans - j - 1 + m) % m)
		ans %= m
	print(ans)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from collections import defaultdict as dd
(t,) = I()
while t:
	t -= 1
	(x,) = I()
	s = list(input())
	n = len(s)
	cur = len(s)
	for i in range(1, x + 1):
		k = int(s[i - 1])
		if k >= 2 and n <= x:
			curn = n
			for j in range(k - 1):
				for ii in range(i, curn):
					s.append(s[ii])
					n += 1
					if n > x:
						break
				if n > x:
					break
		cur = (i + (cur - i) * k % MOD) % MOD
	print(cur % MOD)


import sys
input = sys.stdin.readline
MOD = 10 ** 9 + 7
t = int(input())
for _ in range(t):
	x = int(input())
	s = list(input())
	n = len(s) - 1
	for i in range(n):
		s[i] = int(s[i])
	memo = {}
	for i in range(n):
		memo[i] = s[i]
	ans = len(memo)
	pos = i + 1
	for i in range(x):
		tmp = pos
		if tmp <= x:
			for j in range(memo[i] - 1):
				for k in range(i + 1, pos):
					memo[tmp] = memo[k]
					tmp += 1
			pos = tmp
			ans = tmp
		else:
			ans = ans + (ans - i - 1) * (memo[i] - 1)
			ans %= MOD
	print(ans % MOD)


t = int(input())
for i in range(t):
	x = int(input())
	s = input()
	l = 1
	length = len(s)
	while l <= x:
		length = (l + (length - l) * int(s[l - 1])) % 1000000007
		le = len(s)
		for i in range(int(s[l - 1]) - 1):
			if le < x:
				s += s[l:le]
			else:
				break
		l += 1
	print(length)


import time
import sys
MOD = 1000000000.0 + 7
f = sys.stdin
t1 = time.time()
for t in range(int(f.readline())):
	(x, s) = (int(f.readline().rstrip()), f.readline().rstrip())
	i = 0
	while len(s) < x:
		i += 1
		s += s[i:] * (int(s[i - 1]) - 1)
	answer = len(s)
	while i < x:
		i += 1
		answer = (answer + (answer - i) * (int(s[i - 1]) - 1)) % MOD
	print(int(answer))


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
mod = 10 ** 9 + 7

def solve():
	x = int(input())
	s = [int(i) for i in input()]
	ans = len(s)
	cur = -1
	while cur != x - 1:
		cur += 1
		if len(s) < x:
			k = len(s)
			for _ in range(s[cur] - 1):
				for j in range(cur + 1, k):
					s.append(s[j])
		ans += (s[cur] - 1) * (ans - (cur + 1))
		ans %= mod
	print(ans)
t = int(input())
for i in range(t):
	solve()


for _ in range(int(input())):
	x = int(input())
	s = list(input())
	leng = len(s)
	times = 1
	till = 0
	ended = 0
	while times <= x and leng < x:
		till = leng
		for i in range(int(s[times - 1]) - 1):
			if leng >= x:
				leng += (int(s[times - 1]) - i - 1) * (till - times)
				break
			s.extend(s[times:till])
			leng += till - times
		times += 1
	while times <= x:
		leng += (int(s[times - 1]) - 1) * (leng - times)
		times += 1
		leng = leng % 1000000007
	print(leng)


import sys
input = sys.stdin.readline
t = int(input())
co = 0
mod = 10 ** 9 + 7
for _ in range(t):
	x = int(input())
	arr = list(map(int, input().strip()))
	note = arr[:]
	ans = len(note)
	for i in range(x):
		ans = (ans - (i + 1)) * note[i] + (i + 1)
		if ans < 0:
			ans += mod
		ans = ans % mod
		temp = []
		for j in range(note[i] - 1):
			if len(note) + len(temp) < x:
				temp += note[i + 1:]
			else:
				break
		note += temp
	print(ans % mod)


z = 1000000007
for _ in range(int(input())):
	x = int(input())
	s = input()
	l = 0
	while len(s) < x:
		l += 1
		s += s[l:] * (int(s[l - 1]) - 1)
	ls = len(s)
	while l != x:
		l += 1
		ls += (ls - l) * (int(s[l - 1]) - 1)
		ls %= z
	print(ls)


def main():
	MOD = int(1000000000.0 + 7)
	n = int(input())
	string = str(input())
	i = 0
	while len(string) < n:
		temp = int(string[i])
		string = string + string[i + 1:] * (temp - 1)
		i += 1
	ans = len(string)
	if i < n:
		while i < n:
			temp = int(string[i])
			ans += (temp - 1) * (ans - i - 1)
			ans = ans % MOD
			i += 1
	print(ans)
	return

def test():
	t = int(input())
	while t:
		main()
		t -= 1
	return
test()


import sys
reader = (line.rstrip() for line in sys.stdin)
t = int(next(reader))
modulo = 10 ** 9 + 7
for _ in range(t):
	x = int(next(reader))
	s = [int(i) for i in next(reader)]
	l = 0
	total = len(s)
	while l < x:
		l += 1
		k = s[l - 1]
		if len(s) < x:
			n = len(s)
			for _ in range(k - 1):
				s.extend(s[l:n])
			total = len(s)
		else:
			total += (k - 1) * (total - l)
			total %= modulo
	print(total)


mod = 10 ** 9 + 7

def main():
	N = int(input())
	for _ in range(N):
		L = int(input())
		S = list(map(int, str(input())))
		res = len(S)
		for i in range(L):
			res = (res - (i + 1)) * S[i] + (i + 1)
			res %= mod
			size = len(S)
			if size < L:
				for _ in range(S[i] - 1):
					S.extend(S[i + 1:size])
		print(res)
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
answerlist = []
MOD = 10 ** 9 + 7
for _ in range(testcases):
	x = int(input())
	initialstring = list(map(int, input()))
	whilelength = len(initialstring)
	initiallength = len(initialstring)
	slicecounter = 1
	while x > whilelength:
		numberatend = initialstring[slicecounter - 1]
		lc = len(initialstring)
		for _ in range(numberatend - 1):
			initialstring += initialstring[slicecounter:lc]
		slicecounter += 1
		whilelength = len(initialstring)
		initiallength = whilelength
	for j in range(slicecounter - 1, x):
		initiallength = (initiallength + (initiallength - (j + 1)) * (initialstring[j] - 1)) % MOD
	answerlist.append(initiallength)
for k in answerlist:
	print(k)


from collections import Counter, OrderedDict
from itertools import permutations as perm
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
import math
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()
for _ in range(t):
	(x,) = gil()
	s = gbs()
	prevLen = len(s)
	p = 0
	while len(s) < x:
		n = len(s)
		for _ in range(s[p] - 1):
			if len(s) >= x:
				break
			for i in range(p + 1, n):
				if len(s) >= x:
					break
				s.append(s[i])
		p += 1
	(p, l, r, ans) = (0, 1, prevLen - 1, 0)
	for p in range(x):
		r = (r + (s[p] - 1) * r) % mod - 1
	ans = (r + x + 1) % mod
	print(ans)


M = int(1000000000.0 + 7)
n = int(input())
while n > 0:
	n -= 1
	x = int(input())
	s = input()
	idx = 0
	length = len(s)
	while length < x:
		t = ord(s[idx]) - ord('1')
		s += s[idx + 1:] * t
		length += (length - idx - 1) * t
		idx += 1
	while idx != x:
		t = ord(s[idx]) - ord('1')
		length += (length - idx - 1) * t % M
		idx += 1
	print(length % M)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(str, input().split())

def li():
	return list(mi())

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
mod = 1000000007

def main():
	for i in range(ii()):
		x = ii()
		s = si()
		c = len(s)
		l = 1
		while l <= x:
			c = ((c - l) * int(s[l - 1]) + l) % mod
			r = len(s)
			for j in range(int(s[l - 1]) - 1):
				if len(s) >= x:
					break
				s += s[l:r]
			l += 1
		print(c)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
MOD = int(1000000000.0 + 7)
for _ in range(t):
	x = int(input())
	s = input()
	l = 0
	n = len(s)
	q = 0
	while n < x:
		l += 1
		q = int(s[l - 1])
		s += s[l:] * (q - 1)
		n = l + (n - l) * q
	while l < x:
		l += 1
		q = int(s[l - 1])
		n = l + (n - l) * q
		n %= MOD
	print(n)


import sys
T = int(sys.stdin.readline().strip())
for _ in range(T):
	x = int(sys.stdin.readline().strip())
	s = list(sys.stdin.readline().strip())
	ls = len(s)
	for l in range(x):
		t = ord(s[l]) - ord('0') - 1
		ls = (ls + (ls - l - 1) * t) % (10 ** 9 + 7)
		e = len(s)
		while len(s) <= x and t > 0:
			s += s[l + 1:e]
			t -= 1
	print(ls)


for _ in range(int(input())):
	x = int(input())
	sen = list(map(int, input()))
	l = 1
	len_sen = len(sen)
	while l <= x:
		itr = sen[l - 1]
		len_sen = (l + (len_sen - l) * sen[l - 1]) % 1000000007
		idx = len(sen)
		for _ in range(itr - 1):
			if len(sen) < x:
				sen += sen[l:idx]
			else:
				break
		l += 1
	print(len_sen)


t = int(input())
ans = []
arr = [None] * 10 ** 6
cur = 0
MOD = 10 ** 9 + 7
for _ in range(t):
	(x, s) = (int(input()), list(map(int, str(input()))))
	max_p = cur + x
	for i in range(cur, min(max_p, cur + len(s))):
		arr[i] = s[i - cur]

	def copy(start, length, times):
		b = start + length
		for i in range(times):
			for j in range(length):
				if b >= max_p:
					return
				arr[b] = arr[start + j]
				b += 1
	val = len(s)
	cnt = 1
	while x:
		if arr[max_p - 1] is None and arr[cur] > 1:
			copy(cur + 1, val - cnt, arr[cur] - 1)
		val = (cnt + (val - cnt) * arr[cur]) % MOD
		cnt += 1
		cur += 1
		x -= 1
	ans.append(val)
for a in ans:
	print(a)