n = int(input()) s = input() s1 = [ 0 ] * (n + 1) s2 = [ 0 ] * (n + 1) s3 = [ 0 ] * (n + 1) for i in range(n): s1[i + 1] = s1[i] + (s[i] == 'A') s2[i + 1] = s2[i] + (s[i] == 'C') s3[i + 1] = s3[i] + (s[i] == '?') t = list(s) for i in range(n): if s[i] == '?': na = s1[i] + s3[i] nc = (s2[n] - s2[i + 1]) + (s3[n] - s3[i + 1]) t[i] = ('A' if na <= nc else 'C') numa = 0 answer = 0 for i in range(n): numa += (t[i] == 'A') if t[i] == 'C': answer += numa print(answer)