Mahmoud wrote a message s of length n. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That's because this magical paper doesn't allow character number i in the English alphabet to be written on it in a string of length more than a_{i}. For example, if a_1 = 2 he can't write character 'a' on this paper in a string of length 3 or more. String "aa" is allowed while string "aaa" is not.

Mahmoud decided to split the message into some non-empty substrings so that he can write every substring on an independent magical paper and fulfill the condition. The sum of their lengths should be n and they shouldn't overlap. For example, if a_1 = 2 and he wants to send string "aaa", he can split it into "a" and "aa" and use 2 magical papers, or into "a", "a" and "a" and use 3 magical papers. He can't split it into "aa" and "aa" because the sum of their lengths is greater than n. He can split the message into single string if it fulfills the conditions.

A substring of string s is a string that consists of some consecutive characters from string s, strings "ab", "abc" and "b" are substrings of string "abc", while strings "acb" and "ac" are not. Any string is a substring of itself.

While Mahmoud was thinking of how to split the message, Ehab told him that there are many ways to split it. After that Mahmoud asked you three questions:   How many ways are there to split the string into substrings such that every substring fulfills the condition of the magical paper, the sum of their lengths is n and they don't overlap? Compute the answer modulo 10^9 + 7.  What is the maximum length of a substring that can appear in some valid splitting?  What is the minimum number of substrings the message can be spit in? 

Two ways are considered different, if the sets of split positions differ. For example, splitting "aa|a" and "a|aa" are considered different splittings of message "aaa".


-----Input-----

The first line contains an integer n (1 ≤ n ≤ 10^3) denoting the length of the message.

The second line contains the message s of length n that consists of lowercase English letters.

The third line contains 26 integers a_1, a_2, ..., a_26 (1 ≤ a_{x} ≤ 10^3) — the maximum lengths of substring each letter can appear in.


-----Output-----

Print three lines.

In the first line print the number of ways to split the message into substrings and fulfill the conditions mentioned in the problem modulo 10^9  +  7.

In the second line print the length of the longest substring over all the ways.

In the third line print the minimum number of substrings over all the ways.


-----Examples-----
Input
3
aab
2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Output
3
2
2

Input
10
abcdeabcde
5 5 5 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Output
401
4
3



-----Note-----

In the first example the three ways to split the message are:   a|a|b  aa|b  a|ab 

The longest substrings are "aa" and "ab" of length 2.

The minimum number of substrings is 2 in "a|ab" or "aa|b".

Notice that "aab" is not a possible splitting because the letter 'a' appears in a substring of length 3, while a_1 = 2.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Greedy algorithms

Dynamic programming

Complete search

String algorithms


Solutions:
import sys
mod = 10 ** 9 + 7
n = int(input())
s = input()
cnt = list(map(int, input().split()))
dp = [0]
for i in range(0, n):
	dp.append(0)
dp[0] = 1
ans2 = 1
for i in range(1, n + 1):
	o = i - 1
	ml = cnt[ord(s[o]) - ord('a')]
	l = 1
	while ml >= l and o >= 0:
		dp[i] += dp[o]
		dp[i] = dp[i] % mod
		if l > ans2:
			ans2 = l
		l += 1
		o -= 1
		ml = min(ml, cnt[ord(s[o]) - ord('a')])
ans1 = dp[n] % mod
ans3 = 0
ml = n
l = 0
for i in range(n):
	ml = min(ml, cnt[ord(s[i]) - ord('a')])
	l += 1
	if l > ml:
		ans3 += 1
		l = 1
		ml = cnt[ord(s[i]) - ord('a')]
ans3 += 1
print(ans1)
print(ans2)
print(ans3)


n = int(input())
s = input()
t = tuple(map(int, input().split()))
dp = [1]
maxi = 0
for i in range(n):
	ca = 0
	ma = 0
	if i == 0:
		ca += dp[0]
		ma += 1
	else:
		cl = 0
		cp = i
		m = t[ord(s[i]) - 97]
		while cl < m and cp >= 0:
			ca += dp[cp]
			cl += 1
			m = min(m, t[ord(s[cp - 1]) - 97])
			cp -= 1
			ma += 1
	if ma > maxi:
		maxi = ma
	dp.append(ca)
print(dp[-1] % 1000000007)
print(maxi)
i = 0
count = 0
cl = 1
while i < n:
	m = t[ord(s[i]) - 97]
	cp = i + 1
	count += 1
	while cl < m and cp < n:
		m = min(m, t[ord(s[cp]) - 97])
		if cl < m:
			cl += 1
			cp += 1
		else:
			break
	i = i + cl
	cl = 1
	cp = i
print(count)


from sys import stdin, stdout
n = int(stdin.readline())
s = stdin.readline().strip()
length = list(map(int, stdin.readline().strip().split()))
bn = [2 ** i for i in range(10 ** 3 + 2)]
dp = [0 for i in range(n + 10)]
dp[0] = 1
dp[-1] = 1
ans = [0, 1, 0]
dp1 = [0 for i in range(n + 5)]
dp1[0] = 1
for i in range(1, n):
	label = 1
	cnt = min(i + 1, length[ord(s[i]) - ord('a')])
	while label:
		label = 0
		for ind in range(i - cnt + 1, i + 1):
			if length[ord(s[ind]) - ord('a')] < cnt:
				cnt -= 1
				label = 1
				break
	dp1[i] = dp1[i - cnt] + 1
	ans[1] = max(ans[1], cnt)
	if cnt == i + 1:
		dp[i] = bn[cnt - 1]
	else:
		for j in range(1, cnt + 1):
			dp[i] += dp[i - j]
ans[0] = dp[n - 1] % (10 ** 9 + 7)
ans[2] = dp1[n - 1]
stdout.write('\n'.join(list(map(str, ans))))


n = int(input())
s = input()
l = list(map(int, input().split()))
dp = [0] * (n + 2)
mn = [10 ** 4] * (n + 2)
dp[0] = dp[n + 1] = 1
mn[0] = 1
mn[n + 1] = 0
mod = 10 ** 9 + 7
maxx = 1
for i in range(1, n):
	cur = 10 ** 4
	for j in range(i, -1, -1):
		c = ord(s[j]) - ord('a')
		cur = min(cur, l[c])
		if cur < i - j + 1:
			break
		dp[i] = (dp[i] + dp[j - 1]) % mod
		mn[i] = min(mn[i], mn[j - 1] + 1)
		maxx = max(maxx, i - j + 1)
print(dp[n - 1])
print(maxx)
print(mn[n - 1])


import sys
from collections import deque

def debug(x, table):
	for (name, val) in table.items():
		if x is val:
			print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
			return None

def get_minsp(message, As):
	min_sp = 1
	cur_len = 0
	capa = 0
	for ch in message:
		index = ord(ch) - ord('a')
		if cur_len == 0:
			cur_len += 1
			capa = As[index]
		else:
			capa = min(capa, As[index])
			if cur_len + 1 <= capa:
				cur_len += 1
			else:
				min_sp += 1
				cur_len = 1
				capa = As[index]
	return min_sp

def solve():
	MOD = 10 ** 9 + 7
	n = int(input())
	msg = input()
	As = [int(i) for i in input().split()]
	caps = []
	for ch in msg:
		idx = ord(ch) - ord('a')
		caps.append(As[idx])
	dp = [0] * (n + 1)
	dp[0] = 1
	max_len = 0
	lim = 0
	for i in range(1, n + 1):
		lim = caps[i - 1]
		cur_len = 1
		for j in range(i - 1, -1, -1):
			if cur_len > lim:
				break
			max_len = max(max_len, cur_len)
			dp[i] += dp[j]
			dp[i] %= MOD
			lim = min(lim, caps[j - 1])
			cur_len += 1
	min_sp = get_minsp(msg, As)
	print(dp[n])
	print(max_len)
	print(min_sp)

def __starting_point():
	solve()
__starting_point()


import sys
from collections import deque

def debug(x, table):
	for (name, val) in table.items():
		if x is val:
			print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
			return None

def get_minsp(message, As):
	min_sp = 0
	max_len = 0
	cur_len = 0
	capa = 0
	for ch in message:
		index = ord(ch) - ord('a')
		if cur_len == 0:
			cur_len += 1
			max_len = 1
			capa = As[index]
		else:
			capa = min(capa, As[index])
			if cur_len + 1 <= capa:
				cur_len += 1
				max_len = max(max_len, cur_len)
			else:
				min_sp += 1
				cur_len = 1
				capa = As[index]
	return (max_len, min_sp + 1)

def solve():
	MOD = 10 ** 9 + 7
	n = int(input())
	message = input()
	As = [int(i) for i in input().split()]
	capas = []
	for ch in message:
		i = ord(ch) - ord('a')
		capas.append(As[i])
	deq = deque()
	cur_len = 0
	max_len = 0
	min_sp = 0
	capa = 0
	for (i, capa) in enumerate(capas):
		if not deq:
			deq.append(1)
			cur_len = 1
			max_len = 1
		else:
			limit = capa
			lim_len = capa
			for j in range(1, min(i + 1, capa)):
				if min(limit, capas[i - j]) < j + 1:
					lim_len = j
					break
				limit = min(limit, capas[i - j])
			else:
				lim_len = min(i + 1, capa)
			if cur_len + 1 <= lim_len:
				cur_len += 1
				max_len = max(cur_len, max_len)
				deq.appendleft(sum(deq) % MOD)
			else:
				deq.appendleft(sum(deq) % MOD)
				num_del = cur_len - (lim_len - 1)
				for i in range(num_del):
					deq.pop()
				cur_len = lim_len
	(_, min_sp) = get_minsp(message, As)
	print(sum(deq) % MOD)
	print(max_len)
	print(min_sp)
solve()


n = int(input())
s = list(input())
abc = 'abcdefghijklmnopqrstuvwxyz'
limit = list(map(int, input().split()))
mod = int(1000000000.0) + 7
memo = [None] * n

def main():
	(num_ways, longest, min_num) = split(0)
	print(num_ways)
	print(longest)
	print(min_num)

def split(start):
	if start >= len(s):
		return (1, 0, 0)
	if memo[start] is not None:
		return memo[start]
	num_ways = 0
	longest = 0
	min_num = 100000.0
	max_allowed = 100000.0
	for i in range(start, len(s)):
		current_length = i - start + 1
		max_allowed = min(max_allowed, limit[abc.find(s[i])])
		if current_length > max_allowed:
			break
		longest = max(current_length, longest)
		(ss_num_ways, ss_longest, ss_min_num) = split(i + 1)
		num_ways = (num_ways + ss_num_ways) % mod
		longest = max(longest, ss_longest)
		min_num = min(min_num, ss_min_num + 1)
	memo[start] = (num_ways, longest, min_num)
	return memo[start]

def __starting_point():
	main()
__starting_point()


n = int(input())
s = input()
S = [ord(znak) - 97 for znak in s]
a = list(map(int, input().strip().split(' ')))
MOD = 10 ** 9 + 7
st = [0] * (n + 1)
st[0] = 1
maks = [0] * (n + 1)
minway = [10 ** 4] * (n + 1)
minway[0] = 0
for i in range(1, n + 1):
	cnt = 0
	maks_dolzina = n
	for j in range(i, 0, -1):
		maks_dolzina = min(maks_dolzina, a[S[j - 1]])
		if i - j + 1 > maks_dolzina:
			break
		cnt += st[j - 1]
		maks[i] = max(i - j + 1, maks[j - 1], maks[i])
		minway[i] = min(minway[i], 1 + minway[j - 1])
	st[i] = cnt
print(st[-1] % MOD)
print(maks[-1])
print(minway[-1])


cin = lambda : list(map(int, input().split()))
n = int(input())
st = input()
a = list(cin())
dp = [0] * (n + 2)
mn = [10 ** 4] * (n + 2)
mx = 1
dp[0] = 1
dp[-1] = 1
mn[0] = 1
mn[-1] = 0
mod = int(1000000000.0) + 7
for i in range(1, n):
	c = 10 ** 4
	for j in range(i, -1, -1):
		ind = ord(st[j]) - ord('a')
		c = min(c, a[ind])
		if c < i - j + 1:
			break
		dp[i] += dp[j - 1]
		mn[i] = min(mn[i], mn[j - 1] + 1)
		mx = max(mx, i - j + 1)
print('\n'.join(map(str, [dp[n - 1] % mod, mx, mn[n - 1]])))


n = int(input())
st = input()
a = list(map(int, input().split()))
list = [[] for i in range(n)]
arr = []
for i in range(n):
	arr.append(a[ord(st[i]) - 97])
ans3 = 0
add = 0
flag = 0
for i in range(n):
	mini = arr[i]
	count = 1
	j = i
	while count <= mini and j < n:
		list[i].append(j)
		j += 1
		count += 1
		if j < n:
			mini = min(mini, arr[j])
	if i == ans3 and flag == 0:
		add += 1
		ans3 = j
		if j == n:
			flag = 1
counted = [0 for i in range(n)]
for i in list[0]:
	counted[i] = 1
ans2 = len(list[0])
i = 0
while i < n - 1:
	total = counted[i]
	ans2 = max(ans2, len(list[i]))
	for j in list[i + 1]:
		counted[j] += total
	i += 1
	ans2 = max(ans2, len(list[i]))
print(counted[n - 1] % (10 ** 9 + 7))
print(ans2)
print(add)


MOD = int(1000000000.0) + 7
n = int(input())
s = input()
a = list(map(int, input().split()))
dp = [0] * (n + 2)
(dp[0], dp[n + 1]) = (1, 1)
mn = [int(10000.0)] * (n + 2)
(mn[0], mn[n + 1]) = (1, 0)
res = 1
for i in range(1, n):
	cur = int(10000.0)
	for j in range(i, -1, -1):
		c = ord(s[j]) - ord('a')
		cur = min(cur, a[c])
		if cur < i - j + 1:
			break
		dp[i] = (dp[i] + dp[j - 1]) % MOD
		mn[i] = min(mn[i], mn[j - 1] + 1)
		res = max(res, i - j + 1)
print(dp[n - 1])
print(res)
print(mn[n - 1])


MOD = 1000000007
n = int(input())
s = input()
a = list(map(int, input().split()))
dp = [1]
dp_min = [0]
max_len = 1
for i in range(0, n):
	len = 1
	dp.append(dp[-1])
	dp_min.append(dp_min[-1] + 1)
	min_let = a[ord(s[i]) - ord('a')]
	while i - len >= 0 and len < min_let:
		min_let = min(min_let, a[ord(s[i - len]) - ord('a')])
		if len < min_let:
			dp[-1] = (dp[-1] + dp[i - len]) % MOD
			dp_min[-1] = min(dp_min[-1], dp_min[i - len] + 1)
			max_len = max(len + 1, max_len)
		len += 1
print(dp[-1] % MOD)
print(max_len)
print(dp_min[-1])


import sys
mod = 10 ** 9 + 7
inf = 1 << 30

def solve():
	n = int(input())
	msg = input()
	clim = [int(i) for i in input().split()]
	dp = [0] * (n + 1)
	dp[0] = 1
	max_len = 0
	for i in range(1, n + 1):
		limlen = inf
		cur_len = 0
		for j in range(i, 0, -1):
			alpha = ord(msg[j - 1]) - ord('a')
			limlen = min(limlen, clim[alpha])
			cur_len += 1
			if cur_len > limlen:
				break
			max_len = max(max_len, cur_len)
			dp[i] = (dp[i] + dp[j - 1]) % mod
	min_sp = 0
	cur_len = 0
	limlen = inf
	for ch in msg:
		cur_len += 1
		alpha = ord(ch) - ord('a')
		limlen = min(limlen, clim[alpha])
		if cur_len > limlen:
			cur_len = 1
			min_sp += 1
			limlen = clim[alpha]
	print(dp[n])
	print(max_len)
	print(min_sp + 1)
solve()


n = int(input())
s = input()
alf = list(map(int, input().split()))
d = [0 for i in range(n + 1)]
div = int(10 ** 9 + 7)
d[0] = 1
maxlen = -1
a = ord('a')
INF = int(10 ** 10)
md = [INF for i in range(n + 1)]
md[0] = 0
for i in range(1, n + 1):
	l = alf[ord(s[i - 1]) - a]
	minlen = l
	for j in range(l):
		if i - j < 1:
			break
		minlen = min(minlen, alf[ord(s[i - j - 1]) - a])
		if minlen < j + 1:
			break
		maxlen = max(maxlen, j + 1)
		d[i] += d[i - j - 1] % div
		d[i] %= div
		md[i] = min(md[i], 1 + md[i - j - 1])
print(d[n])
print(maxlen)
print(md[n])


def ri():
	return map(int, input().split())
n = int(input())
s = input()
s = [ord(x) - ord('a') for x in s]
a = list(ri())
T = [0 for i in range(n)]
maxs = 0
mins = 0
lasts = 0
for i in range(len(s)):
	if i == 0:
		maxs = 1
		T[i] = 1
		mins += 1
		continue
	mm = i
	smin = 10 ** 9
	for j in range(i, max(i - a[s[i]], -1), -1):
		length = i - j + 1
		flag = 0
		smin = min(a[s[j]], smin)
		if smin < length:
			break
		else:
			mm = j
			if j == 0:
				adding = 1
			else:
				adding = T[j - 1]
			T[i] += adding
			T[i] = T[i] % (10 ** 9 + 7)
	maxs = max(maxs, i - mm + 1)
	if mm > lasts:
		mins += 1
		lasts = i
print(T[n - 1])
print(maxs)
print(mins)


n = int(input())
d = [0] * (n + 1)
way = [n] * (n + 1)
s = '0' + input()
m = list(map(int, input().split()))
d[1] = 1
d[0] = 1
way[0] = 0
way[1] = 1
dic = dict()
dic['0'] = 1000
i = 0
for l in 'abcdefghijklmnopqrstuvwxyz':
	dic[l] = m[i]
	i += 1
high = 1
big = 10 ** 9 + 7
for i in range(2, n + 1):
	z = i - 1
	x = i - dic[s[i]]
	while z >= 0 and x <= z:
		x = max(x, i - dic[s[z]])
		high = max(high, i - z)
		d[i] += d[z]
		way[i] = way[z] + 1
		z -= 1
	d[i] = d[i] % big
	z += 1
	if not z:
		high = i
print(d[-1])
print(high)
print(way[-1])


def getMap(str):
	characters = 'abcdefghijklmnopqrstuvwxyz'
	countMap = {}
	for i in characters:
		countMap[i] = 0
	arrCountMap = []
	for i in str:
		countMap[i] += 1
		arrCountMap.append(countMap)
	return arrCountMap

def getCharIndex():
	characters = 'abcdefghijklmnopqrstuvwxyz'
	index = {}
	count = 0
	for i in characters:
		index[i] = count
		count += 1
	return index
n = int(input())
str = input()
input_a = input()
if n == 1:
	print(1)
	print(1)
	print(1)
	quit()
a = list(map(int, input_a.split(' ')))
dp = []
for i in range(n + 10):
	dp.append(0)
dp[0] = 1
arrCountMap = getMap(str)
mod = 1000000000.0 + 7
maxLen = 0
charIndex = getCharIndex()
for j in range(1, len(str)):
	i = j - 1
	charSet = set()
	charSet.add(str[j])
	while i >= -1:
		allowed = True
		for char in charSet:
			if a[charIndex[char]] < j - i:
				allowed = False
				break
		if not allowed:
			break
		maxLen = max(maxLen, j - i)
		if i == -1:
			dp[j] = int((dp[j] + 1) % mod)
			break
		else:
			dp[j] = int((dp[j] + dp[i]) % mod)
			charSet.add(str[i])
			i -= 1
count = 1
subStringLen = 0
charSet.clear()
for i in range(len(str)):
	newSubstring = False
	charSet.add(str[i])
	subStringLen += 1
	for char in charSet:
		if a[charIndex[char]] < subStringLen:
			newSubstring = True
			break
	if newSubstring:
		count += 1
		charSet.clear()
		charSet.add(str[i])
		subStringLen = 1
print(int(dp[n - 1] % mod))
print(maxLen)
print(count)


M = 10 ** 9 + 7

def o(i):
	return ord(s[i]) - 97
n = int(input())
s = input()
a = [int(item) for item in input().split()]
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1
mn = float('inf')
longest = -float('inf')
maxNb = 1
ii = 1
tmp = a[o(0)]
if a[o(0)] == 1 and n != 1:
	maxNb += 1
	tmp = float('inf')
	ii = 0
if n == 1:
	longest = 1
for i in range(2, n + 1):
	ii += 1
	j = 0
	mn = a[o(i - 1)]
	k = -1
	while j <= i and j <= mn:
		mn = min(mn, a[o(i - j - 1)])
		dp[i] += dp[i - j] % M
		j += 1
		k += 1
	dp[i] %= M
	longest = max(longest, k)
	tmp = min(tmp, a[o(i - 1)])
	if i == n:
		break
	if tmp == ii:
		ii = 0
		tmp = float('inf')
		maxNb += 1
	elif tmp < ii:
		ii = 1
		maxNb += 1
		tmp = a[o(i - 1)]
		if i == n - 1:
			maxNb += 1
	elif i == n - 1:
		if a[o(i)] < ii + 1:
			maxNb += 1
print(dp[n] % M)
print(longest)
print(maxNb)


n = int(input())
s = input()
ai = list(map(int, input().split()))
a = ord('a')
e = 10 ** 9 + 7
dp = [1] + [0] * n
dp2 = [0] * (n + 1)
l = 0
for i in range(1, n + 1):
	dp2[i] = n
	f = i - 1
	ma = 10000
	while f >= 0:
		ma = min(ma, ai[ord(s[f]) - a])
		if ma < i - f:
			break
		dp[i] = (dp[i] + dp[f]) % e
		dp2[i] = min(dp2[i], dp2[f] + 1)
		l = max(l, i - f)
		f -= 1
print(dp[n], l, dp2[n], sep='\n')


(N, s, l) = (int(input()), [ord(x) - ord('a') for x in input()], [int(x) for x in input().split()])
arr = [[1, l[s[0]]]]
total = 1
ma = 1
t = 1
mi = 1
for c in s[1:]:
	tmp = 0
	for i in range(len(arr)):
		arr[i][1] = min(arr[i][1], l[c])
		if i + 1 >= arr[i][1]:
			arr = arr[:i]
			if t > i:
				t = 0
				mi += 1
			break
		else:
			tmp += arr[i][0]
	t += 1
	arr.insert(0, [total, l[c]])
	ma = max(ma, len(arr))
	total += tmp
	total %= 10 ** 9 + 7
print(total)
print(ma)
print(mi)


mod = 10 ** 9 + 7
n = int(input())
s = input()
l = list(map(int, input().split()))
dp = [0] * (n + 1)
dp1 = [0] * (n + 1)
dp[n] = 1
ans2 = 0
ans3 = 0
for i in range(n - 1, -1, -1):
	e = [0] * 26
	for j in range(i, n):
		e[ord(s[j]) - 97] = 1
		flag = 1
		for b in range(26):
			if e[b] and l[b] < j - i + 1:
				flag = 0
				break
		if flag:
			ans2 = max(ans2, j - i + 1)
			if dp1[i] == 0:
				dp1[i] = 1 + dp1[j + 1]
			else:
				dp1[i] = min(dp1[i], 1 + dp1[j + 1])
			dp[i] = (dp[i] + dp[j + 1]) % mod
		else:
			break
print(dp[0])
print(ans2)
print(dp1[0])


import sys
mod = pow(10, 9) + 7
n = int(sys.stdin.readline())
s = sys.stdin.readline()[:-1]
a = [int(x) for x in sys.stdin.readline().split()]
dp = [0 for _ in range(n + 1)]
dp[0] = 1

def ci(c):
	return ord(c) - ord('a')
l = 0
for i in range(1, n + 1):
	f = 0
	for x in range(i - 1, -1, -1):
		f = max(f, i - a[ci(s[x])])
		if f > x:
			continue
		dp[i] = (dp[i] + dp[x]) % mod
		l = max(l, i - x)
print(dp[n])
print(l)
res = 1
m = 9999
j = 0
for i in range(n):
	m = min([a[ci(s[x])] for x in range(j, i + 1)])
	if m < i - j + 1:
		res += 1
		j = i
print(res)


from math import inf
n = int(input())
s = input().strip()
a = [int(X) for X in input().split()]
dp = [0] * (n + 1)
dp[1] = 1
dp[0] = 1
dp12 = [inf] * (n + 1)
dp12[1] = 1
dp12[0] = 0
c = 1
mod = 10 ** 9 + 7
for i in range(2, n + 1):
	m = 10000
	for j in range(i, 0, -1):
		m = min(a[ord(s[j - 1]) - 97], m)
		if i - j + 1 > m:
			break
		dp[i] = (dp[i] + dp[j - 1]) % mod
		dp12[i] = min(dp12[i], dp12[j - 1] + 1)
		c = max(c, i - j + 1)
print(dp[n])
print(c)
print(dp12[n])


read = lambda : map(int, input().split())
n = int(input())
s = input()
a = list(read())
dp = [0] * (n + 2)
mn = [10 ** 4] * (n + 2)
dp[0] = dp[n + 1] = 1
mn[n + 1] = 0
mn[0] = 1
Max = 1
mod = 10 ** 9 + 7
for i in range(1, n):
	res = 0
	cur = 10 ** 4
	for j in range(i, -1, -1):
		c = ord(s[j]) - ord('a')
		cur = min(cur, a[c])
		if cur < i - j + 1:
			break
		dp[i] = (dp[i] + dp[j - 1]) % mod
		mn[i] = min(mn[i], mn[j - 1] + 1)
		Max = max(Max, i - j + 1)
print(dp[n - 1])
print(Max)
print(mn[n - 1])


M = 1000000007
n = int(input())
s = input()
a = list(map(int, input().split()))
ans2 = 0
dp = [0] * n + [1]
for i in range(n):
	maxlen = 9001
	for j in range(1, i + 2):
		maxlen = min(maxlen, a[ord(s[i - j + 1]) - ord('a')])
		if maxlen >= j:
			dp[i] = (dp[i] + dp[i - j]) % M
		else:
			break
		ans2 = max(ans2, j)
print(dp[n - 1])
print(ans2)
ans3 = 1
acc = 0
maxlen = 9001
for i in range(n):
	acc += 1
	maxlen = min(maxlen, a[ord(s[i]) - ord('a')])
	if acc > maxlen:
		acc = 1
		ans3 += 1
		maxlen = a[ord(s[i]) - ord('a')]
print(ans3)


n = int(input())
s = input()
a = list(map(int, input().split()))
MOD = 10 ** 9 + 7
INF = float('inf')
dp = [[0] * 3 for i in range(n + 1)]
dp[0][0] = 1
for i in range(n + 1):
	dp[i][2] = INF
dp[0][2] = 0
num = 0
for i in range(n):
	li = INF
	for j in range(i + 1, n + 1):
		k = ord(s[j - 1]) - ord('a')
		li = min(li, a[k])
		if j - i > li:
			break
		else:
			dp[j][0] += dp[i][0]
			dp[j][0] %= MOD
			dp[j][1] = max(dp[j][1], max(dp[i][1], j - i))
			dp[j][2] = min(dp[j][2], dp[i][2] + 1)
print(dp[n][0])
print(dp[n][1])
print(dp[n][2])


import math
import sys
from bisect import bisect_right, bisect_left, insort_right
from collections import Counter, defaultdict
from heapq import heappop, heappush
from itertools import accumulate, permutations, combinations
from sys import stdout
R = lambda : map(int, input().split())
n = int(input())
arr = list(map(lambda x: ord(x) - ord('a'), input()))
dis = list(R())
comb = [0] * (1 + n)
cnt = [math.inf] * n
mlen = 0
for i in range(n):
	lm = dis[arr[i]]
	j = i
	while j >= 0 and i - j + 1 <= min(lm, dis[arr[j]]):
		lm = min(lm, dis[arr[j]])
		comb[i] = (comb[i] + max(1, comb[j - 1])) % (10 ** 9 + 7)
		cnt[i] = min(cnt[i], (cnt[j - 1] if j >= 1 else 0) + 1)
		j -= 1
	mlen = max(mlen, i - j)
print(comb[n - 1])
print(mlen)
print(max(cnt))


import sys
import copy
import os

def solve():
	MOD = int(1000000000.0 + 7)
	f = 0
	n = int(input())
	s = input()
	alphabet = {chr(ord('a') + i): num for (i, num) in enumerate(map(lambda x: int(x), input().split()))}
	dp = [0 for _ in range(n)]
	minlen = 2 << 20
	maxlen = 0
	minsplitnum = [2 << 20 for _ in range(n)]

	def check(newstr, start, end):
		nl = end - start
		while start < end:
			if alphabet[newstr[start]] < nl:
				return False
			start += 1
		return True
	i = 0
	while i < n:
		if i == 0:
			dp[i] = 1
			maxlen = 1
			minsplitnum[i] = 1
		else:
			f = 0
			dp[i] = (dp[i - 1] + dp[i]) % MOD
			minsplitnum[i] = minsplitnum[i - 1] + 1 if minsplitnum[i] > minsplitnum[i - 1] + 1 else minsplitnum[i]
			j = i - 1
			f = i + 1 - alphabet[s[i]] if i + 1 - alphabet[s[i]] > f else f
			while j >= 0:
				f = i + 1 - alphabet[s[j]] if i + 1 - alphabet[s[j]] > f else f
				if j >= f:
					if j == 0:
						dp[i] = (1 + dp[i]) % MOD
					else:
						dp[i] = (dp[j - 1] + dp[i]) % MOD
					maxlen = i - j + 1 if i - j + 1 > maxlen else maxlen
					if j == 0:
						minsplitnum[i] = 1 if 1 < minsplitnum[i] else minsplitnum[i]
					else:
						minsplitnum[i] = minsplitnum[j - 1] + 1 if minsplitnum[j - 1] + 1 < minsplitnum[i] else minsplitnum[i]
					j -= 1
				else:
					j -= 1
					continue
		i += 1
	print(int(dp[n - 1]))
	print(maxlen)
	print(int(minsplitnum[n - 1]))
solve()