import sys def multiply_with_overflow(x, y): if x == 0 or y == 0: return 0 if x > (2**62) // y: return None return x * y def pow_with_overflow(a, b): result = 1 current = a while b > 0: if b % 2 == 1: temp = multiply_with_overflow(result, current) if temp is None: return None result = temp temp = multiply_with_overflow(current, current) if temp is None: return None current = temp b = b // 2 return result def main(): S = list(map(int, sys.stdin.readline().split())) T = sys.stdin.readline().strip() # Process T into runs_t runs_t = [] prev = None for c in T: if c != prev: runs_t.append(c) prev = c # Compute frequency of each character in runs_t from collections import defaultdict freq = defaultdict(int) for c in runs_t: freq[c] += 1 # Check if S has enough for each character S_dict = {chr(ord('a') + i): S[i] for i in range(26)} for c in freq: if S_dict[c] < freq[c]: print(0) return # Compute the product total = 1 for c in freq: required = freq[c] available = S_dict[c] - required k = required m = available q = m // k r = m % k # Compute part1 = (q+2)^r part1 = pow_with_overflow(q + 2, r) # Compute part2 = (q+1)^(k - r) part2 = pow_with_overflow(q + 1, k - r) if part1 is None or part2 is None: print("hel") return contribution = multiply_with_overflow(part1, part2) if contribution is None: print("hel") return new_total = multiply_with_overflow(total, contribution) if new_total is None: print("hel") return total = new_total if total > (2**62): print("hel") return print(total) if __name__ == '__main__': main()