def main(): import sys from collections import defaultdict # Read input s_alpha = list(map(int, sys.stdin.readline().split())) T = sys.stdin.readline().strip() # Compute runs of T if not T: print(0) return t_runs = [] prev = T[0] t_runs.append(prev) for c in T[1:]: if c != prev: t_runs.append(c) prev = c K = len(t_runs) if K == 0: print(0) return # Group runs by character groups = defaultdict(list) for idx, c in enumerate(t_runs): groups[c].append(idx) # Check feasibility and compute a_i for each group product = 1 for c in groups: m_c = len(groups[c]) s = s_alpha[ord(c) - ord('a')] if s < m_c: print(0) return d = s - m_c x = d // m_c r = d % m_c # Assign a_i's for each run in the group # Since the runs are in the order of t_runs, we need to collect them # We'll store a list of a_i's for each run in t_runs order a_list = [1 + x + (1 if i < r else 0) for i in range(m_c)] for idx_in_group, run_pos in enumerate(groups[c]): a = a_list[idx_in_group] t_runs[run_pos] = a # Compute the product product = 1 for a in t_runs: product *= a if product > (1 << 62): print("hel") return print(product) if __name__ == "__main__": main()