def compress(s): if not s: return [] res = [s[0]] for c in s[1:]: if c != res[-1]: res.append(c) return res def main(): import sys from collections import defaultdict # Read S_alpha s_alpha_line = sys.stdin.readline().strip().split() s_alpha = {chr(ord('a') + i): int(s_alpha_line[i]) for i in range(26)} # Read T t = sys.stdin.readline().strip() if not t: print(0) return # Compress T t_compressed = compress(t) if not t_compressed: print(0) return # Check if any character in T_compressed has S_alpha[c] == 0 for c in t_compressed: if s_alpha[c] == 0: print(0) return # Count frequency of each character in T_compressed freq = defaultdict(int) for c in t_compressed: freq[c] += 1 # Check if for each c in freq, S_alpha[c] >= freq[c] for c in freq: if s_alpha[c] < freq[c]: print(0) return hel_limit = 2 ** 62 product = 1 for c in freq: m_c = freq[c] sum_available = s_alpha[c] - m_c q, r = divmod(sum_available, m_c) # Compute term = (q+2)^r * (q+1)^(m_c - r) term = 1 # Compute (q+2)^r if r > 0: term *= pow(q + 2, r) # Compute (q+1)^(m_c - r) if m_c - r > 0: term *= pow(q + 1, m_c - r) # Multiply to product and check overflow if product > hel_limit // term: print("hel") return product *= term if product >= hel_limit: print("hel") return print(product) if __name__ == "__main__": main()