"QAQ" is a word to denote an expression of crying. Imagine "Q" as eyes with tears and "A" as a mouth.

Now Diamond has given Bort a string consisting of only uppercase English letters of length n. There is a great number of "QAQ" in the string (Diamond is so cute!). $8$ illustration by 猫屋 https://twitter.com/nekoyaliu 

Bort wants to know how many subsequences "QAQ" are in the string Diamond has given. Note that the letters "QAQ" don't have to be consecutive, but the order of letters should be exact.


-----Input-----

The only line contains a string of length n (1 ≤ n ≤ 100). It's guaranteed that the string only contains uppercase English letters.


-----Output-----

Print a single integer — the number of subsequences "QAQ" in the string.


-----Examples-----
Input
QAQAQYSYIOIWIN

Output
4

Input
QAQQQZZYNOIWIN

Output
3



-----Note-----

In the first example there are 4 subsequences "QAQ": "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN".

Difficulty: EASY
Tags: Dynamic programming

Complete search
Skill types: Dynamic programming

Complete search

Starter code:

Solutions:
import sys
import math
import bisect

def main():
	s = input()
	n = len(s)
	ans = 0
	for i in range(n):
		for j in range(i + 1, n):
			for k in range(j + 1, n):
				if s[i] + s[j] + s[k] == 'QAQ':
					ans += 1
	print(ans)
main()


import itertools
s = input()
com = list(itertools.combinations(s, 3))
com = [''.join(list(i)) for i in com]
print(com.count('QAQ'))


s = input()
q_count = 0
a = []
for z in s:
	if z == 'A':
		a.append(q_count)
	if z == 'Q':
		q_count += 1
print(sum([(q_count - i) * i for i in a]))


s = input().strip()
s = list(s)
n = len(s)
count = 0
for i in range(n):
	for j in range(i + 1, n):
		for k in range(j + 1, n):
			if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
				count += 1
print(count)


string = input()
substring = 'QAQ'
res = []
count = 0
for i in range(len(string)):
	if string[i] == 'A':
		count += string[:i].count('Q') * string[i:].count('Q')
print(count)


s = str(input())
dp = 'QAQ'
c = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i, len(s)):
			if s[j] == 'A':
				for k in range(j, len(s)):
					if s[k] == 'Q':
						c += 1
print(c)


from itertools import combinations as c
(s, n) = (input(), 0)
for (a, b, c) in c(s, 3):
	if a + b + c == 'QAQ':
		n += 1
print(n)


i = input()
count = 0
for (index, elem) in enumerate(i):
	if elem == 'Q':
		fl = i[index + 1:]
		for (index2, elem2) in enumerate(fl):
			if elem2 == 'A':
				sl = fl[index2 + 1:]
				for (index3, elem3) in enumerate(sl):
					if elem3 == 'Q':
						count += 1
print(count)


n = input()
map = {}
for i in range(0, len(n)):
	if n[i] in map:
		map[n[i]].append(i)
	else:
		map[n[i]] = [i]
counter = 0
if 'A' in map and 'Q' in map:
	for i in range(0, len(map['A'])):
		lesser = 0
		greater = 0
		for k in range(0, len(map['Q'])):
			if map['A'][i] > map['Q'][k]:
				lesser += 1
			else:
				greater += 1
		counter += lesser * greater
print(counter)


s = input()
l = []
c = 0
for i in range(len(s)):
	if s[i] == 'Q':
		c += 1
	l.append(c)
ans = 0
n = l[len(s) - 1]
for i in range(len(s)):
	if s[i] == 'A':
		ans += l[i] * (n - l[i])
print(ans)


a = [i for i in input()]
b = ['Q', 'A', 'Q']
dp = [[0 for i in range(len(b) + 1)] for iq in range(len(a) + 1)]
for i in range(len(b) + 1):
	dp[0][i] = 0
for i in range(len(a) + 1):
	dp[i][0] = 1
for i in range(1, len(a) + 1):
	for j in range(1, len(b) + 1):
		if a[i - 1] == b[j - 1]:
			try:
				dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
			except:
				pass
		else:
			try:
				dp[i][j] = dp[i - 1][j]
			except:
				pass
print(dp[len(a)][3])


s = input()
ans = 0
for i in range(len(s) - 2):
	for j in range(i + 1, len(s) - 1):
		for k in range(j + 1, len(s)):
			if s[i] + s[j] + s[k] == 'QAQ':
				ans += 1
print(ans)


s = input()
qcount = 0
combinations = 0
count = 0
for c in s:
	if c == 'Q':
		count += combinations
		qcount += 1
	elif c == 'A':
		combinations += qcount
print(count)


word = 'QAQ'
_list = input()
counter = 0
main_counter = 0
for i in range(len(_list)):
	if _list[i] == 'Q':
		for j in range(i + 1, len(_list)):
			if _list[j] == 'A':
				for n in range(j + 1, len(_list)):
					if _list[n] == 'Q':
						main_counter += 1
print(main_counter)


import collections
from itertools import combinations
a = list(input())
b = []
for i in range(len(a)):
	if a[i] == 'Q' or a[i] == 'A':
		b.append(a[i])
	else:
		continue
c = list(combinations(b, 3))
aux = 0
for i in range(len(c)):
	if c[i][0] == 'Q' and c[i][1] == 'A' and (c[i][2] == 'Q'):
		aux = aux + 1
	else:
		continue
print(aux)


word = input()
count = 0
len_word = len(word)
dp = [0 for letter in word]
for i in range(len_word):
	if word[i] == 'Q':
		dp[i] = dp[i - 1] + 1
	else:
		dp[i] = dp[i - 1]
for i in range(len_word):
	if word[i] == 'A':
		if not i == 0 or i == len_word - 1:
			count += dp[i - 1] * (dp[-1] - dp[i - 1])
print(count)


s = input()
q = []
c = 0
l = len(s)
for i in range(l):
	for j in range(i + 1, l):
		for k in range(j + 1, l):
			if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
				c += 1
print(c)


data = filter(lambda x: x == 'Q' or x == 'A', input())
states = [0, 0, 0, 0]
for s in data:
	if s == 'Q':
		states[0] += 1
		states[3] += states[2]
	if s == 'A':
		states[2] += states[0]
print(states[3])


s = input()
count = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(len(s[i + 1:])):
			if s[i + 1:][j] == 'A':
				for k in range(len(s[i + j + 1:])):
					if s[i + j + 1:][k] == 'Q':
						count += 1
print(count)


l = [x for x in input()]
res = 0
for i in range(len(l)):
	if l[i] == 'Q':
		for j in range(i + 1, len(l)):
			if l[j] == 'A':
				for k in range(j + 1, len(l)):
					if l[k] == 'Q':
						res += 1
print(res)


def solve(s):
	s = ''.join((char for char in s if char == 'Q' or char == 'A'))
	if len(s) == 0:
		return 0
	q_idx = [i for (i, char) in enumerate(s) if char == 'Q']
	a_count = [0] if s[0] == 'Q' else [1]
	for i in range(1, len(s)):
		if s[i] == 'A':
			a_count.append(a_count[-1] + 1)
		else:
			a_count.append(a_count[-1])
	count = 0
	for i in range(len(q_idx)):
		idx1 = q_idx[i]
		for j in range(i + 1, len(q_idx)):
			idx2 = q_idx[j]
			count += a_count[idx2] - a_count[idx1]
	return count
s = input()
print(solve(s))


s = input()

def code(s):
	res = 0
	n = len(s)
	for i in range(0, n):
		for j in range(i + 1, n):
			for k in range(j + 1, n):
				if s[i] == 'Q' and s[k] == 'Q' and (s[j] == 'A'):
					res += 1
	return res
print(code(s))


s = input()
l = len(s)
q1 = q2 = 0
for i in range(l):
	if s[i] == 'Q':
		q1 += 1
res = 0
for i in range(l):
	if s[i] == 'Q':
		q2 += 1
		q1 -= 1
	if s[i] == 'A':
		res += q1 * q2
print(res)


s = input()
q = 0
qa = 0
qaq = 0
for c in s:
	if c == 'A':
		qa += q
	elif c == 'Q':
		qaq += qa
		q += 1
print(qaq)


str1 = input()
tmp = 'QAQ'

def main():
	ans = 0
	i = 0
	while i < len(str1):
		if str1[i] == 'Q':
			j = i + 1
			while j < len(str1):
				if str1[j] == 'A':
					k = j + 1
					while k < len(str1):
						if str1[k] == 'Q':
							ans += 1
						k += 1
				j += 1
		i += 1
	print(ans)
main()


s = input()
q = []
a = dict()
for v in range(len(s) - 1, -1, -1):
	if s[v] == 'Q':
		q.append(v)
	elif s[v] == 'A':
		a[v] = len(q)
q = []
for v in range(len(s)):
	if s[v] == 'Q':
		q.append(v)
	elif s[v] == 'A':
		a[v] = a[v] * len(q)
ans = 0
for k1 in a:
	ans = ans + a[k1]
print(ans)


def soma(i, j, V):
	resp = 0
	for k in range(i, j + 1):
		resp += V[k]
	return resp

def main():
	C = input()
	T = {}
	T['Q'] = [0]
	T['A'] = []
	qtd = {}
	qtd['Q'] = 0
	qtd['A'] = -1
	it = 'Q'
	nit = 'A'
	for c in C:
		if c == it:
			T[it][qtd[it]] += 1
		elif c == nit:
			qtd[nit] += 1
			T[nit].append(1)
			aux = nit
			nit = it
			it = aux
	resp = 0
	for i in range(qtd['A'] + 1):
		resp += soma(0, i, T['Q']) * T['A'][i] * soma(i + 1, qtd['Q'], T['Q'])
	print(resp)
main()


string = input()
n = len(string)
ref = 'QA'
ans = 0
i = 0
while i <= n - 3:
	if string[i] != 'Q':
		i += 1
		continue
	count_for_a = i + 1
	while count_for_a <= n - 2:
		if string[count_for_a] != 'A':
			count_for_a += 1
			continue
		count_for_q = count_for_a + 1
		while count_for_q <= n - 1:
			if string[count_for_q] == 'Q':
				ans += 1
			count_for_q += 1
		count_for_a += 1
	i += 1
print(ans)


import sys
input = sys.stdin.readline
from collections import *
S = input()[:-1]
ans = 0
for i in range(len(S)):
	for j in range(i + 1, len(S)):
		for k in range(j + 1, len(S)):
			if S[i] == 'Q' and S[j] == 'A' and (S[k] == 'Q'):
				ans += 1
print(ans)


str1 = input()
str2 = list(str1)
all = 0
for i in range(len(str2) - 2):
	if str2[i] == 'Q':
		for j in range(i + 1, len(str2)):
			if str2[j] == 'A':
				all += str1.count('Q', j + 1)
print(all)


def f(s):
	n = len(s)
	gc = 0
	for i in range(n):
		for j in range(i + 1, n):
			for k in range(j + 1, n):
				if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
					gc += 1
	return gc
print(f(input()))


(q, a, v) = (0, 0, 0)
for ch in input():
	if ch == 'Q':
		q += 1
		v += a
	elif ch == 'A':
		a += q
print(v)


st = str(input())
a = []
n = 0
count = 0
for i in range(len(st)):
	if st[i] == 'Q':
		n += 1
		for j in range(len(a)):
			a[j][1] += 1
	elif st[i] == 'A':
		a += [[n, 0]]
for i in a:
	count += i[0] * i[1]
print(count)


s = input()
cnt = 0
for i in range(len(s) - 2):
	if s[i] == 'Q':
		for j in range(i + 1, len(s) - 1):
			if s[j] == 'A':
				for k in range(j + 1, len(s)):
					if s[k] == 'Q':
						cnt += 1
print(cnt)


import itertools
string = list(str(input()))
m = []
for j in string:
	if j == 'Q' or j == 'A':
		m.append(j)
string = ''.join(m)
arr = list(itertools.combinations_with_replacement(string, 3))
count = 0
for i in arr:
	if ''.join(i) == 'QAQ':
		count += 1
print(count)


a = input()
b = 'QAQ'
m = len(a)
n = 3
dp = [[0 for i in range(n + 1)] for j in range(m + 1)]
for i in range(n + 1):
	dp[0][i] = 0
for i in range(m + 1):
	dp[i][0] = 1
for i in range(1, m + 1):
	for j in range(1, n + 1):
		if a[i - 1] == b[j - 1]:
			dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
		else:
			dp[i][j] = dp[i - 1][j]
print(dp[m][n])


text = input()
n = len(text)
rez = 0
for i in range(n):
	if text[i] == 'Q':
		j = i + 1
		while j < n:
			if text[j] == 'A':
				k = j
				while k < n:
					if text[k] == 'Q':
						rez += 1
					k += 1
			j += 1
print(rez)


def fun(st):
	count = 0
	for i in st:
		if i == 'Q':
			count = count + 1
	ans = 0
	leftq = 0
	rightq = count
	for i in st:
		if i == 'Q':
			leftq += 1
			rightq -= 1
		if i == 'A':
			ans += leftq * rightq
	print(ans)
st = input()
fun(st)


a = list(input())
cnt = 0
for i in range(len(a)):
	for j in range(i + 1, len(a)):
		for k in range(j + 1, len(a)):
			if a[i] == 'Q' and a[j] == 'A' and (a[k] == 'Q'):
				cnt += 1
print(cnt)


s = input()
(q, QA, r) = (0, 0, 0)
for i in s:
	if i == 'Q':
		q += 1
		r += QA
	if i == 'A':
		QA += q
print(r)


a = str(input())
ma = 0
(pre, suf) = ([0] * len(a), [0] * len(a))
n = len(a)
for i in range(len(a)):
	if i == 0:
		if a[i] == 'Q':
			pre[i] = 1
		else:
			pre[i] = 0
		continue
	if a[i] == 'Q':
		pre[i] = pre[i - 1] + 1
	else:
		pre[i] = pre[i - 1]
for i in range(len(a) - 1, -1, -1):
	if i == len(a) - 1:
		if a[i] == 'Q':
			suf[i] = 1
		else:
			suf[i] = 0
		continue
	if a[i] == 'Q':
		suf[i] = suf[i + 1] + 1
	else:
		suf[i] = suf[i + 1]
for i in range(len(a) - 1):
	if a[i] == 'A':
		ma += pre[i] * suf[i + 1]
print(ma)


import sys
import os
import math
import re
seq = input()
soln = 0
for i in range(len(seq)):
	if seq[i] == 'A':
		if 'Q' in seq[i:]:
			soln += seq[:i].count('Q') * seq[i:].count('Q')
print(soln)


s = input()
ans = 0
q = 0
tot_q = s.count('Q')
for i in range(len(s)):
	if s[i] == 'Q':
		q += 1
	elif s[i] == 'A':
		ans += q * (tot_q - q)
print(ans)


def count(a, b, m, n):
	if m == 0 and n == 0 or n == 0:
		return 1
	if m == 0:
		return 0
	if a[m - 1] == b[n - 1]:
		return count(a, b, m - 1, n - 1) + count(a, b, m - 1, n)
	else:
		return count(a, b, m - 1, n)
a = str(input())
b = 'QAQ'
print(count(a, b, len(a), len(b)))


s = input()
c = 0
for i in range(len(s)):
	if s[i] == 'A':
		c += s[:i].count('Q') * s[i:].count('Q')
print(c)


s = input()
n = len(s)
res = 0
i = 0
while i < n:
	j = i + 1
	while j < n:
		k = j + 1
		while k < n:
			if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
				res += 1
			k += 1
		j += 1
	i += 1
print(res)


s = input()
posQ = 0
posA = 0
ans = 0
for (count1, i) in enumerate(s):
	if i == 'Q':
		posQ = count1
		for (count2, j) in enumerate(s[posQ + 1:]):
			if j == 'A':
				posA = count2
				for k in s[posA + posQ + 1:]:
					if k == 'Q':
						ans += 1
print(ans)


s = input()
ct = 0
for (i, x) in enumerate(s):
	if x != 'Q':
		continue
	for j in range(i + 1, len(s)):
		if s[j] != 'A':
			continue
		for k in range(j + 1, len(s)):
			if s[k] == 'Q':
				ct += 1
print(ct)


s = input().strip()
p = [0] * len(s)
sf = [0] * len(s)
for i in range(1, len(s)):
	if s[i - 1] == 'Q':
		p[i] = p[i - 1] + 1
	else:
		p[i] = p[i - 1]
for i in range(len(s) - 2, -1, -1):
	if s[i + 1] == 'Q':
		sf[i] = sf[i + 1] + 1
	else:
		sf[i] = sf[i + 1]
ans = 0
for i in range(len(s)):
	if s[i] == 'A':
		ans += p[i] * sf[i]
print(ans)


a = input()
sh = 0
if len(a) > 2:
	for i in range(len(a)):
		if a[i] == 'Q':
			for j in range(i + 1, len(a)):
				if a[j] == 'A':
					if a[j + 1:].count('Q') != -1:
						sh = sh + a[j + 1:].count('Q')
print(sh)


s = input()
c1 = 0
a = []
for i in s:
	if i == 'Q':
		c1 += 1
	if i == 'A':
		a.append(c1)
c = 0
b = []
for i in reversed(range(len(s))):
	if s[i] == 'Q':
		c += 1
	if s[i] == 'A':
		b.append(c)
b = b[::-1]
res = 0
for i in range(len(a)):
	res += a[i] * b[i]
print(res)


s = input()
n = len(s)
t = 0
for i in range(n - 2):
	for j in range(i + 1, n - 1):
		for k in range(j + 1, n):
			if s[i] + s[j] + s[k] == 'QAQ':
				t += 1
print(t)


s = input()
q = a = ans = 0
for i in s:
	if i == 'Q':
		q += 1
		ans += a
	elif i == 'A':
		a += q
print(ans)


s = input()
ans = 0
for i in range(len(s)):
	for j in range(i + 1, len(s)):
		if s[i] == 'Q' and s[j] == 'A':
			ans += s[j + 1:].count('Q')
print(ans)


n = input()
s = ''
for i in n:
	if i in 'QA':
		s += i
n = len(s)
D = {'QAQ': [0] * n, 'QA': [0] * n, 'Q': [0] * n}
if n > 0:
	for i in range(len(s)):
		if s[i] == 'Q':
			D['Q'][i] = D['Q'][i - 1] + 1
			D['QA'][i] = D['QA'][i - 1]
			D['QAQ'][i] = D['QAQ'][i - 1] + D['QA'][i - 1]
		else:
			D['Q'][i] = D['Q'][i - 1]
			D['QA'][i] = D['QA'][i - 1] + D['Q'][i]
			D['QAQ'][i] = D['QAQ'][i - 1]
	print(D['QAQ'][n - 1])
else:
	print('0')


s = input()
(lq, rq) = (0, s.count('Q'))
ans = 0
for c in s:
	if c == 'A':
		ans += lq * rq
	elif c == 'Q':
		lq += 1
		rq -= 1
print(ans)


import re
s = re.sub('[^AQ]', '', input()).strip('A')
(l, counter) = (len(s), 0)
for i in range(l - 2):
	if s[i] == 'Q':
		for j in range(i + 1, l - 1):
			if s[j] == 'A':
				for k in range(j + 1, l):
					counter = counter + 1 if s[k] == 'Q' else counter
print(counter)


s = ''
for j in str(input()):
	if j == 'Q' or j == 'A':
		s += j
count = 0
for j in range(len(s)):
	if s[j] == 'Q':
		for k in range(j + 1, len(s)):
			if s[k] == 'A':
				for l in range(k + 1, len(s)):
					if s[l] == 'Q':
						count += 1
print(count)


t = input()
filt = [x for x in t if x == 'Q' or x == 'A']
times = 0
q_pos = []
a_pos = []
for x in range(0, len(filt)):
	if filt[x] == 'Q':
		q_pos.append(x + 1)
	else:
		a_pos.append(x + 1)
if len(q_pos) < 2 or len(a_pos) < 1:
	print(0)
else:
	for y in a_pos:
		q_before = 0
		q_after = 0
		for x in q_pos:
			if x < y:
				q_before += 1
			else:
				q_after += 1
		times += q_before * q_after
	print(times)


def list_output(s):
	print(' '.join(map(str, s)))

def list_input():
	return list(map(int, input().split()))
s = input()
n = len(s)
res = 0
for i in range(1, n - 1):
	if s[i] == 'A':
		(pr, po) = (0, 0)
		for j in range(i):
			if s[j] == 'Q':
				pr += 1
		for j in range(i + 1, n):
			if s[j] == 'Q':
				po += 1
		res += pr * po
print(res)


s = input()
total = 0
q = [0 for i in range(len(s))]
for i in range(len(s)):
	if s[i] == 'Q':
		total += 1
	q[i] = total
ans = 0
for i in range(len(s)):
	if s[i] == 'A':
		ans += q[i] * (total - q[i])
print(ans)


def sub(arr):
	cou = 0
	for i in range(len(arr)):
		for j in range(i + 1, len(arr)):
			for k in range(j + 1, len(arr)):
				if arr[i] + arr[j] + arr[k] == 'QAQ':
					cou += 1
	return cou
print(sub(input()))


a = 0
d = 0
ans = 0
for i in input():
	if i == 'Q':
		ans += d
		a += 1
	elif i == 'A':
		d += a
print(ans)


from collections import Counter
word = input()
subseq = [x for x in word if x in 'QA']
ans = 0
word = ''.join(subseq)
subseq_q = [len(x) for x in word.split('A')]
for i in range(len(subseq_q)):
	ans += sum(subseq_q[:i + 1]) * sum(subseq_q[i + 1:])
print(ans)


s = input()
q = s.count('Q')
res = 0
for i in range(len(s)):
	x = s[:i].count('Q')
	res += (s[i] == 'A') * x * (q - x)
print(res)


a = input()
p = []
x = v = 0
for i in range(len(a)):
	if a[i] == 'A':
		x += v
		p.append(x)
		v = 0
	elif a[i] == 'Q':
		v += 1
x += v
for i in range(len(p)):
	p[i] *= x - p[i]
print(sum(p))


b = input()
Q = 0
A = 0
QA = []
count = 0
for i in range(len(b)):
	if b[i] == 'Q':
		Q += 1
		QA.append(0)
		count_ = 0
		for j in range(i):
			if b[j] == 'A':
				count_ += QA[j]
		count += count_
	elif b[i] == 'A':
		A += 1
		QA.append(Q)
	else:
		QA.append(0)
		continue
print(count)


k = input()
i = 0
n = len(k)
j = n - 1
ans = 0
while i < n:
	j = n - 1
	if k[i] != 'Q':
		i += 1
		continue
	else:
		while i != j:
			ct = 0
			if k[j] == 'Q':
				t = i
				while t < j:
					if k[t] == 'A':
						ct += 1
					t += 1
			j -= 1
			ans += ct
	i += 1
print(ans)


a = str(input())
l = []
for i in range(len(a)):
	if a[i] == 'Q' or a[i] == 'A':
		l.append(a[i])
s = 0
for i in range(len(l)):
	if l[i] == 'Q':
		for j in range(i + 1, len(l)):
			if l[j] == 'A':
				for k in range(j + 1, len(l)):
					if l[k] == 'Q':
						s = s + 1
print(s)


import sys
line = sys.stdin.readline()
line = list(filter(lambda x: x == 'Q' or x == 'A', line))
res = 0
for (findex, first) in enumerate(line):
	if first == 'Q':
		rest = line[findex + 1:]
		for (mindex, middle) in enumerate(rest):
			if middle == 'A':
				res += rest[mindex:].count('Q')
print(res)


from itertools import combinations as comb
st = input()
new = ''
for i in st:
	if i == 'Q' or i == 'A':
		new += i
c = 0
for i in list(comb(new, 3)):
	final = ''.join(i)
	if final == 'QAQ':
		c += 1
print(c)


def solve():
	s = input()
	ans = 0
	for i in range(len(s) - 2):
		for j in range(i + 1, len(s) - 1):
			for k in range(j + 1, len(s)):
				if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
					ans += 1
	print(ans)
solve()


s = input()
a = 'QAQ'
n = len(s)
e = 0
for i in range(n):
	for j in range(i + 1, n):
		for k in range(j + 1, n):
			if s[i] + s[j] + s[k] == a:
				e += 1
print(e)


l = input()
ans = 0
for i in range(len(l)):
	if l[i] == 'A':
		ans += l[:i].count('Q') * l[i:].count('Q')
print(ans)


s = input()
n = len(s)
prefix = [0] * n
for i in range(n):
	if i > 0:
		prefix[i] = prefix[i - 1]
	if s[i] == 'Q':
		prefix[i] += 1
ans = 0
q_cnt = s.count('Q')
for i in range(n):
	if s[i] == 'A':
		ans += prefix[i] * (q_cnt - prefix[i])
print(ans)


ch = input()
l = list(ch)
com = 0
while 'Q' in l:
	pos = l.index('Q')
	l = l[pos:]
	S = l[1:]
	if 'A' in S:
		S = S[S.index('A'):]
	while 'A' in S and 'Q' in S:
		com += S.count('Q')
		S = S[S.index('A') + 1:]
		if 'A' in S:
			S = S[S.index('A'):]
	l = l[1:]
print(com)


str = input()
Arr = []
for ch in str:
	if ch == 'Q' or ch == 'A':
		Arr.append(ch)
qc = 0
lastest = 0
for i in range(len(Arr) - 1, -1, -1):
	if Arr[i] == 'A' and qc != 0:
		lastest = i
		break
	if Arr[i] == 'Q':
		qc += 1
if lastest == 0:
	print('0')
else:
	sum = 0
	qcount = qc
	last = 1
	result = [0 for _ in range(len(Arr))]
	for i in range(lastest, -1, -1):
		if Arr[i] == 'A':
			sum += qcount
		if Arr[i] == 'Q':
			qcount += 1
		result[i] = sum
	qfcount = 0
	Answer = 0
	for i in range(0, lastest):
		if Arr[i] == 'Q':
			Answer += result[i]
	print(Answer)


s = str(input())
n = len(s)
ans = 0
for i in range(n):
	for j in range(i, n):
		for k in range(j, n):
			if s[i] == 'Q' and (s[j] == 'A' and s[k] == 'Q'):
				ans += 1
print(ans)


pp = input()
count = 0
for i in range(len(pp)):
	for j in range(i + 1, len(pp)):
		for k in range(j + 1, len(pp)):
			if pp[i] == 'Q' and pp[j] == 'A' and (pp[k] == 'Q'):
				count += 1
print(count)


from sys import stdin, stdout
from random import randrange
s = stdin.readline().strip()
n = len(s)
ans = 0
for i in range(n):
	for j in range(i + 1, n):
		for z in range(j + 1, n):
			if s[i] == 'Q' and s[j] == 'A' and (s[z] == 'Q'):
				ans += 1
stdout.write(str(ans))


from itertools import *
print(sum((1 for a in combinations(input(), 3) if ''.join(a) == 'QAQ')))


s = input()
result = []
qty = 0
for letter in s:
	if letter == 'Q':
		qty += 1
	elif letter == 'A':
		result.append(qty)
print(sum((qty_q * (qty - qty_q) for qty_q in result)))


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
S = input()
N = len(S)
ans = 0
for i in range(N):
	if S[i] != 'Q':
		continue
	for j in range(i + 1, N):
		if S[j] != 'A':
			continue
		for k in range(j + 1, N):
			if S[k] == 'Q':
				ans += 1
print(ans)


import sys
stdin = sys.stdin
mod = 1000000007
inf = 1 << 60
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
ns = lambda : stdin.readline().rstrip()
nas = lambda : stdin.readline().split()
s = ns()
n = len(s)
p = n * [0]
q = n * [0]
sp = 0
sq = 0
for i in range(n):
	if s[i] == 'Q':
		sp += 1
	p[i] = sp
	if s[n - i - 1] == 'Q':
		sq += 1
	q[n - i - 1] = sq
ans = 0
for i in range(n):
	if s[i] == 'A':
		ans += p[i] * q[i]
print(ans)


inp = list(input().upper())
for item in inp:
	if item not in ['Q', 'A']:
		inp.remove(item)
found = 0
for x in range(len(inp)):
	if inp[x] == 'Q':
		for y in range(x, len(inp)):
			if inp[y] == 'A':
				for z in range(y, len(inp)):
					if inp[z] == 'Q':
						found += 1
print(found)


s = str(input())
l1 = []
for i in s:
	if i == 'Q' or i == 'A':
		l1.append(i)
chk = 'QAQ'
l = len(l1)
ct = 0
i = 0
j = 0
k = 0
for i in range(l + 1):
	for j in range(i + 1, l):
		for k in range(j + 1, l):
			if chk == l1[i] + l1[j] + l1[k]:
				ct += 1
print(ct)


s = input()
c = 0
for x in range(len(s)):
	if s[x] == 'A':
		p = s[0:x].count('Q')
		q = s[x + 1:].count('Q')
		c += p * q
print(c)


S = input()
n = len(S)
QL = [0] * n
for i in range(n):
	QL[i] = (QL[i - 1] if i > 0 else 0) + int(S[i] == 'Q')
QR = [0] * n
for i in range(n - 1, -1, -1):
	QR[i] = (QR[i + 1] if i < n - 1 else 0) + int(S[i] == 'Q')
print(sum((QL[i] * QR[i] for i in range(n) if S[i] == 'A')))


str = input()
t = len(str)
count = 0
for i in range(0, t):
	if str[i] == 'Q':
		for j in range(i + 1, t):
			if str[j] == 'A':
				for k in range(j + 1, t):
					if str[k] == 'Q':
						count = count + 1
print(count)


s = input()
n = len(s)
m = 0
for i in range(n):
	if s[i] == 'Q':
		for j in range(i + 1, n):
			if s[j] == 'A':
				for k in range(j + 1, n):
					if s[k] == 'Q':
						m += 1
print(m)


s = input()
c = 0
for i1 in range(len(s)):
	for i2 in range(i1 + 1, len(s)):
		for i3 in range(i2 + 1, len(s)):
			if s[i1] + s[i2] + s[i3] == 'QAQ':
				c += 1
print(c)


a = input()
r = []
for i in range(len(a)):
	if a[i] == 'A':
		r.append(i)
w = 0
for i in r:
	s = 0
	e = 0
	s = a[:i].count('Q')
	e = a[i:].count('Q')
	w += e * s
print(w)


x = input()
c = 0
for i in range(0, len(x)):
	if x[i] == 'Q':
		for j in range(i, len(x)):
			if x[j] == 'A':
				for k in range(j, len(x)):
					if x[k] == 'Q':
						c += 1
print(c)


s = input()
a = []
cnt = 0
for c in s:
	if c == 'Q':
		cnt += 1
	if c == 'A':
		a.append(cnt)
sol = 0
for br in a:
	sol += br * (cnt - br)
print(sol)


a = input()
ans = 0
for i in range(len(a)):
	for j in range(i, len(a)):
		for k in range(j, len(a)):
			if a[i] == 'Q' and a[j] == 'A' and (a[k] == 'Q'):
				ans += 1
print(ans)


def qaq():
	count = 0
	s = input()
	for i in range(len(s)):
		for j in range(i + 1, len(s)):
			for k in range(j + 1, len(s)):
				if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
					count += 1
	print(count)
qaq()


import math

def s():
	stri = input()
	n = len(stri)
	ans = 0
	for i in range(n):
		if stri[i] == 'Q':
			for j in range(i + 1, n):
				if stri[j] == 'A':
					for k in range(j + 1, n):
						if stri[k] == 'Q':
							ans += 1
	print(ans)
s()


string = input()
ans = 0
for i in range(len(string)):
	if string[i] == 'Q':
		for k in range(i, len(string)):
			if string[k] == 'A':
				for l in range(k, len(string)):
					if string[l] == 'Q':
						ans += 1
print(ans)


t = 'QAQ'
s = input()
dp = list(map(lambda x: 1 if x == t[0] else 0, s))
for i in range(1, len(t)):
	cnt = 0
	for j in range(0, len(dp)):
		if s[j] == t[i]:
			(cnt, dp[j]) = (cnt + dp[j], cnt)
		else:
			(cnt, dp[j]) = (cnt + dp[j], 0)
print(sum(dp))


import re

def main():
	read = lambda : tuple(map(int, input().split()))
	s = input()
	s1 = ''
	for c in s:
		if c in 'QA':
			s1 += c
	s1 = s1.strip('A')
	L = range(len(s1))
	ans = 0
	for i in L:
		for j in L:
			for k in L:
				if s1[i] == 'Q' and s1[j] == 'A' and (s1[k] == 'Q') and (k > j) and (k > i) and (j > i):
					ans += 1
	print(ans)
main()


s = input()
reals = ''
for elem in s:
	if elem in ['Q', 'A']:
		reals += elem
lst = reals.split('A')
ans = 0
for i in range(len(lst)):
	lst[i] = len(lst[i])
for i in range(len(lst) - 1):
	ans += sum(lst[:i + 1]) * sum(lst[i + 1:])
print(ans)


def count(left, right, arr):
	counter = 0
	for i in arr[left:right]:
		if i == 'Q':
			counter += 1
	return counter
s = input()
positions = []
for i in range(len(s)):
	if s[i] == 'A':
		positions.append(i)
result = 0
for i in range(len(positions)):
	result += count(0, positions[i], s) * count(positions[i], len(s), s)
print(result)


import itertools
print(sum(map(lambda x: x == ('Q', 'A', 'Q'), itertools.combinations(input(), 3))))


import sys
sys.setrecursionlimit(100000000)

def input():
	return sys.stdin.readline()[:-1]
arr = [str(x) for x in input()]
N = len(arr)
result = 0
for i in range(N - 2):
	for j in range(i + 1, N - 1):
		for k in range(j + 1, N):
			if arr[i] == 'Q' and arr[j] == 'A' and (arr[k] == 'Q'):
				result += 1
print(result)


try:
	s = input()
	n = len(s)
	a = [0] * n
	for i in range(n):
		if i >= 1:
			a[i] = a[i - 1]
		if s[i] == 'Q':
			a[i] += 1
	ans = 0
	mx = a[n - 1]
	for i in range(n):
		if s[i] == 'A':
			ans += a[i - 1] * (mx - a[i - 1])
	print(ans)
except:
	pass


inp = input()
a = ''
n = 0
qc = ac = 0
al = []
for i in range(len(inp)):
	if inp[i] == 'Q':
		a += 'q'
		qc += 1
	if inp[i] == 'A':
		a += 'a'
		n += 1
		ac += 1
		al.append(i)
res = 0
for i in al:
	res += inp[0:i].count('Q') * inp[i + 1:].count('Q')
print(res)


s = input()
res = 0
for i in range(len(s)):
	for j in range(i + 1, len(s)):
		for k in range(j + 1, len(s)):
			if s[i] + s[j] + s[k] == 'QAQ':
				res += 1
print(res)


st = input()
totalSum = 0
for i in range(0, len(st)):
	if st[i] == 'Q':
		for k in range(len(st[i:])):
			if st[i:][k] == 'A':
				totalSum += st[i:][k:].count('Q')
print(totalSum)


string = input()
Q_val = string.count('Q')
Q_count = 0
result = 0
for i in string:
	if i == 'Q':
		Q_count += 1
	elif i == 'A':
		result += Q_count * (Q_val - Q_count)
print(result)


n = input()
l = 0
for i in range(len(n)):
	if n[i] == 'Q':
		for j in range(i + 1, len(n)):
			if n[j] == 'A':
				c = n[j:].count('Q')
				l += c
print(l)


s = input()
nq = s.count('Q')
count = 0
i = 1
while i <= nq:
	i += 1
	fq = s.index('Q')
	s = s[fq + 1:]
	na = s.count('A')
	j = 1
	s2 = s
	while j <= na:
		j += 1
		fa = s2.index('A')
		s2 = s2[fa + 1:]
		count += s2.count('Q')
print(count)


s = input()
n = len(s)
k = 0
for i in range(n - 2):
	if s[i] == 'Q':
		for j in range(i + 1, n - 1):
			if s[j] == 'A':
				for z in range(j + 1, n):
					if s[z] == 'Q':
						k += 1
print(k)


s = input()
l = len(s)
ind = []
tqb = [0 for i in range(l)]
tqa = [0 for i in range(l)]
if l < 3:
	print(0)
else:
	if s[0] == 'Q':
		tqb[1] = 1
	for i in range(1, l - 1):
		tqb[i + 1] = tqb[i]
		if s[i] == 'Q':
			tqb[i + 1] += 1
	if s[l - 1] == 'Q':
		tqa[l - 2] = 1
	for i in range(l - 2, 1, -1):
		tqa[i - 1] = tqa[i]
		if s[i] == 'Q':
			tqa[i - 1] += 1
	ans = 0
	for i in range(1, l - 1):
		if s[i] == 'A':
			ans += tqb[i] * tqa[i]
	print(ans)


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_intList_from_str = lambda : list(map(int, list(sys.stdin.readline().strip())))
get_charList_from_str = lambda : list(sys.stdin.readline().strip())
get_int = lambda : int(sys.stdin.readline().strip())
pt = lambda x: sys.stdout.write(str(x))
s = get_string()
prefix_q = [0] + [0] * len(s)
total = 0
for i in range(len(s)):
	prefix_q[i + 1] = prefix_q[i]
	if s[i] == 'Q':
		prefix_q[i + 1] += 1
for i in range(len(s)):
	if s[i] == 'A':
		total += (prefix_q[i + 1] - prefix_q[0]) * (prefix_q[-1] - prefix_q[i + 1])
print(total)


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math

def bin1(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = k ** mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin1(mid + 1, r, k, t, b, val, ans)
		else:
			return bin1(l, mid - 1, k, t, b, val, ans)

def bin2(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = t * k ** mid + b * mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin2(l, mid - 1, k, t, b, val, ans)
		else:
			return bin2(mid + 1, r, k, t, b, val, ans)

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	l = []
	for i in range(2, n + 1):
		if prime[i]:
			l.append(i)
	return l

def bin(l, r, ll, val):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		if val >= ll[mid][0] and val <= ll[mid][1]:
			return mid
		elif val < ll[mid][0]:
			return bin(l, mid - 1, ll, val)
		else:
			return bin(mid + 1, r, ll, val)

def deci(n):
	s = ''
	while n != 0:
		if n % 2 == 0:
			n = n // 2
			s = '0' + s
		else:
			n = n // 2
			s = '1' + s
	return s

def diff(s1, s2):
	if len(s1) < len(s2):
		v = len(s1)
		while v != len(s2):
			s1 = '0' + s1
			v = v + 1
	else:
		v = len(s2)
		while v != len(s1):
			s2 = '0' + s2
			v = v + 1
	c = 0
	for i in range(len(s1)):
		if s1[i:i + 1] != s2[i:i + 1]:
			c = c + 1
	return c
from sys import stdin, stdout

def fac(a, b):
	v = a
	while a != b:
		v *= a - 1
		a = a - 1
	return v

def bino(l, r, n):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		val1 = math.log(n / mid + 1, 2)
		val2 = int(val1)
		if val1 == val2:
			return val1
		elif val1 < 1.0:
			return bino(l, mid - 1, n)
		else:
			return bino(mid + 1, r, n)

def binary(l, r, ll, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		if ll[mid] <= val:
			ans = mid + 1
			return binary(mid + 1, r, ll, val, ans)
		else:
			return binary(l, mid - 1, ll, val, ans)

def odd(n, ans, s):
	if n % 2 != 0 or n in s:
		return ans
	else:
		s.add(n)
		return odd(n // 2, ans + 1, s)

def fact(n):
	if n == 0 or n == 1:
		return 1
	else:
		return n * fact(n - 1)
s = str(stdin.readline())
c = 0
for i in range(len(s)):
	for j in range(i + 1, len(s)):
		for k in range(j + 1, len(s)):
			if s[i:i + 1] == 'Q' and s[j:j + 1] == 'A' and (s[k:k + 1] == 'Q'):
				c += 1
print(c)


x = input()
s = sun = s1 = asun = 0
sun = 0
for i in range(len(x)):
	if x[i] == 'Q':
		s += 1
for i in range(len(x)):
	if x[i] == 'Q':
		s1 += 1
	if x[i] == 'A':
		asun += 1
		sun += s1 * (s - s1)
print(sun)


s = input()
k = len(s)
a = 0
x = 0
for i in range(k):
	a = 0
	if s[i] == 'Q':
		for j in range(i, k):
			if s[j] == 'A':
				a += 1
			if s[j] == 'Q':
				x += a
print(x)


s = input()
res = 0
while True:
	a = s.find('A')
	if a == -1:
		break
	res += s[:a].count('Q') * s[a + 1:].count('Q')
	s = s[:a] + s[a + 1:]
print(res)


string = input().lower()
quantity = 0
for index in range(len(string)):
	if string[index] == 'a':
		quantity += string[index:].count('q') * string[:index].count('q')
print(quantity)


string = input()
nb = 0
for x in range(len(string) - 2):
	for y in range(x + 1, len(string) - 1):
		for z in range(y + 1, len(string)):
			if string[x] == 'Q' and string[y] == 'A' and (string[z] == 'Q'):
				nb += 1
print(nb)


ss = input()
t = len(ss)
cnt = 0
for i in range(t - 2):
	for j in range(i + 1, t - 1):
		for k in range(j + 1, t):
			if ss[i] == 'Q' and ss[j] == 'A' and (ss[k] == 'Q'):
				cnt += 1
print(cnt)


from itertools import combinations
s = list(input())
per = combinations(s, 3)
res = list(map(''.join, per))
c = 0
st = 'QAQ'
for i in res:
	if st == i:
		c = c + 1
print(c)


import sys
import math
from collections import defaultdict as ddict
from collections import deque
import heapq
from queue import Queue
fast_inp = lambda : sys.stdin.readline().split('\n')[0]
read_list = lambda : fast_inp().split(' ')
read_listi = lambda : list(map(int, fast_inp().split(' ')))
read_listf = lambda : list(map(float, fast_inp().split(' ')))
read_int = lambda : int(fast_inp())
read_float = lambda : float(fast_inp())
read = lambda : fast_inp()
DEBUG = False

def debug(**kwargs):
	if not DEBUG:
		return
	print('*' * 20)
	for (k, v) in kwargs.items():
		print(f'\t{k}:{v}\t')
	print('*' * 20)

def solve() -> None:
	s = read()
	result = 0
	for i in range(len(s)):
		if s[i] != 'Q':
			continue
		for j in range(i + 1, len(s)):
			if s[j] != 'A':
				continue
			for k in range(j + 1, len(s)):
				if s[k] == 'Q':
					result += 1
	print(result)
t = 1
for _ in range(t):
	solve()


import sys
x = input()
ans = 0
for i in range(len(x)):
	for j in range(i, len(x)):
		for k in range(j, len(x)):
			if x[i] == 'Q' and x[j] == 'A' and (x[k] == 'Q'):
				ans += 1
print(ans)


n = input()
list = []
for i in range(len(n)):
	if n[i] == 'Q':
		list.append(i)
lista = []
for i in range(len(list)):
	for j in range(i, len(list)):
		num = n[int(list[i]):int(list[j]) + 1].count('A')
		lista.append(num)
print(sum(lista))


def handle():
	s = input()
	s = [i for i in s if i == 'Q' or i == 'A'][::-1]
	cur = 0
	total = 0
	qs = 0
	for i in s:
		if i == 'Q':
			total += cur
			qs += 1
		else:
			cur += qs
	print(total)
handle()


t = input('')
count = 0
for i in range(len(t) - 2):
	for j in range(i + 1, len(t) - 1):
		for k in range(j + 1, len(t)):
			if t[i] + t[j] + t[k] == 'QAQ':
				count += 1
			else:
				pass
print(count)


c = 0
s = input()
t = ''
for i in s:
	if i in 'AQ':
		t += i
for i in range(len(t)):
	if t[i] == 'Q':
		for j in range(i + 1, len(t) - 1):
			if t[j] == 'A':
				c += t[j + 1:].count('Q')
print(c)


l = list(input())
h = 0
for i in range(len(l)):
	for j in range(i, len(l)):
		for n in range(j, len(l)):
			if l[i] + l[j] + l[n] == 'QAQ':
				h += 1
print(h)


def main():
	text = ''.join(list(filter(lambda x: x == 'Q' or x == 'A', input())))
	qs = []
	for i in range(len(text)):
		if text[i] == 'Q':
			qs.append(i)
	count = 0
	for i in range(len(qs)):
		for j in range(i + 1, len(qs)):
			count += noOfAs(text, qs[i], qs[j])
	print(count)

def noOfAs(text, a, b):
	return len(list(filter(lambda x: x == 'A', text[a:b + 1])))
main()


def findall(s, t):
	result = []
	for (i, c) in enumerate(s):
		if c == t:
			result.append(i)
	return result

def count(s):
	list_A = findall(s, 'A')
	list_Q = findall(s, 'Q')
	result = 0
	idx = 0
	ppre = 0
	for a in list_A:
		pre = ppre
		post = 0
		for (i, q) in enumerate(list_Q[idx:], idx):
			if a > q:
				pre += 1
			else:
				post = len(list_Q) - pre
				ppre = pre
				idx = i
				break
		result += pre * post
	return result
s = input()
print(count(s))


word = input()
arrA = []
arrQ = []
for i in range(len(word)):
	if word[i] == 'A':
		arrA.append(i)
	if word[i] == 'Q':
		arrQ.append(i)
cnt = 0
for i in range(len(arrQ)):
	for j in range(len(arrA)):
		for k in range(i + 1, len(arrQ)):
			if arrA[j] > arrQ[i] and arrQ[k] > arrA[j]:
				cnt += 1
print(cnt)


L = input()
line = L
c = 0
for i in range(len(line)):
	if line[i] == 'Q':
		for j in range(i + 1, len(line)):
			if line[j] == 'A':
				for n in range(j + 1, len(line)):
					if line[n] == 'Q':
						c += 1
print(c)


s = list(input())
res = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i + 1, len(s)):
			if s[j] == 'A':
				for k in range(j + 1, len(s)):
					if s[k] == 'Q':
						res += 1
print(res)


s = input()
(c, n, found) = (0, len(s), False)
for i in range(n):
	if s[i] == 'Q':
		for j in range(i + 1, n):
			if s[j] == 'A':
				for k in range(j + 1, n):
					if s[k] == 'Q':
						c += 1
print(c)


def main():
	s = str(input())
	l = s.count('Q')
	curr = -1
	ans = 0
	for i in range(1, l):
		lcurr = s[curr + 1:].find('Q') + curr + 1
		rcurr = s[lcurr + 1:].find('Q') + lcurr + 1
		for j in range(1, l - i + 1):
			ans += s[lcurr:rcurr].count('A')
			rcurr = s[rcurr + 1:].find('Q') + rcurr + 1
		curr = lcurr
	print(ans)
main()


string = input()
a = []
count = 0
for i in string:
	if i == 'Q':
		count += 1
	elif i == 'A':
		a.append(count)
ans = 0
for i in a:
	ans += i * (count - i)
print(ans)


inp = input()
q = qa = qaq = 0
for let in inp:
	if let == 'Q':
		q += 1
	if q > 0 and let == 'A':
		qa += q
	if qa > 0 and let == 'Q':
		qaq += qa
print(qaq)


s = input()
x = []
for i in range(len(s)):
	if s[i] == 'Q':
		x.append(i)
ans = 0
for j in range(len(x)):
	for k in range(j, len(x)):
		ans += s[x[j]:x[k]].count('A')
print(ans)


import re

def calc_for_q(s):
	result = 0
	q_count = s.count('q')
	for ch in s:
		if ch == 'a':
			result += q_count
		elif ch == 'q':
			q_count -= 1
	return result
s = input().lower()
s = re.sub('[^aq]', '', s)
try:
	s = s[s.index('q'):]
except:
	print(0)
	exit()
result = 0
for i in range(len(s)):
	if s[i] == 'q':
		result += calc_for_q(s[i:])
print(result)


def qaq_counter(l, counter):
	q_pos = [i for i in range(len(l)) if l[i] == 'Q']
	a_pos = [i for i in range(len(l)) if l[i] == 'A']
	for first in q_pos:
		for second in a_pos:
			for third in q_pos:
				if first < second and second < third:
					counter += 1
	return counter
import sys
n = list(str(sys.stdin.readline()))
print(qaq_counter(n, 0))


x = input()
n = len(x)
left = [0] * n
cnt = 0
for i in range(n):
	if x[i] == 'Q':
		cnt += 1
	left[i] = cnt
right = [0] * n
cnt = 0
for i in range(n - 1, -1, -1):
	if x[i] == 'Q':
		cnt += 1
	right[i] = cnt
res = 0
for i in range(n):
	if x[i] == 'A':
		res += left[i] * right[i]
print(res)


ans = 0
s = input().strip()
prefixSum = [0 for i in range(len(s))]
for i in range(len(s)):
	if s[i] == 'Q':
		prefixSum[i] = 1
	if i - 1 >= 0:
		prefixSum[i] += prefixSum[i - 1]
for i in range(len(s)):
	if s[i] == 'A' and i - 1 >= 0:
		(x, y) = (prefixSum[i - 1], prefixSum[len(s) - 1] - prefixSum[i])
		ans += x * y
print(ans)


n = list(input())
l = len(n)
c = 0
for i in range(l):
	for j in range(i, l):
		for k in range(j, l):
			if n[i] == 'Q' and n[j] == 'A' and (n[k] == 'Q'):
				c += 1
print(c)


import sys
from bisect import bisect_left, bisect_right, insort
from collections import deque, Counter
from math import gcd, sqrt, factorial, ceil, log10, log2, floor
from itertools import permutations
from heapq import heappush, heappop, heapify
inf = float('inf')
mod = 10 ** 9 + 7

def factorial_p(n, p):
	ans = 1
	if n <= p // 2:
		for i in range(1, n + 1):
			ans = ans * i % p
	else:
		for i in range(1, p - n):
			ans = ans * i % p
		ans = pow(ans, p - 2, p)
		if n % 2 == 0:
			ans = p - ans
	return ans

def nCr_p(n, r, p):
	ans = 1
	while n != 0 or r != 0:
		(a, b) = (n % p, r % p)
		if a < b:
			return 0
		ans = ans * factorial_p(a, p) * pow(factorial_p(b, p), p - 2, p) * pow(factorial_p(a - b, p), p - 2, p) % p
		n //= p
		r //= p
	return ans

def prime_sieve(n):
	flag = n % 6 == 2
	sieve = bytearray((n // 3 + flag >> 3) + 1)
	for i in range(1, int(n ** 0.5) // 3 + 1):
		if not sieve[i >> 3] >> (i & 7) & 1:
			k = 3 * i + 1 | 1
			for j in range(k * k // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
			for j in range(k * (k - 2 * (i & 1) + 4) // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
	return sieve

def prime_list(n):
	res = []
	if n > 1:
		res.append(2)
	if n > 2:
		res.append(3)
	if n > 4:
		sieve = prime_sieve(n + 1)
		res.extend((3 * i + 1 | 1 for i in range(1, (n + 1) // 3 + (n % 6 == 1)) if not sieve[i >> 3] >> (i & 7) & 1))
	return res

def is_prime(n):
	if n < 5 or n & 1 == 0 or n % 3 == 0:
		return 2 <= n <= 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			p = p * p % n
			if p == n - 1:
				break
		else:
			return False
	return True

def all_factors(n):
	(small, large) = ([], [])
	for i in range(1, int(n ** 0.5) + 1, 2 if n & 1 else 1):
		if not n % i:
			small.append(i)
			large.append(n // i)
	large.reverse()
	small.extend(large)
	return small

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
string = input()
n = len(string)
suffix_q = [0] * n
count = 0
if string[-1] == 'Q':
	count = 1
for i in range(n - 2, -1, -1):
	if string[i] == 'Q':
		count += 1
	elif string[i] == 'A':
		suffix_q[i] = count
suffix_a = [0] * n
count = 0
if string[-1] == 'A':
	count = 1
for i in range(n - 2, -1, -1):
	if string[i] == 'A':
		count += 1
	elif string[i] == 'Q':
		suffix_a[i] = count
total = 0
for i in range(n - 1):
	if string[i] == 'Q':
		for j in range(i + 1, n):
			total += suffix_q[j]
print(total)


s = input()
c = 0
l = len(s)
arr = [0]
for i in s:
	if i == 'Q':
		arr.append(arr[-1] + 1)
	else:
		arr.append(arr[-1])
for i in range(l):
	if s[i] == 'A':
		c = c + arr[i + 1] * (arr[-1] - arr[i + 1])
print(c)


s = input()
QA = 0
Q = 0
QAQ = 0
for c in s:
	if c == 'Q':
		Q += 1
		QAQ += QA
	elif c == 'A':
		QA = Q + QA
print(QAQ)


def f():
	s = input().strip('A')
	l = []
	for i in s:
		if i == 'Q':
			l.append(1)
		if i == 'A':
			l.append(0)
	ans = 0
	for i in range(len(l)):
		if l[i] == 0:
			ans += sum(l[:i]) * sum(l[i + 1:])
	print(ans)
f()


s = input().strip()
prefix = [','] * len(s)
suffix = [','] * len(s)
(pre, post, ans) = (0, 0, 0)
for i in range(len(s)):
	if s[i] == 'Q':
		pre += 1
	elif s[i] == 'A':
		prefix[i] = pre
for i in range(len(s) - 1, -1, -1):
	if s[i] == 'Q':
		post += 1
	elif s[i] == 'A':
		suffix[i] = post
for i in range(len(s)):
	if s[i] == 'A':
		ans += prefix[i] * suffix[i]
print(ans)


arr = input()
counter = 0
for i in range(len(arr)):
	if arr[i] == 'A':
		a = arr[:i]
		b = arr[i + 1:]
		counter += a.count('Q') * b.count('Q')
print(counter)


n = tuple(input())
ans = 0
sheathQ = []
sheathA = []
for i in range(len(n)):
	if n[i] == 'Q':
		sheathQ = list(n)
		sheathA = list(n)
		for j in range(i + 1, len(n)):
			if sheathA[j] == 'A':
				sheathQ = list(n)
				for k in range(j + 1, len(n)):
					if sheathQ[k] == 'Q':
						ans += 1
						sheathQ[k] = None
				sheathA[j] = None
print(ans)


def main():
	solver(input())

def solver(s):
	print(helper(s)[0])

def helper(s):
	if len(s) == 0:
		return [0, 0, 0]
	else:
		ccounts = helper(s[1:])
		if s[0] == 'Q':
			ccounts[0] += ccounts[1]
			ccounts[2] += 1
		elif s[0] == 'A':
			ccounts[1] += ccounts[2]
		return ccounts
main()


n = input()
l = []
q1 = []
q2 = []
count = 0
for i in range(len(n)):
	if n[i] == 'A':
		l.append(i)
for j in l:
	q1.append(n[:j + 1].count('Q'))
	q2.append(n[j + 1:].count('Q'))
for k in range(len(q1)):
	count += q1[k] * q2[k]
print(count)


import math
import timeit
import re
fibonacci_numbers = [0, 1, 2]
for i in range(3, 20):
	fibonacci_numbers.append(fibonacci_numbers[i - 1] + fibonacci_numbers[i - 2])
import collections

def remove_common(x, y):
	count = lambda x: collections.Counter((c for c in x.lower() if c.isalpha()))
	(cx, cy) = (count(x), count(y))
	diff = cx - cy
	rev_diff = cy - cx
	assert len(rev_diff) == 0, '%s in y and not x' % ''.join(rev_diff.elements())
	return ''.join(sorted(diff.elements()))
q = 1
while q > 0:
	q -= 1
	a = ''
	ans = input()
	count = 0
	for letter in ans:
		if letter == 'A' or letter == 'Q':
			a += letter
	for i in range(len(a) - 2):
		for j in range(i, len(a) - 1):
			for k in range(j, len(a)):
				if a[i] == 'Q' and a[j] == 'A' and (a[k] == 'Q'):
					count += 1
	print(count)


s = input()
cnt = [0, 0, 0]
for c in s:
	if c == 'Q':
		cnt[0] += 1
		cnt[2] += cnt[1]
	if c == 'A':
		cnt[1] += cnt[0]
print(cnt[2])


def numberofsubsequences(s):
	n = len(s)
	i = 0
	res = 0
	while i < n:
		if s[i] == 'Q':
			j = i + 1
			while j < n:
				if s[j] == 'A':
					k = j
					while k < n:
						if s[k] == 'Q':
							res += 1
						k += 1
				j += 1
		i += 1
	return res
print(numberofsubsequences(input()))


s = str(input())
q = s.count('Q')
k = q
ans = 0
for i in range(len(s)):
	if s[i] == 'Q':
		q = q - 1
		k = q
		for j in range(i + 1, len(s)):
			if s[j] == 'A':
				ans = ans + k
			if s[j] == 'Q':
				k = k - 1
print(ans)


import sys
import collections
import math
import functools
import itertools
import bisect
import operator
import heapq
import random
true = True
false = False
null = None
tcid = 0
tcmax = 99999999

def compute(val, func):
	return func(val)

def seq(lo, hi, step=1):
	return range(lo, hi + 1, step)

def sround(val, nd):
	return f'{val:.{nd}f}'

def ceil(a, b):
	ans = a // b
	if a % b != 0:
		ans += 1
	return ans

def perr(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)

def line():
	ln = sys.stdin.readline().strip()
	if ln == '':
		sys.exit()
	return ln

def lines(n):
	return [line() for i in range(n)]

def split(ln=None):
	return (ln or line()).split()

def num(str=None):
	str = str or line()
	return float(str) if '.' in str else int(str)

def nums(o=None):
	if o is not None:
		if isinstance(o, int):
			o = lines(o)
		elif isinstance(o, str):
			o = split(o)
	return list(map(num, o or split()))

def mainloop(tcid=1):
	s = line()
	q = 'Q'
	a = 'A'
	ans = 0
	for ii in range(len(s) - 2):
		for jj in range(ii + 1, len(s) - 1):
			for kk in range(jj + 1, len(s)):
				if s[ii] == q and s[jj] == a and (s[kk] == q):
					ans += 1
	print(ans)
while tcid < tcmax:
	tcid += 1
	mainloop(tcid)


s = input()
QCount = 0
QACount = 0
emojiCount = 0
for i in s:
	if i == 'Q':
		QCount += 1
		emojiCount += QACount
	elif i == 'A':
		QACount += QCount
print(emojiCount)


c = 0
s = list(map(str, input()))
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i + 1, len(s)):
			if s[j] == 'A':
				for k in range(j + 1, len(s)):
					if s[k] == 'Q':
						c += 1
print(c)


s = input().rstrip()
count = 0
for i in range(len(s) - 2):
	for j in range(i + 1, len(s) - 1):
		for k in range(j + 1, len(s)):
			test = s[i] + s[j] + s[k]
			if test == 'QAQ':
				count += 1
print(count)


string = input().strip(' ')

def ON2solution(string):
	count = 0
	for i in range(len(string)):
		if string[i] == 'A':
			num_Q_before = 0
			num_Q_after = 0
			for j in range(i):
				if string[j] == 'Q':
					num_Q_before = num_Q_before + 1
			for k in range(i + 1, len(string)):
				if string[k] == 'Q':
					num_Q_after = num_Q_after + 1
			count = count + num_Q_after * num_Q_before
	return count
print(ON2solution(string))


s = input()
a = 0
n = len(s)
for i in range(n):
	if s[i] == 'A':
		a += s[:i].count('Q') * s[i:].count('Q')
print(a)


from collections import Counter
import math
import itertools
import random
import sys
sys.setrecursionlimit(100000)

def nq(s, next, num, f):
	print(s)
	print(next)
	l = len(s)
	if l == 1:
		if num == 2 and s[0] == next[0]:
			print('Got One')
			return 1
		else:
			return 0
	elif num == 3:
		return 1
	for i in range(l):
		print('original: ', s)
		print('s: ', s[i:])
		print('number: ', num)
		print(next)
		if s[i] == next[0]:
			f += nq(s[i + 1:], [next[1], next[0]], num + 1, f)
		else:
			f += nq(s[i + 1:], next, num, f)
	print('returning f: ', f)
	print('Where s: ', s)
	return f

def main():
	s = input()
	ans = 0
	for i in range(len(s)):
		if s[i] == 'Q':
			for j in range(i + 1, len(s)):
				if s[j] == 'A':
					for k in range(j + 1, len(s)):
						if s[k] == 'Q':
							ans += 1
	print(ans)
main()


s = input().strip()

def solve(s: str) -> int:
	n = len(s)
	count = 0
	for i in range(n):
		if s[i] == 'Q':
			for j in range(i + 1, n):
				if s[j] == 'A':
					for k in range(j + 1, n):
						if s[k] == 'Q':
							count += 1
	return count
print(solve(s))


n = input()
q = []
k = 0
for i in range(len(n)):
	if n[i] == 'Q':
		q.append(i)
m = len(q)
for i in range(m - 1):
	for j in range(i + 1, m):
		k += n[q[i] + 1:q[j]].count('A')
print(k)


word = input()
n = len(word)
(qfirst, qlast) = (0, 0)
for i in range(n):
	if word[i] == 'Q':
		qlast += 1
res = 0
for i in range(n):
	if word[i] == 'Q':
		qfirst += 1
		qlast -= 1
	if word[i] == 'A':
		res += qfirst * qlast
print(res)


def main():
	s = input()
	p = s.count('Q')
	q = r = 0
	for c in s:
		if c == 'A':
			r += q * p
		elif c == 'Q':
			q += 1
			p -= 1
	print(r)
main()


s = input()
tot = s.count('Q')
t1 = 0
ans = 0
for i in s:
	if i == 'Q':
		tot -= 1
		t1 += 1
	elif i == 'A':
		ans += tot * t1
print(ans)


s = input()
a = 0
for (i, j) in enumerate(s):
	if j == 'A':
		a += s[:i].count('Q') * s[i + 1:].count('Q')
print(a)


def count(a, b):
	(m, n) = (len(a), len(b))
	lookup = [[0] * (n + 1) for _ in range(m + 1)]
	for i in range(n + 1):
		lookup[0][i] = 0
	for i in range(m + 1):
		lookup[i][0] = 1
	for i in range(1, m + 1):
		for j in range(1, n + 1):
			if a[i - 1] == b[j - 1]:
				lookup[i][j] = lookup[i - 1][j - 1] + lookup[i - 1][j]
			else:
				lookup[i][j] = lookup[i - 1][j]
	return lookup[m][n]

def main():
	a = input()
	b = 'QAQ'
	print(count(a, b))
main()


s = input()
qaq = ''
for i in s:
	if i in 'QA':
		qaq += i
n = len(qaq)
cnt = 0
for i in range(n):
	for j in range(i + 1, n):
		for k in range(j + 1, n):
			if qaq[i] + qaq[j] + qaq[k] == 'QAQ':
				cnt += 1
print(cnt)


import re
rexpr = re.compile('[AQ]+')
string = input()
string_ = ''
for elem in rexpr.findall(string):
	string_ += elem
total = 0
for i in range(len(string_)):
	if string_[i] == 'Q':
		for j in range(i + 1, len(string_)):
			if string_[j] == 'A':
				total += string_[j + 1:].count('Q')
print(total)


l = []
c = 0
q = 0
for i in input():
	if i == 'A':
		l.append(q)
	elif i == 'Q':
		q += 1
for i in l:
	c += i * (q - i)
print(c)


a = input()
t = len(a)
lists = [[] for i in range(t + 1)]
read = [[] for i in range(t + 1)]
i = 0
k = 0
b = 0
sume = 0
for item in a:
	if item == 'Q' or item == 'A':
		lists[i] = item
		i = i + 1
		if item == 'A':
			read[k] = i
			k = k + 1
while read[b] != []:
	sume = (read[b] - 1 - b) * (i - read[b] - k + b + 1) + sume
	b = b + 1
print(sume)


a = input().strip()
result = 0
s = ''
for i in a:
	if i in 'QA':
		s += i
i = 0
j = 1
k = 2
for i in range(len(s) - 1):
	if s[i] == 'A':
		q1 = s[:i].count('Q')
		q2 = s[i + 1:].count('Q')
		result += q1 * q2
print(result)


import sys
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def print_array(a):
	print(' '.join(map(str, a)))

def main():
	s = input()
	qc = 0
	for c in s:
		if c == 'Q':
			qc += 1
	dp = []
	(ans, qb) = (0, 0)
	for c in s:
		if c == 'Q':
			qb += 1
		elif c == 'A':
			dp.append([qb, qc - qb])
	for e in dp:
		ans += e[0] * e[1]
	print(ans)
main()


t = list(input())
score = 0
for i in range(len(t)):
	if t[i] == 'Q':
		for j in range(i, len(t)):
			if t[j] == 'A':
				for k in range(j, len(t)):
					if t[k] == 'Q':
						score += 1
print(score)


def Qaq():
	text = input()
	output = 0
	for i in range(len(text)):
		if text[i] == 'Q':
			for j in range(i + 1, len(text)):
				if text[j] == 'A':
					for x in range(j + 1, len(text)):
						if text[x] == 'Q':
							output += 1
	print(output)
	return
Qaq()


def count(a, b):
	m = len(a)
	n = len(b)
	lookup = [[0] * (n + 1) for i in range(m + 1)]
	for i in range(n + 1):
		lookup[0][i] = 0
	for i in range(m + 1):
		lookup[i][0] = 1
	for i in range(1, m + 1):
		for j in range(1, n + 1):
			if a[i - 1] == b[j - 1]:
				lookup[i][j] = lookup[i - 1][j - 1] + lookup[i - 1][j]
			else:
				lookup[i][j] = lookup[i - 1][j]
	return lookup[m][n]
s = input()
s1 = 'QAQ'
print(count(s, s1))


s = input()
c = 0
for i in range(0, len(s)):
	c1 = 0
	c2 = 0
	if s[i] == 'A':
		for j in range(0, i):
			if s[j] == 'Q':
				c1 = c1 + 1
		for j in range(i + 1, len(s)):
			if s[j] == 'Q':
				c2 = c2 + 1
	c = c + c1 * c2
print(c)


def solve(s):
	cnt = 0
	for i in range(len(s)):
		if s[i] == 'Q':
			for j in range(i + 1, len(s)):
				if s[j] == 'A':
					for k in range(j + 1, len(s)):
						if s[k] == 'Q':
							cnt += 1
	print(cnt)
s = input()
solve(s)


import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().rstrip().split())
inl = lambda : list(map(int, input().split()))
out = lambda x, s='\n': print(s.join(map(str, x)))
s = ins()
ans = 0
for i in range(len(s)):
	count = 0
	if s[i] == 'Q':
		for j in range(i + 1, len(s)):
			if s[j] == 'A':
				count += 1
			if s[j] == 'Q':
				ans += count
print(ans)


def ans(s):
	Q = 0
	for i in s:
		if i == 'Q':
			Q += 1
	pre = 0
	res = 0
	for i in s:
		if i == 'Q':
			pre += 1
		elif i == 'A':
			res += pre * (Q - pre)
	return res
s = input()
print(ans(s))


s = input()
ans1 = 0
ans2 = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i + 1, len(s)):
			if s[j] == 'A':
				for g in range(j + 1, len(s)):
					if s[g] == 'Q':
						ans1 += 1
s = s[::-1]
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i + 1, len(s)):
			if s[j] == 'A':
				for g in range(j + 1, len(s)):
					if s[g] == 'Q':
						ans2 += 1
print(max(ans1, ans2))


import os, sys
from collections import defaultdict, Counter, deque
from io import BytesIO, IOBase
from math import gcd

def lcm(x, y):
	return x * y // gcd(x, y)

def main():
	s = input().strip()
	x = s.count('Q')
	y = 0
	an = 0
	for i in s:
		if i == 'Q':
			y += 1
		elif i == 'A':
			an += y * (x - y)
	print(an)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


arr = input()
dp = []
num = 0
for i in reversed(range(len(arr))):
	if arr[i] == 'Q':
		num += 1
	elif arr[i] == 'A':
		dp.append(num)
ans = 0
for i in range(len(dp)):
	ans += (num - dp[i]) * dp[i]
print(ans)


a = []
for i in input():
	if i in 'AQ':
		a.append(i)
n = len(a)
count = 0
for i in range(n):
	for j in range(n):
		for k in range(n):
			if a[i] == a[k] == 'Q' and a[j] == 'A' and (i < j < k):
				count += 1
print(count)


jomle = input()
tedad = 0
for (i, harf1) in enumerate(jomle):
	if harf1 == 'Q':
		for (j, harf2) in enumerate(jomle[i + 1:]):
			if harf2 == 'A':
				for harf3 in jomle[i + j + 2:]:
					if harf3 == 'Q':
						tedad += 1
print(tedad)


n = input()
i = 0
for x in range(len(n)):
	if n[x] == 'Q':
		for y in range(x, len(n)):
			if n[y] == 'A':
				for z in range(y, len(n)):
					if n[z] == 'Q':
						i += 1
print(i)


s = list(input())
l = len(s)
q = 0
qa = 0
qaq = 0
for i in s:
	if i == 'Q':
		q += 1
		qaq += qa
	elif i == 'A':
		qa += q
print(qaq)


str = input()
s = []
ans = 0
for i in str:
	if i == 'Q' or i == 'A':
		s.append(i)
for i in range(len(s)):
	if s[i] == 'A':
		a = s[:i].count('Q')
		b = s[i:].count('Q')
		ans = ans + a * b
print(ans)


from sys import stdin
from sys import stdout
sprint = stdout.write
sinput = stdin.readline

def func():
	inp = input()
	QBc = 0
	QAc = 0
	QB = []
	QA = []
	A = []
	for i in range(len(inp)):
		QB.append(QBc)
		QA.append(QAc)
		QBc += 1 if inp[i] == 'Q' else 0
		QAc += 1 if inp[-1 - i] == 'Q' else 0
		if inp[i] == 'A':
			A.append(i)
	QA.reverse()
	QBc = 0
	for i in range(len(A)):
		QBc += QB[A[i]] * QA[A[i]]
	print(QBc)
t = int(1)
while t != 0:
	t -= 1
	func()


s = input()
sum_QAQ = 0
len_s = len(s)
for i in range(len_s):
	if s[i] == 'Q':
		n_Q = 0
		n_A = 0
		for j in range(i + 1, len_s):
			if s[j] == 'A':
				n_A += 1
			elif s[j] == 'Q':
				sum_QAQ = sum_QAQ + n_A
print(sum_QAQ)


s = input()
d = [0, 0, 0]
for i in range(len(s)):
	if s[i] == 'Q':
		d[2] += d[1]
		d[0] += 1
	elif s[i] == 'A':
		d[1] += d[0]
print(d[2])


s = input()
src = 'QAQ'
pre = [[0 for i in range(4)] for j in range(len(s) + 1)]
for i in range(len(s) + 1):
	pre[i][0] = 1
for i in range(1, len(s) + 1):
	for j in range(1, 4):
		if s[i - 1] == src[j - 1]:
			pre[i][j] = pre[i - 1][j - 1] + pre[i - 1][j]
		else:
			pre[i][j] = pre[i - 1][j]
print(pre[len(s)][3])


sequence = list(input())
QAQCount = 0
for i in range(len(sequence)):
	if sequence[i] == 'Q':
		for j in range(i + 1, len(sequence)):
			if sequence[j] == 'A':
				for e in range(j + 1, len(sequence)):
					if sequence[e] == 'Q':
						QAQCount += 1
print(QAQCount)


arr = input()
n = arr.find('A')
cnt = 0
while n != -1:
	cnt += arr[:n].count('Q') * arr[n:].count('Q')
	n = arr.find('A', n + 1)
print(cnt)


s = input()
c = 0
for (i, j) in enumerate(s):
	for q in range(len(s)):
		if j == 'Q':
			if q > i and s[q] == 'A':
				for h in range(len(s)):
					if h > q and s[h] == 'Q':
						c += 1
print(c)


s = ''.join([i if i == 'Q' or i == 'A' else '' for i in list(input())])
ans = 0
for i in range(len(s)):
	if s[i] == 'A':
		ans += s[:i].count('Q') * s[i:].count('Q')
print(ans)


s = input()
q = s.count('Q')
i = 0
ans = 0
for x in s:
	i += x == 'Q'
	if x == 'A':
		ans += i * (q - i)
print(ans)


seq = input()
n = len(seq)
count = 0
for i in range(n):
	for j in range(i, n):
		for k in range(j, n):
			if seq[i] + seq[j] + seq[k] == 'QAQ':
				count += 1
print(count)


def count(a, b):
	m = len(a)
	n = len(b)
	dp = [[0] * (n + 1)] * (m + 1)
	for i in range(n + 1):
		dp[0][i] = 0
	for i in range(m + 1):
		dp[i][0] = 1
	for i in range(1, m + 1):
		for j in range(1, n + 1):
			if a[i - 1] == b[j - 1]:
				dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
			else:
				dp[i][j] = dp[i - 1][j]
	return dp[i][j]
x = input()
y = 'QAQ'
print(count(x, y))


word = input()
n = word.count('Q')
(counter, appeared) = (0, 0)
for i in word:
	if i == 'Q':
		appeared += 1
	if i == 'A':
		counter += (n - appeared) * appeared
print(counter)


s = input()
cnt = len([i for i in s if i == 'Q'])
sm = 0
for i in range(len(s)):
	if s[i] == 'A':
		cnt1 = len([j for j in s[:i] if j == 'Q'])
		sm += cnt1 * (cnt - cnt1)
print(sm)


string = list(input())
count = 0
for i in range(len(string)):
	if string[i] == 'Q':
		for j in range(i + 1, len(string)):
			if string[j] == 'A':
				for k in range(j + 1, len(string)):
					if string[k] == 'Q':
						count += 1
print(count)


S = input()
N = len(S)
count = 0
count2 = 0
W = 0
h = 0
h1 = 0
Z = 0
for i in range(0, N):
	if h == 0 or h1 == 0:
		if S[i] == 'Q':
			count = count + 1
			h = 1
		elif S[i] == 'A' and h != 0:
			h1 = 1
			pos = i
		if h == 1 and h1 == 1:
			for j in range(pos + 1, N):
				if S[j] == 'Q':
					count2 = count2 + 1
					Z = 1
			if Z == 1:
				W = W + count2 * count
				count2 = 0
				Z = 0
				h1 = 0
print(W)


s = input()
A = [i for (i, letter) in enumerate(s) if letter == 'A']
Q = [i for (i, letter) in enumerate(s) if letter == 'Q']
val = 0
for i in A:
	val += sum((x < i for x in Q)) * sum((x > i for x in Q))
print(val)


a = input()
count = 0
for i in range(len(a)):
	if a[i] == 'A':
		z = a[:i].count('Q')
		x = a[i + 1:].count('Q')
		count += z * x
print(count)


a = input()
d = 0
e = 0
f = []
for i in range(len(a)):
	if a[i] == 'Q':
		f.append(d)
		for j in range(len(f)):
			e += f[-1] - f[j]
	elif a[i] == 'A':
		d += 1
print(e)


s = str(input(''))
ans = []
c1 = 0
c2 = 0
for i in range(len(s)):
	if s[i] == 'A':
		for j in range(i):
			if s[j] == 'Q':
				c1 += 1
		for k in range(i, len(s)):
			if s[k] == 'Q':
				c2 += 1
		ans.append(c1 * c2)
		c1 = 0
		c2 = 0
print(sum(ans))


st = list(input())
co = 0
for i in range(len(st)):
	if st[i] == 'Q':
		for j in range(i, len(st)):
			if st[j] == 'A':
				for k in range(j, len(st)):
					if st[k] == 'Q':
						co += 1
print(co)


t = list(input())
q_inds = [i for (i, x) in enumerate(t) if x == 'Q']
a_inds = [i for (i, x) in enumerate(t) if x == 'A']
ans = 0
for (i, qi) in enumerate(q_inds):
	for ai in a_inds:
		if qi < ai:
			for qi_2 in q_inds[i + 1:]:
				if ai < qi_2:
					ans += 1
print(ans)


from itertools import combinations
s = [x for x in input() if x == 'Q' or x == 'A']
s = ''.join(s)
n = len(s)
m = 0
y = combinations(s, 3)
for i in y:
	if ''.join(i) == 'QAQ':
		m += 1
print(m)


s = input()
allQ = tempA = prev = prevPrev = 0
for i in s:
	if i == 'Q':
		(prev, prevPrev) = (2 * prev - prevPrev + tempA * allQ, prev)
		allQ += 1
		tempA = 0
	elif i == 'A':
		tempA += 1
print(prev)


def iinput():
	return [int(i) for i in input().split()]
mas = list(input())
ans = 0
pref = [0 for i in mas]
for i in range(len(mas)):
	if i == 0:
		if mas[i] == 'Q':
			pref[i] = 1
		else:
			pref[i] = 0
	elif mas[i] == 'Q':
		pref[i] = pref[i - 1] + 1
	else:
		pref[i] = pref[i - 1]
max_pref = pref[-1]
for i in range(len(mas)):
	if mas[i] == 'A':
		ans += pref[i] * (max_pref - pref[i])
print(ans)


S = input()
counter = 0
for i in range(len(S)):
	if S[i] != 'Q':
		continue
	for j in range(i + 1, len(S)):
		if S[j] != 'A':
			continue
		for k in range(j + 1, len(S)):
			if S[k] == 'Q':
				counter += 1
print(counter)


x = list(input())
i = 0
a = []
while i < len(x):
	if x[i] == 'Q' or x[i] == 'A':
		a.append(x[i])
	i = i + 1
l = 0
for i in range(len(a)):
	for j in range(i + 1, len(a)):
		for k in range(j + 1, len(a)):
			if a[i] == 'Q' and a[j] == 'A' and (a[k] == 'Q'):
				l = l + 1
print(l)


import sys
s = sys.stdin.readline()
qAfter = s.count('Q')
qBefore = 0
sol = 0
for i in range(len(s)):
	if s[i] == 'A':
		sol = sol + qAfter * qBefore
	if s[i] == 'Q':
		qAfter -= 1
		qBefore += 1
print(sol)


k = ''.join((i for i in input() if i in 'QA'))
a = 0
for i in range(len(k) + 1):
	try:
		if k[i] == 'A':
			a += k[:i].count('Q') * k[i:].count('Q')
	except:
		print(a)


s = input()
n = len(s)
summ = 0
for i in range(n):
	if s[i] == 'A':
		prv = 0
		nxt = 0
		for j in range(i):
			if s[j] == 'Q':
				prv += 1
		for j in range(i, n):
			if s[j] == 'Q':
				nxt += 1
		summ += prv * nxt
print(summ)


n = input()
string = ''
totalCount = 0
for i in n:
	if i == 'Q' or i == 'A':
		string += i
for i in range(len(string)):
	if string[i] == 'A':
		countb = string.count('Q', 0, i)
		counta = string.count('Q', i, len(string))
		totalCount += counta * countb
print(totalCount)


word = input()
countt = 0
for i in range(0, len(word)):
	if word[i] == 'Q':
		for j in range(i, len(word)):
			if word[j] == 'A':
				for k in range(j, len(word)):
					if word[k] == 'Q':
						countt = countt + 1
print(countt)


s1 = input()
s2 = 'QAQ'
matrix = []
for i in range(len(s2)):
	matrix.append([])
	for j in range(len(s1)):
		matrix[-1].append(0)
cnt = 0
for i in range(len(s1)):
	if s1[i] == s2[0]:
		cnt += 1
	matrix[0][i] = cnt
for i in range(1, len(s2)):
	for j in range(len(s1)):
		if j != 0:
			matrix[i][j] = matrix[i][j - 1]
			if s1[j] == s2[i]:
				matrix[i][j] += matrix[i - 1][j - 1]
print(matrix[-1][-1])


A = list(input())
n = 0
count = []
temp = 0
for i in range(0, len(A)):
	if n == 0:
		if ord(str(A[i])) == ord('Q'):
			n += 1
	elif ord(str(A[i])) == ord('Q'):
		n += 1
		count = count + [temp]
		temp = 0
	elif ord(str(A[i])) == ord('A'):
		temp += 1
final = 0
for i in range(0, len(count)):
	final = final + int(count[i]) * (i + 1) * (n - i - 1)
print(final)


a = input()
i = 0
m = 0
co = []
while i < len(a):
	if a[i] == 'Q':
		co.append(a[i])
	if a[i] == 'A':
		co.append(a[i])
	i = i + 1
i = 0
count = 0
while i < len(co):
	if co[i] == 'Q':
		m = i
		while m < len(co):
			if co[m] == 'A':
				n = m
				while n < len(co):
					if co[n] == 'Q':
						count = count + 1
					n = n + 1
			m = m + 1
	i = i + 1
print(count)


s = input()
qaq = 0
for i in range(0, len(s)):
	if s[i] == 'A':
		before = s[:i].count('Q')
		after = s[i + 1:].count('Q')
		qaq += before * after
print(qaq)


a = [i for i in input() if i == 'Q' or i == 'A']
result = 0
for i in range(len(a) - 2):
	if a[i] == 'Q':
		for l in range(i, len(a)):
			if a[l] == 'A':
				pos = a[l + 1:].count('Q')
				result += pos
print(result)


from itertools import combinations
s = list(combinations(input(), 3))
cnt = 0
for i in s:
	if i == ('Q', 'A', 'Q'):
		cnt += 1
print(cnt)


str = input()
a = []
prev = 0
for i in str:
	if i == 'Q':
		prev += 1
	if i == 'A':
		a.append(prev)
qaq = 0
for i in a:
	qaq += i * (prev - i)
print(qaq)


QAQ = [x for x in input() if x == 'Q' or x == 'A']

def countQ(arr):
	return sum((1 for x in arr if x == 'Q'))
result = sum((countQ(QAQ[:i]) * countQ(QAQ[i:]) for (i, v) in enumerate(QAQ) if v == 'A'))
print(result)


a = input()
b = 'QAQ'
m = len(a)
l = [[0] * 4 for _ in range(m + 1)]
for i in range(4):
	l[0][i] = 0
for i in range(m + 1):
	l[i][0] = 1
for i in range(1, m + 1):
	for j in range(1, 4):
		if a[i - 1] == b[j - 1]:
			l[i][j] = l[i - 1][j - 1] + l[i - 1][j]
		else:
			l[i][j] = l[i - 1][j]
print(l[m][3])


s = input()
a = list()
b = list()
k = 0
for i in range(len(s)):
	if s[i] == 'A':
		a.append(i)
	elif s[i] == 'Q':
		k += 1
	b.append(k)
if len(a) == 0:
	print(0)
else:
	suum = b[a[0]] * (b[-1] - b[a[0]])
	buf = suum
	for i in range(1, len(a)):
		if a[i] - a[i - 1] > 1:
			buf = b[a[i]] * (b[-1] - b[a[i]])
			suum += buf
		else:
			suum += buf
	print(suum)


class Main:

	def __init__(self):
		self.s = input()

	def main(self):
		pref_sum_As = [0] * self.s.__len__()
		self.fill_cumulative_freq('A', pref_sum_As)
		subsequence_QAQ_count = 0
		for i in range(self.s.__len__() - 2):
			if self.s[i] == 'Q':
				for j in range(i + 2, self.s.__len__()):
					if self.s[j] == 'Q':
						subsequence_QAQ_count += pref_sum_As[j] - pref_sum_As[i]
		print(subsequence_QAQ_count)

	def fill_cumulative_freq(self, c, pref_sum):
		if self.s[0] == c:
			pref_sum[0] = 1
		for i in range(self.s.__len__() - 1):
			pref_sum[i + 1] = pref_sum[i]
			if self.s[i + 1] == c:
				pref_sum[i + 1] += 1
Main().main()


s = str(input())
counter = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i, len(s)):
			if s[j] == 'A':
				for k in range(j, len(s)):
					if s[k] == 'Q':
						counter += 1
print(counter)


s = str(input())
ns = ''
for i in s:
	if i in 'QA':
		ns += i
for i in range(100):
	if len(ns) > 0:
		if ns[-1] == 'A':
			ns = ns[:-1]
	if len(ns) > 0:
		if ns[0] == 'A':
			ns = ns[1:]
(num, f) = (0, 1)
for i in range(len(ns)):
	if ns[i] == 'A':
		f = ns[:i].count('Q')
		num += f * ns[i:].count('Q')
print(num)


s = input()
count = 0
prev_scores = [0, 0]
for i in s:
	if i == 'Q':
		prev_scores[0] += 1
		count += prev_scores[1]
	elif i == 'A':
		prev_scores[1] += prev_scores[0]
print(count)


s = input()
cnt = 0
for i in range(len(s)):
	for j in range(i + 1, len(s)):
		for l in range(j + 1, len(s)):
			if s[i] == 'Q' and s[j] == 'A' and (s[l] == 'Q'):
				cnt += 1
print(cnt)


from collections import defaultdict as dd
from collections import Counter as ccd
from itertools import permutations as pp
from itertools import combinations as cc
from random import randint as rd
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq as hq
import math
mod = pow(10, 9) + 7

def inp(flag=0):
	if flag == 0:
		return list(map(int, input().strip().split(' ')))
	else:
		return int(input())

def count_recursion(i, j):
	if j == len(target):
		return 1
	if i == len(source):
		return 0
	if source[i] == target[j]:
		return count_recursion(i + 1, j + 1) + count_recursion(i + 1, j)
	else:
		return count_recursion(i + 1, j)
source = input().strip()
target = 'QAQ'
print(count_recursion(0, 0))


n = list(input())
for b in n:
	if b == 'Q' or b == 'A':
		pass
	else:
		n.remove(b)
t = 0
for i in range(len(n)):
	for j in range(i + 1, len(n)):
		for k in range(j + 1, len(n)):
			if n[i] == 'Q' and n[j] == 'A' and (n[k] == 'Q'):
				t += 1
print(t)


a = list(input())
q = 0
w = []
for i in range(len(a)):
	if a[i] == 'Q':
		q += 1
		for j in range(len(w)):
			w[j][0] += 1
	if a[i] == 'A':
		w.append([0, q])
c = 0
for (i, j) in w:
	if i != 0 and j != 0:
		c += max(i, j) * min(i, j)
print(c)


string = input()
res = 0
for a in range(0, len(string)):
	if string[a] in list('Q'):
		for d in range(a + 1, len(string)):
			if string[d] in list('A'):
				for n in range(d + 1, len(string)):
					if string[n] == 'Q':
						res += 1
print(res)


def QAQ(string):
	res = 0
	for i in range(len(string)):
		if string[i] == 'Q':
			for j in range(i + 1, len(string)):
				if string[j] == 'A':
					for k in range(j + 1, len(string)):
						if string[k] == 'Q':
							res += 1
	print(res)
QAQ(input())


s = input()
l = len(s)
count = 0
for x in range(l):
	if s[x] == 'Q':
		for y in range(x + 1, l):
			if s[y] == 'A':
				for z in range(y + 1, l):
					if s[z] == 'Q':
						count += 1
print(count)


string = input()
count = 0
Qplace = []
for i in string:
	if i == 'A' or i == 'Q':
		count += 1
		if i == 'Q':
			Qplace.append(count)
total = 0
if len(Qplace) > 0:
	lastQ = Qplace[-1]
while len(Qplace) >= 2:
	i = Qplace[0]
	qplace = 1
	while qplace < len(Qplace):
		total += Qplace[qplace] - i - 1 - (qplace - 1)
		qplace += 1
	Qplace.remove(i)
print(total)


start = input()
(qaq, aq, q) = ({}, {}, {})

def gqaq(s):
	if s in qaq:
		return qaq[s]
	ans = 0
	for (i, l) in enumerate(s):
		if l == 'Q':
			ans += gaq(s[i + 1:])
	qaq[s] = ans
	return ans

def gaq(s):
	if s in aq:
		return aq[s]
	ans = 0
	for (i, l) in enumerate(s):
		if l == 'A':
			ans += gq(s[i + 1:])
	aq[s] = ans
	return ans

def gq(s):
	if s in q:
		return q[s]
	ans = 0
	for (i, l) in enumerate(s):
		if l == 'Q':
			ans += 1
	q[s] = ans
	return ans
print(str(gqaq(start)))


s = input()
q_count = 0
qa_count = 0
qaq_count = 0
for c in s:
	if c == 'Q':
		q_count += 1
		qaq_count += qa_count
	elif c == 'A':
		qa_count += q_count
print(qaq_count)


s = input()
co = 0
for a in range(len(s) - 2):
	if s[a] == 'Q':
		for b in range(a + 1, len(s) - 1):
			if s[b] == 'A':
				for c in range(b + 1, len(s)):
					if s[c] == 'Q':
						co += 1
print(co)


ins = input()
q_cnt = ins.count('Q')
(qs, has) = (0, 0)
for (i, ch) in enumerate(ins):
	if ch == 'A':
		has += qs * (q_cnt - qs)
	elif ch == 'Q':
		qs += 1
print(has)


n = input()
ql = qr = sum = 0
for i in range(0, len(n)):
	if n[i] == 'A':
		for j in range(0, i):
			if n[j] == 'Q':
				ql += 1
		for j in range(i, len(n)):
			if n[j] == 'Q':
				qr += 1
		sum += ql * qr
		qr = 0
		ql = 0
print(sum)


st = input()
Qcount = [0 for i in range(len(st))]
QAcount = [0 for i in range(len(st))]
QAQcount = [0 for i in range(len(st))]
if st[0] == 'Q':
	Qcount[0] = 1
for i in range(1, len(st)):
	ch = st[i]
	if ch == 'Q':
		Qcount[i] = Qcount[i - 1] + 1
		QAcount[i] = QAcount[i - 1]
		QAQcount[i] = QAQcount[i - 1] + QAcount[i - 1]
	elif ch == 'A':
		Qcount[i] = Qcount[i - 1]
		QAcount[i] = QAcount[i - 1] + Qcount[i - 1]
		QAQcount[i] = QAQcount[i - 1]
	else:
		Qcount[i] = Qcount[i - 1]
		QAcount[i] = QAcount[i - 1]
		QAQcount[i] = QAQcount[i - 1]
print(QAQcount[-1])


def solve(stg):
	cnt_q = [0]
	n = len(stg)
	stg = list(stg)
	stg.insert(0, 0)
	for i in range(1, n + 1, 1):
		cnt_q.append(cnt_q[i - 1])
		if stg[i] == 'Q':
			cnt_q[i] += 1
	res = 0
	for i in range(1, n + 1, 1):
		if stg[i] == 'A':
			res += cnt_q[i - 1] * (cnt_q[n] - cnt_q[i])
	return res

def dp004_QAQ(stg):
	return solve(stg)
stg = input()
print(dp004_QAQ(stg))


from sys import stdin

def calc_q(s):
	return len(list(filter(lambda x: x == 'Q', s)))

def calc_aq(s):
	if len(s) <= 1:
		return 0
	res = 0
	for (i, c) in enumerate(s):
		if c != 'A' or i == len(s) - 1:
			continue
		res += calc_q(s[i + 1:])
	return res
s = stdin.readline().rstrip()
res = 0
for (i, c) in enumerate(s):
	if c != 'Q' or i == len(s) - 1:
		continue
	res += calc_aq(s[i + 1:])
print(res)


import math as mt
import sys, string, bisect
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
s = input().strip()
i = 0
ans = 0
l = []
m = []
l = [int(s[0] == 'Q')]
i = 1
while i < len(s):
	l.append(l[-1] + int(s[i] == 'Q'))
	i += 1
m = [int(s[-1] == 'Q')]
i = len(s) - 2
while i >= 0:
	m.append(m[-1] + int(s[i] == 'Q'))
	i -= 1
m = m[-1::-1]
ans = 0
for i in range(len(s)):
	if s[i] == 'A':
		ans += l[i] * m[i]
print(ans)


s = input()
qaq = 'QAQ'
dp = [[-1, -1, -1] for x in range(len(s))]

def fun(ss, i, j):
	if j == 3:
		return 1
	if len(ss) > 3 or i >= len(s):
		return 0
	if dp[i][j] != -1:
		return dp[i][j]
	if s[i] == qaq[j]:
		dp[i][j] = fun(ss + s[i], i + 1, j + 1) + fun(ss, i + 1, j)
		return dp[i][j]
	dp[i][j] = fun(ss, i + 1, j)
	return dp[i][j]
print(fun('', 0, 0))


s = input()
res = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i, len(s)):
			if s[j] == 'A':
				str = s[j:]
				x = str.count('Q')
				res += x
print(res)


A = input()
N = len(A)
prefix = []
count = 0
for i in range(N):
	if A[i] == 'Q':
		count += 1
	prefix += [count]
suffix = [0] * N
count = 0
for i in range(1, N + 1):
	if A[-i] == 'Q':
		count += 1
	suffix[-i] = count
sum = 0
for i in range(N):
	if A[i] == 'A':
		sum += suffix[i] * prefix[i]
print(sum)


s = input()
q1 = 0
for c in s:
	if c == 'Q':
		q1 += 1
count = 0
q2 = 0
for c in s:
	if c == 'Q':
		q2 += 1
		q1 -= 1
	if c == 'A':
		count += q1 * q2
print(count)


s = str(input())
q1 = list()
a = list()
for i in range(len(s)):
	if s[i] == 'Q':
		q1.append(i)
	elif s[i] == 'A':
		a.append(i)
k = 1
cnt = 0
for i in q1:
	for j in a:
		for k in q1:
			if k > j and k != i and (i < j):
				cnt += 1
print(cnt)


import math
a = list(input())
j = 0
while j < len(a) and a[j] != 'Q':
	j += 1
b = []
c = {'A': 0, 'Q': 0}
for i in range(j, len(a)):
	if a[i] in ['Q', 'A']:
		c[a[i]] += 1
		if len(b) == 0:
			b.append([a[i], 1])
		elif a[i] == b[-1][0]:
			b[-1][1] += 1
		else:
			b.append([a[i], 1])
if len(b) > 0 and b[-1][0] == 'A':
	c['A'] -= b[-1][1]
	b = b[:len(b) - 1]
cnt = 0
qtillnow = 0
for (i, k) in enumerate(b):
	if k[0] == 'A':
		cnt += (b[i - 1][1] + qtillnow) * b[i][1] * (c['Q'] - b[i - 1][1] - qtillnow)
		qtillnow += b[i - 1][1]
print(cnt)


inp = input()
count = 0
for i in range(0, len(inp) - 1):
	if inp[i] == 'Q':
		for j in range(i + 1, len(inp)):
			if inp[j] == 'A':
				count += inp[j + 1::1].count('Q')
print(count)


s = input()
res = 0
for i in range(0, len(s)):
	if s[i] == 'Q':
		for i in range(i + 1, len(s)):
			if s[i] == 'A':
				for i in range(i + 1, len(s)):
					if s[i] == 'Q':
						res += 1
print(res)


s1 = input()
s = []
if 'Q' not in s1:
	print(0)
else:
	for i in s1:
		if i in 'QA':
			s += [i]
	i = 0
	while s[i] == 'A':
		s.remove('A')
	s.reverse()
	i = 0
	while s[i] == 'A':
		s.remove('A')
	SummA = 0
	KolQ = 0
	SummQ = 0
	for i in s:
		if i == 'Q':
			KolQ += 1
			SummQ += SummA
		else:
			SummA += KolQ
	print(SummQ)


s = input()
l = len(s)
ans = 0
for i in range(l):
	if s[i] == 'Q':
		for j in range(i + 1, l):
			if s[j] == 'A':
				for k in range(j + 1, l):
					if s[k] == 'Q':
						ans += 1
print(ans)


s = input()
striped = ''
arr = []
for (i, elem) in enumerate(s):
	if elem == 'Q':
		striped += elem
		arr.append(len(striped) - 1)
	elif elem == 'A':
		striped += elem
if arr == []:
	print(0)
else:
	ans = 0
	n = 0
	for (i, elem) in enumerate(striped):
		if elem == 'A' and i > arr[0]:
			while n < len(arr) - 1:
				if i in range(arr[n], arr[n + 1]):
					ans += len(arr[:n + 1]) * len(arr[n + 1:])
					break
				n += 1
	print(ans)


a = input()
count = 0
for i in range(1, len(a) - 1):
	if a[i] == 'A':
		Lsub = a[0:i].count('Q')
		Rsub = a[i + 1:len(a)].count('Q')
		count += Lsub * Rsub
print(count)


def f():
	ans = 0
	s = input()
	qa = []
	q = []
	qa.append(0)
	if s[0] == 'Q':
		q.append(1)
	else:
		q.append(0)
	for i in range(1, len(s)):
		if s[i] == 'A':
			qa.append(qa[-1] + q[-1])
			q.append(q[-1])
		elif s[i] == 'Q':
			qa.append(qa[-1])
			q.append(q[-1] + 1)
			ans += qa[-1]
	print(ans)
f()


str = list(input())
countQ = str.count('Q')
ans = 0
remQ = countQ
for i in range(0, len(str) - 2):
	if str[i] == 'Q':
		remQ = remQ - 1
		if remQ >= 1:
			remQ1 = remQ
			for j in range(i + 1, len(str)):
				if str[j] == 'A' and remQ1 >= 1:
					ans = ans + remQ1
				elif str[j] == 'Q':
					remQ1 -= 1
print(ans)


t = input()
s = ''
for i in range(len(t)):
	if t[i] == 'Q' or t[i] == 'A':
		s += t[i]
abc = len(s)
cnt = 0
for j in range(abc - 2):
	for k in range(j + 1, abc - 1):
		for p in range(k + 1, abc):
			if s[j] == 'Q' and s[k] == 'A' and (s[p] == 'Q'):
				cnt += 1
print(cnt)


s = input()
count = 0
for c in range(0, len(s)):
	if s[c] == 'Q':
		for l in range(c, len(s)):
			if s[l] == 'A':
				for k in range(l, len(s)):
					if s[k] == 'Q':
						count += 1
print(count)


letterList = list(str(input()))
counter = 0
for i in range(0, len(letterList)):
	if letterList[i] == 'Q':
		for j in range(i, len(letterList)):
			if letterList[j] == 'A':
				for k in range(j, len(letterList)):
					if letterList[k] == 'Q':
						counter += 1
print(counter)


def subseq(s, n):
	cntQ = 0
	cntA = 0
	result = 0
	C = 0
	for i in range(n):
		if s[i] == 'Q':
			cntQ += 1
			result += C
			continue
		if s[i] == 'A':
			cntA += 1
			C += cntQ
			continue
		else:
			continue
	print(result)
s = input()
n = len(s)
subseq(s, n)


s = input()
p = 0
for i in range(len(s)):
	c = 0
	c1 = 0
	if s[i] == 'A':
		for w in range(i):
			if s[w] == 'Q':
				c += 1
		for w in range(i, len(s)):
			if s[w] == 'Q':
				c1 += 1
		p = p + c1 * c
print(p)


s = input()
Q = [0] * len(s)
prefA = [0] * len(s)
if s[0] == 'A':
	prefA[0] = 1
for i in range(1, len(s)):
	if s[i] == 'A':
		prefA[i] = prefA[i - 1] + 1
	else:
		prefA[i] = prefA[i - 1]
ans = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i):
			if s[j] == 'Q':
				ans += prefA[i] - prefA[j]
print(ans)


a = input()
c = 0
for i in range(len(a)):
	if a[i] == 'Q':
		for o in range(i, len(a)):
			if a[o] == 'A':
				for p in range(o, len(a)):
					if a[p] == 'Q':
						c += 1
print(c)


inp = input()
otv = 0
for i in range(len(inp)):
	for j in range(len(inp)):
		for k in range(len(inp)):
			if inp[i] == 'Q' and inp[j] == 'A' and (inp[k] == 'Q') and (i < j < k):
				otv += 1
print(otv)


def solve():
	s = input()
	qaq = []
	for c in s:
		if c == 'Q' or c == 'A':
			qaq.append(c)
	count = 0
	for i in range(len(qaq)):
		if qaq[i] == 'Q':
			for j in range(i + 1, len(qaq)):
				if qaq[j] == 'A':
					for k in range(j + 1, len(qaq)):
						if qaq[k] == 'Q':
							count += 1
	print(count)
solve()


Str = input()
num = 0
for i in range(len(Str) - 2):
	if Str[i] != 'Q':
		continue
	for j in range(i + 1, len(Str) - 1):
		if Str[j] != 'A':
			continue
		for k in range(j + 1, len(Str)):
			if Str[k] != 'Q':
				continue
			num += 1
print(num)


s = ''

def read_input():
	global s
	s = input()

def solve():
	total_count = 0
	for i in range(0, len(s)):
		if s[i] == 'Q':
			for j in range(i + 1, len(s)):
				if s[j] == 'A':
					for k in range(j + 1, len(s)):
						if s[k] == 'Q':
							total_count = total_count + 1
	return total_count
read_input()
print(solve())


s = input()
l = []
k = []
c = 0
for i in s:
	l.append(i)
for i in l:
	if i == 'Q' or i == 'A':
		k.append(i)
n = len(k)
for i in range(n + 1):
	for j in range(i + 1, n):
		for z in range(j + 1, n):
			m = k[i] + k[j] + k[z]
			if m == 'QAQ':
				c += 1
print(c)


a = list(input())
x = len(a)
c = 0
arr = [0]
for i in a:
	if i == 'Q':
		c += 1
	arr.append(c)
c = 0
op = 0
for i in a:
	if i == 'Q':
		c += 1
	if i == 'A':
		op += c * (arr[-1] - c)
print(op)


def solve(w):
	c = 0
	for i in range(len(w)):
		if w[i] == 'Q':
			for j in range(i, len(w)):
				if w[j] == 'A':
					for k in range(j, len(w)):
						if w[k] == 'Q':
							c += 1
	return c

def main():
	w = input()
	print(solve(w))
main()


k = str(input())
a = 0
for i in range(len(k)):
	if k[i] == 'A':
		a += k[:i].count('Q') * k[i:].count('Q')
print(a)


s = input()
n = len(s)
q = [0] * (n + 1)
qa = [0] * (n + 1)
qaq = [0] * (n + 1)
for (i, c) in enumerate(s):
	if c == 'Q':
		q[i + 1] = q[i] + 1
		qaq[i + 1] = qaq[i] + qa[i]
	else:
		q[i + 1] = q[i]
		qaq[i + 1] = qaq[i]
	if c == 'A':
		qa[i + 1] = qa[i] + q[i]
	else:
		qa[i + 1] = qa[i]
print(qaq[n])


kata = str(input())
n = len(kata)
sum = int(0)
for i in range(n):
	if kata[i] == 'A':
		sum += kata.count('Q', 0, i) * kata.count('Q', i + 1, n)
print(sum)


s = input()
n = len(s)
qr = s.count('Q')
ql = 0
ans = 0
for i in range(n):
	if s[i] == 'Q':
		ql += 1
		qr -= 1
	if s[i] == 'A':
		ans += ql * qr
print(ans)


s = input()
sb = 'QAQ'
results = 0
sub_len = len(sb)
for i in range(len(s)):
	for j in range(i + 1, len(s)):
		for k in range(j + 1, len(s)):
			if s[i] + s[j] + s[k] == sb:
				results += 1
print(results)


string = input()
sums_arr = [int(string[0] is 'Q')]
answer = 0
for i in range(1, len(string)):
	sums_arr.append(int(string[i] is 'Q') + sums_arr[i - 1])
for i in range(1, len(string)):
	if string[i] is 'A':
		left = sums_arr[i]
		right = sums_arr[-1] - sums_arr[i]
		answer += left * right
print(answer)


count = 0
s = input()
for i in range(len(s)):
	for j in range(i + 1, len(s)):
		for k in range(j + 1, len(s)):
			if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
				count += 1
print(str(count), end='')


s = input()
r = ''
for i in s:
	if i == 'Q' or i == 'A':
		r += i
r += '.'
r = r.strip('A')
tmp = 1
lis = []
for i in range(1, len(r)):
	if r[i] != r[i - 1]:
		lis.append(tmp)
		tmp = 1
	else:
		tmp += 1
l1 = [lis[i] for i in range(0, len(lis), 2)]
l2 = [lis[i] for i in range(1, len(lis), 2)]
l2.append(0)
if len(l1) == 0 or len(l2) == 0:
	print('0')
	exit()
(QA, Q, res) = (0, 0, 0)
for i in range(len(l1)):
	res += QA * l1[i]
	Q += l1[i]
	QA += Q * l2[i]
print(res)


x = input()
z = 0
a = []
q = []
for i in x:
	if i == 'A':
		a.append(z)
	elif i == 'Q':
		q.append(z)
	z += 1
i = 0
kk = 0
while len(a) > 0 and len(q) > 0 and (i < len(a)):
	j = 0
	k = 0
	flag = 0
	while len(q) > 0 and len(a) > 0 and (j < len(q)):
		if a[i] > q[j]:
			j += 1
			k += 1
		elif a[i] < q[j]:
			flag += 1
			j += 1
		if j == len(q):
			i += 1
			flag = flag * k
			break
	kk = kk + flag
print(kk)


import sys
from collections import defaultdict
import bisect
input = sys.stdin.readline

def insr():
	s = input()
	return list(s[:len(s) - 1])

def solve(s):
	hash_map = defaultdict(list)
	for (i, c) in enumerate(s):
		if c in ['Q', 'A']:
			hash_map[c].append(i)
	count = 0
	for a in hash_map['A']:
		q = hash_map['Q']
		i = bisect.bisect(q, a)
		count += i * (len(q) - i)
	return count
print(solve(insr()))


line = input()
QBefore = []
QAfter = []
for char in line:
	QBefore.append(0)
	QAfter.append(0)
if line[0] == 'Q':
	QBefore[0] = 1
else:
	QBefore[0] = 0
for i in range(1, len(line), 1):
	QBefore[i] = QBefore[i - 1]
	if line[i] == 'Q':
		QBefore[i] += 1
stop = len(line) - 1
if line[stop] == 'Q':
	QAfter[stop] = 1
else:
	QAfter[stop] = 0
for i in range(len(line) - 2, -1, -1):
	QAfter[i] = QAfter[i + 1]
	if line[i] == 'Q':
		QAfter[i] += 1
answer = 0
for i in range(0, len(line)):
	if line[i] == 'A':
		answer += QBefore[i] * QAfter[i]
print(answer)


n = input()
count = 0
for i in range(len(n) - 2):
	if n[i] == 'Q':
		for j in range(i + 1, len(n) - 1):
			if n[j] == 'A':
				for k in range(j + 1, len(n)):
					if n[k] == 'Q':
						count += 1
print(count)


s = input()
if s.count('Q') < 2 or s.count('A') < 1:
	print(0)
else:
	n = len(s)
	ans = 0
	for i in range(n):
		a = 0
		if s[i] == 'Q':
			for j in range(i + 1, n):
				if s[j] == 'A':
					a += 1
				elif s[j] == 'Q':
					ans += a
	if s[-1] == 'Q':
		ans += a
	print(ans)


s = list(input())
n = len(s)
ans = 0
for i in range(n - 2):
	if s[i] != 'Q':
		continue
	for j in range(i + 1, n - 1):
		if s[j] != 'A':
			continue
		for k in range(j + 1, n):
			if s[k] != 'Q':
				continue
			ans += 1
print(ans)


MAX = 100

def countSubsequence(s, n):
	cntQ = 0
	cntA = 0
	result = 0
	C = 0
	for i in range(n):
		if s[i] == 'Q':
			cntQ += 1
			result += C
			continue
		if s[i] == 'A':
			cntA += 1
			C += cntQ
			continue
		else:
			continue
	print(result)
s = input()
n = len(s)
countSubsequence(s, n)


from math import ceil, sqrt, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def li():
	return list(mi())
s = si()
n = len(s)
l = [0] * (n + 1)
r = [0] * (n + 1)
for i in range(1, n + 1):
	if s[i - 1] == 'Q':
		l[i] = l[i - 1] + 1
	else:
		l[i] = l[i - 1]
for i in range(n - 1, -1, -1):
	if s[i] == 'Q':
		r[i] = r[i + 1] + 1
	else:
		r[i] = r[i + 1]
ans = 0
for i in range(n):
	if s[i] == 'A':
		ans += l[i] * r[i + 1]
print(ans)


a = ''
s = input()
for i in s:
	if i == 'Q' or i == 'A':
		a += i
c = 0
for f in range(0, len(a)):
	for a1 in range(f, len(a)):
		for q in range(a1, len(a)):
			if a[f] + a[a1] + a[q] == 'QAQ':
				c += 1
print(c)


strg = input()
lenstrg = len(strg)
if lenstrg <= 2:
	print(0)
else:
	dp = [[0 for j in range(lenstrg)] for i in range(3)]
	dp[0][0] = int(strg[0] == 'Q')
	for i in range(1, lenstrg):
		dp[0][i] = int(strg[i] == 'Q') + dp[0][i - 1]
	for i in range(1, lenstrg):
		dp[1][i] = int(strg[i] == 'A') * dp[0][i - 1] + dp[1][i - 1]
	for i in range(2, lenstrg):
		dp[2][i] = int(strg[i] == 'Q') * dp[1][i - 1] + dp[2][i - 1]
	print(dp[2][-1])


n = list(str(input()))
count = 0
for i in range(len(n)):
	if n[i] == 'Q':
		for j in range(i, len(n)):
			if n[j] == 'A':
				for k in range(j, len(n)):
					if n[k] == 'Q':
						count += 1
print(count)


li = input()
n = len(li)
ans = 0
for i in range(n):
	if li[i] == 'Q':
		for j in range(i + 1, n):
			if li[j] == 'A':
				ans = ans + li.count('Q', j + 1, n)
print(ans)


a = input()
ans = 0
for i in range(len(a)):
	if a[i] == 'Q':
		for j in range(i, len(a)):
			if a[j] == 'A':
				ans += a[j + 1:].count('Q')
print(ans)


word = input()
n = len(word)
ans = 0
if 'Q' in word:
	x = word.index('Q')
	l = word.rindex('Q')
	for i in range(x, l):
		if word[i] == 'Q':
			for j in range(i, n):
				if word[j] == 'A':
					for k in range(j, n):
						if word[k] == 'Q':
							ans += 1
print(ans)


string = input()
(q, qa, qaq) = (0, 0, 0)
for i in string:
	if i == 'A':
		qa += q
	elif i == 'Q':
		qaq += qa
		q += 1
print(qaq)


a = str(input())
ans = 0
for i in range(len(a)):
	if a[i] == 'A':
		left = 0
		right = 0
		for ii in range(i):
			if a[ii] == 'Q':
				left += 1
		for ii in range(i + 1, len(a)):
			if a[ii] == 'Q':
				right += 1
		ans += left * right
print(ans)


import re
c = 0
s = input()
for i in range(len(s)):
	for j in range(i + 1, len(s)):
		for k in range(j + 1, len(s)):
			if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
				c += 1
print(c)


s = input()
n = s.count('Q')
cur = 0
out = 0
for c in s:
	if c == 'Q':
		cur += 1
	if c == 'A':
		out += cur * (n - cur)
print(out)


counts = [1, 0, 0, 0]
s = input()
for c in s:
	if c == 'A':
		counts[2] += counts[1]
	elif c == 'Q':
		counts[1] += counts[0]
		counts[3] += counts[2]
print(counts[3])


def main():
	N = input()
	qaq = []
	apnd = qaq.append
	for s in N:
		if s in 'Q' or s in 'A':
			apnd(s)
	n = len(qaq)
	ans = 0
	for i in range(n - 2):
		for j in range(i + 1, n - 1):
			for k in range(j + 1, n):
				if qaq[i] == qaq[k] == 'Q' and qaq[j] == 'A':
					ans += 1
	print(ans)
main()


s = input()
x = s.find('A')
c = 0
while x != -1:
	c1 = s[:x].count('Q')
	c2 = s[x:].count('Q')
	c += c1 * c2
	x = s.find('A', x + 1)
print(c)


def solve():
	n = list(input())
	q = n.count('Q')
	c = 0
	s = 0
	for i in range(len(n)):
		if n[i] == 'Q':
			c += 1
		elif n[i] == 'A':
			s += c * (q - c)
		else:
			continue
	print(s)
solve()


qaq = input()
q = 0
pocet = [None] * len(qaq)
acka = []
for i in range(len(qaq)):
	if qaq[i] == 'Q':
		q += 1
	elif qaq[i] == 'A':
		acka.append(i)
	pocet[i] = q
suma = 0
for i in range(len(acka)):
	suma += pocet[acka[i]] * (q - pocet[acka[i]])
print(suma)


s = input()
n = len(s)
Q1 = Q2 = count = 0
for i in range(n):
	if s[i] == 'A':
		for j in range(0, i):
			if s[j] == 'Q':
				Q1 += 1
		for k in range(i + 1, n):
			if s[k] == 'Q':
				Q2 += 1
	count += Q1 * Q2
	Q1 = Q2 = 0
print(count)


def f(s):
	(q1, a, q2) = (0, 0, 0)
	for c in s:
		if c == 'Q':
			q2 += a
			q1 += 1
		elif c == 'A':
			a += q1
	return q2
assert f('QAQAQYSYIOIWIN') == 4
assert f('QAQQQZZYNOIWIN') == 3
s = input()
n = f(s)
print(n)


char_list = [char for char in input()]
pattern = [char for char in 'QAQ']
seen_prefix = [0] * len(pattern)
for letter in char_list:
	for p_i in range(0, len(pattern)):
		if pattern[p_i] == letter:
			if p_i == 0:
				seen_prefix[p_i] += 1
			else:
				seen_prefix[p_i] += seen_prefix[p_i - 1]
print(seen_prefix[-1])


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		l = list(input())
		ans = 0
		n = len(l)
		(noa, noq) = (0, 0)
		dp = [0]
		for x in range(n):
			if l[x] == 'Q':
				noq += 1
				temp = dp[-1]
				temp += (noq - 1) * noa
				dp.append(temp)
				noa = 0
			elif l[x] == 'A':
				if noq != 0:
					noa += 1
		print(sum(dp))
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(1):
	main()


word = input()
n = len(word)
prefix1 = []
q = 0
for i in range(n):
	if word[i] == 'Q':
		q += 1
	prefix1.append(q)
q = 0
prefix2 = [0 for i in range(n)]
for i in range(n - 1, -1, -1):
	if word[i] == 'Q':
		q += 1
	prefix2[i] = q
res = 0
for i in range(n):
	if word[i] == 'A':
		res += prefix1[i] * prefix2[i]
print(res)


input_string = input()
limit = len(input_string)
count = 0
for i in range(limit):
	if input_string[i] == 'Q':
		for j in range(i + 1, limit):
			if input_string[j] == 'A':
				for k in range(j + 1, limit):
					if input_string[k] == 'Q':
						count += 1
print(count)


s = str(input())
n = 0
(m, total) = (0, 0)
ans = [0] * len(s)
for i in range(0, len(s)):
	if s[i] == 'Q':
		m += 1
	elif s[i] == 'A':
		total += m
	if i != 0:
		ans[i] = ans[i - 1]
	if s[i] == 'Q':
		ans[i] += total
print(ans[len(s) - 1])


aaftq = []
qafta = []
for i in input():
	if i == 'Q':
		aaftq.append(0)
		for j in range(len(qafta)):
			qafta[j] += 1
	elif i == 'A':
		qafta.append(0)
		for j in range(len(aaftq)):
			aaftq[j] += 1
sm = 0
for i in aaftq:
	for j in range(-i, 0, 1):
		sm += qafta[j]
print(sm)


arr = list(input())
count_ = 0
for i in range(0, len(arr)):
	for j in range(i + 1, len(arr)):
		for k in range(j + 1, len(arr)):
			if arr[i] == 'Q' and arr[j] == 'A' and (arr[k] == 'Q'):
				count_ += 1
			else:
				continue
print(count_)


def check(n, param):
	if param == '':
		return 1
	s = 0
	for (i, v) in enumerate(n):
		if n[i] == param[0]:
			s += check(n[i + 1:], param[1:])
	return s

def main():
	n = input()
	r = check(n, 'QAQ')
	print(r)
main()


a = []
b = []
s = input()
n = len(s)
cc = 0
ans = 0
for i in range(0, n):
	if s[i] == 'Q':
		cc += 1
	a.append(cc)
cc = 0
for i in range(n - 1, -1, -1):
	if s[i] == 'Q':
		cc += 1
	b.append(cc)
b.reverse()
for i in range(0, n):
	if s[i] == 'A':
		ans += a[i] * b[i]
print(ans)


s = input()
k = [0] * 3
for i in range(0, len(s)):
	if s[i] == 'Q':
		k[2] += k[1]
	if s[i] == 'A':
		k[1] += k[0]
	if s[i] == 'Q':
		k[0] += 1
print(k[2])


st = input().strip()
ans = 0
for i in range(len(st)):
	for j in range(i + 1, len(st)):
		for k in range(j + 1, len(st)):
			if st[i] == 'Q' and st[j] == 'A' and (st[k] == 'Q'):
				ans += 1
print(ans)


from sys import stdin

def solve():
	s = str(input())
	if len(s) < 3:
		return 0
	t = 'QAQ'
	dp = [0] * (len(t) + 1)
	dp[0] = 1
	for i in range(len(s)):
		for j in range(len(t)):
			if s[i] == t[j]:
				dp[j + 1] += dp[j]
	return dp[len(t)]
print(solve())


import itertools
n = list(input())
a = []
for i in range(len(n)):
	if n[i] == 'Q' or n[i] == 'A':
		a.append(n[i])
a = list(itertools.combinations(a, 3))
for i in range(len(a)):
	a[i] = ''.join(a[i])
print(a.count('QAQ'))


s = input()
x = s.count('Q')
a = 0
k = 0
for i in s:
	if i == 'Q':
		a += 1
	elif i == 'A':
		k += a * (x - a)
print(k)


s = input()
qCount = 0
aCount = 0
count = []
for i in range(len(s)):
	if s[i] == 'Q':
		qCount += 1
	if qCount > 0 and s[i] == 'A':
		count.append(qCount)
		aCount += 1
count = [i * (qCount - i) for i in count]
print(sum(count))


s = input()
length = len(s)
ans = 0
for i in range(length):
	if s[i] == 'A':
		left = right = 0
		for l in s[:i]:
			if l == 'Q':
				left += 1
		for r in s[i + 1:]:
			if r == 'Q':
				right += 1
		ans += left * right
print(ans)


s = input()
clean = ''
for i in s:
	if i == 'Q' or i == 'A':
		clean += i
res = 0
s = clean
for i in range(len(clean)):
	for j in range(i + 1, len(clean)):
		for k in range(j + 1, len(clean)):
			if s[i] + s[j] + s[k] == 'QAQ':
				res += 1
print(res)


def solve(a, b):
	m = len(a)
	n = len(b)
	tab = [[0] * (n + 1) for i in range(m + 1)]
	for i in range(n + 1):
		tab[0][i] = 0
	for i in range(m + 1):
		tab[i][0] = 1
	for i in range(1, m + 1):
		for j in range(1, n + 1):
			if a[i - 1] == b[j - 1]:
				tab[i][j] = tab[i - 1][j - 1] + tab[i - 1][j]
			else:
				tab[i][j] = tab[i - 1][j]
	return tab[m][n]
a = input()
b = 'QAQ'
print(solve(a, b))


x = input()
y = 0
n = 0
m = 0
for i in range(len(x)):
	if x[i] == 'A':
		a = x[:i]
		b = x[i:]
		for k in a:
			if k == 'Q':
				n += 1
		for k in b:
			if k == 'Q':
				m += 1
		y += n * m
		n = 0
		m = 0
print(y)


import re

def cnt(a, b):
	m = len(a)
	n = len(b)
	lookup = [[0] * (n + 1) for i in range(m + 1)]
	for i in range(n + 1):
		lookup[0][1] = 0
	for i in range(m + 1):
		lookup[i][0] = 1
	for i in range(1, m + 1):
		for j in range(1, n + 1):
			if a[i - 1] == b[j - 1]:
				lookup[i][j] = lookup[i - 1][j - 1] + lookup[i - 1][j]
			else:
				lookup[i][j] = lookup[i - 1][j]
	return lookup[m][n]
s = input()
index = 0
curr = ''
s = re.sub('[^AQ]+', '', s)
print(cnt(s, 'QAQ'))


n = input()
kolvo = 0
for i in range(len(n)):
	for j in range(i + 1, len(n)):
		for y in range(j + 1, len(n)):
			if n[i] + n[j] + n[y] == 'QAQ':
				kolvo += 1
print(kolvo)


s = input()
c = 0
try:
	q = s.index('Q')
	for i in range(q, len(s)):
		if s[i] == 'A':
			k = s[i:].count('Q')
			l = s[:i].count('Q')
			c += l * k
	print(c)
except Exception:
	print(0)


s = input()
sum = 0
for i in range(len(s)):
	if s[i] == 'A':
		l = 0
		r = 0
		s1 = s[:i]
		l = s1.count('Q')
		s2 = s[i:]
		r = s2.count('Q')
		sum += l * r
print(sum)


a = input()
b = 0
for i in range(len(a)):
	if a[i] == 'A':
		b += a[:i].count('Q') * a[i:].count('Q')
print(b)


st = input().strip()
q = [0] * len(st)
a = [0] * len(st)
qa = [0] * len(st)
qaq = [0] * len(st)

def f(k):
	if k == 0:
		if st[0] == 'Q':
			q[0] = 1
		elif st[0] == 'A':
			a[0] = 1
		return
	f(k - 1)
	if st[k] == 'Q':
		q[k] = q[k - 1] + 1
		a[k] = a[k - 1]
		qa[k] = qa[k - 1]
		qaq[k] = qa[k - 1] + qaq[k - 1]
	elif st[k] == 'A':
		q[k] = q[k - 1]
		a[k] = a[k - 1] + 1
		qa[k] = q[k - 1] + qa[k - 1]
		qaq[k] = qaq[k - 1]
	else:
		q[k] = q[k - 1]
		a[k] = a[k - 1]
		qa[k] = qa[k - 1]
		qaq[k] = qaq[k - 1]
f(len(st) - 1)
print(qaq[len(st) - 1])


seq = input()
count = 0
while seq.find('A') != -1:
	order = seq.split('A', 1)
	cur_sub = order[0].count('Q') * order[1].count('Q')
	count = count + cur_sub
	seq = seq.replace('A', '', 1)
print(count)


s = input()
ss = 0
for i in range(0, len(s)):
	if s[i] == 'A':
		be = 0
		for j in range(0, i):
			if s[j] == 'Q':
				be += 1
		af = 0
		for j in range(i + 1, len(s)):
			if s[j] == 'Q':
				af += 1
		ss += be * af
print(ss)


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()
string = input()
substring = 'QAQ'
res = []
count = 0
for i in range(len(string) - 1):
	if string[i] == 'Q':
		for j in range(i + 1, len(string)):
			if string[j] == 'A':
				for k in range(j, len(string)):
					if string[k] == 'Q':
						count += 1
						res.append(string[i] + string[j] + string[k])
print(len(res))


s = list(input())
n = len(s)
s.insert(0, '*')
p = print
(mx, mn, ans) = (-int(1e+18), int(1e+18), 0)
a = [0 for i in range(n + 1)]
for i in range(1, n + 1):
	a[i] = 1 if s[i] == 'Q' else 0
for i in range(1, n + 1):
	a[i] += a[i - 1]
for i in range(1, n + 1):
	if s[i] != 'A':
		continue
	t1 = a[i - 1] - a[0]
	t2 = a[n] - a[i]
	ans += t1 * t2
print(ans)


a = input()
dp = [0] * len(a)
for i in range(len(a)):
	for j in range(0, i):
		if a[j] == 'Q' and a[i] == 'A':
			dp[i] += 1
dp2 = [0] * len(a)
s = 0
for i in range(len(a)):
	for j in range(0, i):
		if dp[j] != 0:
			if a[j] == 'A' and a[i] == 'Q':
				dp2[i] += dp[j]
				s += dp[j]
print(s)


import math
string = input()
n = len(string)
table = [0 for i in range(n)]
count = 0
for i in range(n):
	if string[i] == 'Q':
		count += 1
	table[i] = count
result = 0
total = table[n - 1]
for i in range(n):
	if string[i] == 'A':
		result += table[i] * (total - table[i])
print(result)


S = input()
cpt = 0
for l in range(len(S)):
	if S[l] == 'Q':
		a = 0
		for r in range(l + 1, len(S)):
			if S[r] == 'Q':
				cpt += a
			elif S[r] == 'A':
				a += 1
print(cpt)


a = input()
if len(a) > 2 and 'Q' in a:
	x = 0
	a = a[::-1]
	while a[x] != 'Q':
		x += 1
	a = a[x:]
	x = 0
	for i in range(len(a)):
		if a[i] == 'Q':
			for j in range(i + 1, len(a)):
				if a[j] == 'A':
					for k in range(j + 1, len(a)):
						if a[k] == 'Q':
							x += 1
	print(x)
else:
	print(0)


s = input()
ch_arr = [ch for ch in s if ch in ('Q', 'A')]

def find_frequency(ch_arr, s):
	if not ch_arr or not s:
		return 0
	count = 0
	for (i, ch) in enumerate(ch_arr):
		if len(s) == 1 and ch == s[0]:
			count += 1
		if ch == s[0]:
			count += find_frequency(ch_arr[i + 1:], s[1:])
	return count
print(find_frequency(ch_arr, 'QAQ'))


s = input()
a = [0]
for i in s:
	if i == 'Q':
		a.append(a[-1] + 1)
	else:
		a.append(a[-1])
k = 0
for i in range(len(s)):
	if s[i] == 'A':
		k += a[i + 1] * (a[-1] - a[i + 1])
print(k)


s = input()
before = [0] * len(s)
after = [0] * len(s)
count = 0
for i in range(len(s)):
	if s[i] == 'Q':
		count += 1
	elif s[i] == 'A':
		before[i] = count
count = 0
for i in range(len(s) - 1, -1, -1):
	if s[i] == 'Q':
		count += 1
	elif s[i] == 'A':
		after[i] = count
ans = 0
for i in range(len(s)):
	if s[i] == 'A':
		ans += before[i] * after[i]
print(ans)


s = list(input())
count = 0
for i in range(1, len(s) - 1):
	count += 0 if s[i] != 'A' else s[0:i].count('Q') * s[i + 1:len(s)].count('Q')
print(count)


input = input()
sum = 0
for i in range(0, len(input)):
	if input[i] == 'Q':
		for j in range(i + 1, len(input)):
			if input[j] == 'A':
				for z in range(j + 1, len(input)):
					if input[z] == 'Q':
						sum = sum + 1
print(sum)


s = input()
c = 0
l = len(s)
lst = []
for i in s:
	if i == 'Q' or i == 'A':
		lst.append(i)
le = len(lst)
for i in range(le - 2):
	if lst[i] == 'Q':
		for j in range(i + 1, le - 1):
			if lst[j] == 'A':
				for j in range(j + 1, le):
					if lst[j] == 'Q':
						c = c + 1
print(c)


Str = str(input())
dp = []
total = 0
for Char in Str:
	if Char == 'Q':
		dp.append([-1, -1])
		for i in range(len(dp)):
			if dp[i][0] >= 0:
				dp[i][0] += 1 + dp[i][1]
	if Char == 'A':
		if dp:
			for i in range(len(dp)):
				if dp[i][0] == -1:
					dp[i][0] += 1
				dp[i][1] += 1
for i in range(len(dp)):
	if dp[i][0] != -1:
		total += dp[i][0]
print(total)


s = input()
cou = 0
pre = 0
for i in range(len(s)):
	if s[i] == 'Q':
		pre += 1
	if s[i] == 'A':
		if pre > 0:
			flag_right = 0
			for k in range(i + 1, len(s)):
				if s[k] == 'Q':
					flag_right += 1
			if flag_right > 0:
				cou += pre * flag_right
print(cou)


ss = input()
t = len(ss)
sum = 0
for i in range(t):
	if ss[i] == 'A':
		sum += ss[:i].count('Q') * ss[i:].count('Q')
print(sum)


def qaq(string):
	l = len(string)
	(result, q, aux) = (0, 0, 0)
	for i in range(l):
		if string[i] == 'Q':
			q += 1
	for i in range(l):
		if string[i] == 'Q':
			aux += 1
		if string[i] == 'A':
			result += aux * (q - aux)
	return result
n = str(input())
print(qaq(n))


num_qs = 0
num_qas = 0
num_qaqs = 0
text = input()
for character in text:
	if character == 'Q':
		num_qaqs += num_qas
		num_qs += 1
	elif character == 'A':
		num_qas += num_qs
print(num_qaqs)


a = input()
b = ''
for i in range(len(a)):
	if a[i] == 'Q' or a[i] == 'A':
		b += a[i]
count = 0
arr = []
for i in range(len(b)):
	if b[i] == 'Q' and b[i] != 'A':
		count += 1
	else:
		arr.append(count)
		count = 0
		continue
arr.append(count)
count = 0
for i in range(1, len(arr), 1):
	count += sum(arr[0:i]) * sum(arr[i:])
print(count)


st = input()
n = len(st)
prefix = [0 for i in range(n)]
postfix = [0 for i in range(n)]
count = 0
for i in range(n):
	if st[i] == 'Q':
		count += 1
	prefix[i] = count
count = 0
for i in range(n - 1, -1, -1):
	if st[i] == 'Q':
		count += 1
	postfix[i] = count
ans = 0
for i in range(n):
	if st[i] == 'A':
		a = prefix[i]
		b = postfix[i]
		ans += a * b
print(ans)


s = input()
n = len(s)
cnt = 0
i = 0
while i < n - 2:
	while i < n - 2 and s[i] != 'Q':
		i += 1
	j = i + 1
	while j < n - 1:
		while j < n - 1 and s[j] != 'A':
			j += 1
		if j < n - 1:
			cnt += s[j:].count('Q')
		j += 1
	i += 1
print(cnt)


s = input()
work = ''
for i in range(len(s)):
	if s[i] in 'QA':
		work += s[i]
dp = [0] * len(work)
for i in range(len(work)):
	try:
		if work[i] == 'A':
			dp[i] = work[:i].count('Q') + max([dp[j] for j in range(0, i) if work[j] == 'A'])
		elif work[i] == 'Q':
			dp[i] = dp[i - 1]
	except ValueError:
		if work[i] == 'A':
			dp[i] = work[:i].count('Q')
		elif work[i] == 'Q':
			dp[i] = dp[i - 1]
print(sum([dp[i] for i in range(len(dp)) if work[i] == 'Q']))


n = input()
p = 0
for i in range(len(n)):
	for j in range(i + 1, len(n)):
		for k in range(j + 1, len(n)):
			if n[i] + n[j] + n[k] == 'QAQ':
				p += 1
print(p)


s = input()
c = 0
g = []
for i in range(len(s)):
	if s[i] == 'Q' or s[i] == 'A':
		g.append(s[i])
for i in range(len(g)):
	if g[i] == 'Q':
		for j in range(i):
			if g[j] == 'Q':
				for k in range(j, i):
					if g[k] == 'A':
						c += 1
print(c)


n = input()
cnt = 0
for i in range(len(n)):
	for j in range(i + 1, len(n)):
		for k in range(j + 1, len(n)):
			if n[i] == 'Q' and n[j] == 'A' and (n[k] == 'Q'):
				cnt += 1
print(cnt)


seq = input()
qaq = 0
for (i, s) in enumerate(seq):
	if s == 'Q':
		a = 0
		for e in seq[i + 1:]:
			if e == 'A':
				a += 1
			elif e == 'Q':
				qaq += a
print(qaq)


s = list(input())
t = len(s)
ans = 0
for i in range(t):
	if s[i] == 'Q':
		for j in range(i + 1, t):
			if s[j] == 'A':
				for k in range(j + 1, t):
					if s[k] == 'Q':
						ans = ans + 1
print(ans)


x = input()
ans = []
for i in range(len(x)):
	if x[i] == 'Q' or x[i] == 'A':
		ans.append(x[i])
q = ans.count('Q')
a = ans.count('A')
sum = 0
co = 0
for j in range(len(ans)):
	if ans[j] == 'Q':
		co += 1
	else:
		sum += co * (q - co)
print(sum)


def main():
	from sys import stdin, stdout
	s = stdin.read()
	tot_q_cnt = s.count('Q')
	curr_q_cnt = 0
	ans = 0
	for x in s:
		if x == 'A':
			ans += curr_q_cnt * (tot_q_cnt - curr_q_cnt)
		elif x == 'Q':
			curr_q_cnt += 1
	stdout.write(str(ans) + '\n')
main()


l = input()
q = 0
a = 0
ctr = 0
for i in range(len(l)):
	if l[i] == 'Q':
		for j in range(i + 1, len(l)):
			if l[j] == 'A':
				for k in range(j + 1, len(l)):
					if l[k] == 'Q':
						ctr += 1
print(ctr)


s = str(input())
d = [0]
for i in range(len(s)):
	if s[i] == 'Q':
		d.append(d[i] + 1)
	else:
		d.append(d[i])
k = d[-1]
answer = 0
for i in range(1, len(s) - 1):
	if s[i] == 'A':
		answer += d[i] * (k - d[i])
print(answer)


def firstDP(mainString, string, i, n):
	if i == len(mainString):
		if string == 'QAQ':
			return 1
		else:
			return 0
	if n == 3:
		if string == 'QAQ':
			return 1
		else:
			return 0
	noOfSubsequences = firstDP(mainString, string + mainString[i], i + 1, n + 1) + firstDP(mainString, string, i + 1, n)
	return noOfSubsequences
s = input()
print(firstDP(s, '', 0, 0))


def subseq(s, p, ind):
	res = 0
	for i in range(ind, len(s)):
		if s[i] == p[0]:
			if len(p) == 1:
				res += 1
			else:
				res += subseq(s, p[1:], i + 1)
	return res
print(subseq(input(), 'QAQ', 0))


def solve(s1, s2, m, n, d):
	if n == 0:
		return 1
	if m == 0:
		return 0
	if (m, n) in d:
		return d[m, n]
	if s1[m - 1] == s2[n - 1]:
		temp = solve(s1, s2, m - 1, n - 1, d) + solve(s1, s2, m - 1, n, d)
	else:
		temp = solve(s1, s2, m - 1, n, d)
	d[m, n] = temp
	return temp
s1 = input()
s2 = 'QAQ'
d = {}
print(solve(s1, s2, len(s1), 3, d))


s = input()
col = 0
for i in range(len(s)):
	if s[i] == 'A':
		l = 0
		r = 0
		for j in range(i):
			if s[j] == 'Q':
				l += 1
		for j in range(i + 1, len(s)):
			if s[j] == 'Q':
				r += 1
		col += r * l
print(col)


from itertools import combinations
s = input()
l = list(combinations(s, 3))
res = 0
for i in l:
	if ''.join(i) == 'QAQ':
		res += 1
print(res)


s = input()
b = []
if 'A' in s and 'Q' in s:
	k = s.index('A')
	b.append(s.index('A'))
	for i in s:
		try:
			b.append(s.index('A', k + 1))
			k = s.index('A', k + 1)
		except:
			break
	tot = 0
	for i in b:
		tot += s[:i].count('Q') * s[i:].count('Q')
	print(tot)
else:
	print('0')


s = input()
s = list(s)
i = 0
j = 0
k = 0
count = 0
while i < len(s):
	j = i + 1
	if s[i] == 'Q':
		while j < len(s):
			if s[j] == 'A':
				k = j + 1
				while k < len(s):
					if s[k] == 'Q':
						count += 1
					k += 1
			j += 1
	i += 1
print(count)


s = input().strip()
answer = 0
new_s = ''
for c in s:
	if c in 'AQ':
		new_s += c
s = new_s
for i in range(len(s)):
	if s[i] == 'A':
		val1 = 0
		val2 = 0
		for i in range(0, i):
			if s[i] == 'Q':
				val1 += 1
		for i in range(i + 1, len(s)):
			if s[i] == 'Q':
				val2 += 1
		answer += val1 * val2
print(answer)


import sys
import os
from math import sqrt, log, log2, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial
from copy import copy, deepcopy
from sys import exit, stdin, stdout
from collections import Counter, defaultdict, deque
from itertools import permutations
import heapq
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = 1000000007
iinp = lambda : int(sys.stdin.readline())
inp = lambda : sys.stdin.readline().strip()
strl = lambda : list(inp().strip().split(' '))
intl = lambda : list(map(int, inp().split(' ')))
mint = lambda : map(int, inp().split())
flol = lambda : list(map(float, inp().split(' ')))
flush = lambda : stdout.flush()

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	p = int(sqrt(n))
	for i in range(5, p + 1, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def solve():
	x = inp()
	pre = []
	suf = []
	cur = 0
	s = ''
	for i in x:
		if i == 'Q' or i == 'A' or i == 'Q':
			s += i
	for i in range(len(s)):
		if s[i] == 'Q':
			cur += 1
		pre.append(cur)
	cur = 0
	suf = [0] * len(pre)
	for i in range(len(s) - 1, -1, -1):
		if s[i] == 'Q':
			cur += 1
		suf[i] = cur
	ans = 0
	for i in range(1, len(s) - 1):
		if s[i] == 'A':
			ans += pre[i - 1] * suf[i + 1]
	print(ans)
solve()


import sys
from collections import *
from math import *
import re

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	c = 0
	for i in range(2, n):
		if prime[i]:
			c += 1
	return c

def iseven(n):
	return [False, True][0 if n % 2 else 1]

def inp_arr():
	return list(map(int, input().split()))

def inp():
	return map(int, input().split())

def lcm(a, b):
	return a * b / gcd(a, b)
max_int = sys.maxsize
mod = 10 ** 9 + 7
s = input()
res = 0
for i in range(len(s)):
	for j in range(i + 1, len(s)):
		for k in range(j + 1, len(s)):
			if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
				res += 1
print(res)


word = str(input())
qaqs = 0
for lol1 in range(len(word)):
	for lol2 in range(lol1 + 1, len(word)):
		for lol3 in range(lol2 + 1, len(word)):
			if word[lol1] == 'Q' and word[lol2] == 'A' and (word[lol3] == 'Q'):
				qaqs += 1
print(qaqs)


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
l = z()
n = len(l)
ans = 0
p = 0
try:
	p = l.index('Q')
except:
	pass
for i in range(p, n):
	if l[i] == 'A':
		ans += l[:i].count('Q') * l[i + 1:].count('Q')
print(ans)


def main():
	from re import sub
	s = sub('[^AQ]', '', input())
	length = len(s)
	count = 0
	for i in range(length - 2):
		if s[i] == 'Q':
			for j in range(i + 1, length - 1):
				if s[j] == 'A':
					count += s[j + 1:].count('Q')
	print(count)
main()


s = str(input())
arr1 = []
arr2 = [0] * len(s)
count1 = 0
for i in s:
	if i == 'Q':
		count1 += 1
	arr1.append(count1)
count1 = 0
for i in range(len(s) - 1, -1, -1):
	if s[i] == 'Q':
		count1 += 1
	arr2[i] = count1
ans = 0
for i in range(len(s)):
	if s[i] == 'A':
		ans += arr1[i] * arr2[i]
print(ans)


s = input()
(t, c) = (0, 0)
pos = 0
while t < len(s):
	pos = s.find('A', t, len(s))
	if pos == -1:
		break
	else:
		t = pos + 1
		k = s.count('Q', 0, t)
		l = s.count('Q', t, len(s))
		c = c + l * k
print(c)


ss = input()
cnt = 0
n1 = len(ss)
for i in range(n1):
	if ss[i] == 'Q':
		for j in range(i, n1):
			if ss[j] == 'A':
				for k in range(j, n1):
					if ss[k] == 'Q':
						cnt += 1
print(cnt)


s = input()
r = ''
for i in s:
	if i in 'QA':
		r += i
while len(r) and r[0] != 'Q':
	r = r[1:]
while len(r) and r[-1] != 'Q':
	r = r[:-1]
res = 0
for i in range(len(r)):
	if r[i] == 'A':
		res += r[:i].count('Q') * r[i + 1:].count('Q')
print(res)


R = lambda : map(int, input().split())
s = input()
(f, b) = ([0] * len(s), [0] * len(s))
for (i, c) in enumerate(s):
	f[i] = f[i - 1] + (1 if c == 'Q' else 0)
for (i, c) in enumerate(s[::-1]):
	b[i] = b[i - 1] + (1 if c == 'Q' else 0)
b = b[::-1]
res = 0
for i in range(1, len(s) - 1):
	res += f[i - 1] * b[i + 1] if s[i] == 'A' else 0
print(res)


str1 = input()
count = 0
for i in range(len(str1) - 2):
	for j in range(i + 1, len(str1) - 1):
		for k in range(j + 1, len(str1)):
			if str1[i] == 'Q' and str1[j] == 'A' and (str1[k] == 'Q'):
				count += 1
print(count)


st = input()
n = len(st)
dp = [0 for i in range(n)]
for i in range(n):
	if st[i] == 'Q':
		for j in range(i - 1, -1, -1):
			if st[j] == 'A':
				for k in range(j - 1, -1, -1):
					if st[k] == 'Q':
						dp[i] += 1
print(sum(dp))


import sys
import math
import time
bf = [0]
af = [0]
st = input()
for c in st:
	bf.append(bf[-1] + (1 if c == 'Q' else 0))
for c in reversed(st):
	af.append(af[-1] + (1 if c == 'Q' else 0))
af = af[::-1]
ans = 0
for (n, c) in enumerate(st):
	if c == 'A':
		ans += bf[n] * af[n]
print(ans)


from collections import Counter
import math
import itertools
import random
import sys
sys.setrecursionlimit(100000)

def nq(s, next, num, f):
	l = len(s)
	for i in range(l):
		if s[i] == next:
			if num == 2:
				f += 1
			else:
				f = nq(s[i + 1:], 'Q', 2, f)
	return f

def main():
	s = input()
	ans2 = 0
	for j in range(len(s)):
		if s[j] == 'Q':
			ans2 += nq(s[j + 1:], 'A', 1, 0)
	print(ans2)
main()


s = input()
c = 0
S = ''
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i + 1, len(s)):
			if s[j] == 'A':
				for k in range(j + 1, len(s)):
					if s[k] == 'Q':
						c += 1
print(c)


string = input()
count = 0
i = 0
while i < len(string) - 2:
	if string[i] == 'Q':
		j = i + 1
		while j < len(string) - 1:
			if string[j] == 'A':
				k = j + 1
				while k < len(string):
					if string[k] == 'Q':
						count += 1
					k += 1
			j += 1
	i += 1
print(count)


import math
from sys import stdin, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque
setrecursionlimit(10 ** 7)
inf = float('inf')

def LI():
	return list(map(int, stdin.readline().split()))

def LF():
	return list(map(float, stdin.readline().split()))

def LS():
	return stdin.readline().split()

def ONE_IL():
	return list(map(int, list(stdin.readline())))

def ONE_SL():
	return list(stdin.readline())

def I():
	return int(stdin.readline())

def F():
	return float(stdin.readline())

def S():
	return stdin.readline()

def ALI():
	return list(map(int, stdin))

def ALS():
	return stdin

def main():
	s = [i for i in S() if i == 'A' or i == 'Q']
	len_s = len(s)
	ans = 0
	for i in range(len_s - 2):
		for j in range(i + 1, len_s - 1):
			for k in range(j + 1, len_s):
				qaq = s[i] + s[j] + s[k] == 'QAQ'
				if qaq:
					ans += 1
	print(ans)
main()


def until_this(a, x):
	c = 0
	for i in a:
		if i < x:
			c += 1
		else:
			break
	return c
s = input().strip()
q = []
a = []
count = 0
for i in range(len(s)):
	if s[i] == 'Q':
		q.append(i)
	elif s[i] == 'A':
		a.append(i)
for i in a:
	now = until_this(q, i)
	rest = len(q) - now
	count += now * rest
print(count)


TARGET = 'QAQ'

def SeqNum(s, countCalls):
	if countCalls < 3:
		answer = 0
		for (idx, char) in enumerate(s):
			if char == TARGET[countCalls]:
				answer += SeqNum(s[idx + 1:], countCalls + 1)
	else:
		answer = 1
	return answer
print(SeqNum(input(), 0))


def solve(s):
	count = 0
	n = len(s)
	for i in range(n - 2):
		if s[i] == 'Q':
			for j in range(i + 1, n - 1):
				if s[j] == 'A':
					for k in range(j + 1, n):
						if s[k] == 'Q':
							count += 1
	return count
s = input()
print(solve(s))


def crying(s):
	ans = 0
	for i in range(len(s)):
		if s[i] != 'Q':
			continue
		for j in range(i + 1, len(s)):
			if s[j] != 'A':
				continue
			for k in range(j + 1, len(s)):
				if s[k] == 'Q':
					ans += 1
	return ans
s = input()
print(crying(s))


Q_num = A_num = 0
num = []
for letter in input():
	if letter == 'Q':
		Q_num += 1
		if A_num and num:
			num.append(A_num)
			A_num = 0
	if letter == 'A':
		A_num += 1
		if Q_num:
			num.append(Q_num)
			Q_num = 0
		elif not num:
			A_num = 0
if Q_num:
	num.append(Q_num)
print(sum((num[2 * i + 1] * sum(num[:2 * i + 1:2]) * sum(num[2 * i + 2::2]) for i in range((len(num) - 1) // 2))))


s = input()
dp = [0 for x in range(len(s))]
ss = 0
qc = 0
for c in range(len(dp) - 1, -1, -1):
	if s[c] == 'A':
		if qc > dp[c]:
			dp[c] = qc
	if s[c] == 'Q':
		qc += 1
qc = 0
for c in range(len(dp)):
	if s[c] == 'A':
		ss += qc * dp[c]
	if s[c] == 'Q':
		qc += 1
print(ss)


chaine = input()
n = 0
for q1 in range(len(chaine)):
	if chaine[q1] == 'Q':
		for a in range(q1 + 1, len(chaine)):
			if chaine[a] == 'A':
				for q2 in range(a + 1, len(chaine)):
					if chaine[q2] == 'Q':
						n += 1
print(n)


A = input()
n = len(A)
ans = 0
for i in range(n - 2):
	if A[i] == 'Q':
		for j in range(i + 1, n - 1):
			if A[j] == 'A':
				for m in range(j + 1, n):
					if A[m] == 'Q':
						ans += 1
print(ans)


string = input()
length = len(string)
string2 = ''
arrQ = []
arrA = []
for i in range(length):
	if string[i] == 'Q':
		string2 += string[i]
		arrQ.append(i)
	if string[i] == 'A':
		string2 += string[i]
		arrA.append(i)
rs = 0
for i in arrA:
	truoc = 0
	sau = 0
	for j in arrQ:
		if j < i:
			truoc += 1
		else:
			sau += 1
	rs += truoc * sau
print(rs)


s = input().strip()
c = 'QAQ'
count = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i + 1, len(s)):
			if s[j] == 'A':
				for k in range(j + 1, len(s)):
					if s[k] == 'Q':
						count += 1
print(count)


s = input()
q = [int(i == 'Q') for i in s]
a = [int(i == 'A') for i in s]
res = 0
for i in range(len(a)):
	if a[i]:
		res += sum(q[:i]) * sum(q[i:])
print(res)


n = input()
a = []
b = []
for i in range(len(n)):
	if n[i] == 'Q':
		a.append(i)
	if n[i] == 'A':
		b.append(i)
output = 0
for i in b:
	p = []
	q = []
	for j in a:
		if j < i:
			p.append(j)
		else:
			q.append(j)
	output += len(p) * len(q)
print(output)


a = input()
ans = 0
n = len(a)
for i in range(n):
	for j in range(i + 1, n):
		for k in range(j + 1, n):
			if a[i] + a[j] + a[k] == 'QAQ':
				ans += 1
print(ans)


t = input()
s = [x for x in t if x in ['Q', 'A']]
if len(s) > 0:
	l = [1 if x == 'Q' else 0 for x in s]
	pre = [l[0]]
	for i in range(1, len(l)):
		pre.append(pre[-1] + l[i])
	rev = l[::-1]
	post = [rev[0]]
	for i in range(1, len(rev)):
		post.append(post[-1] + rev[i])
	post = post[::-1]
	ans = 0
	for i in range(len(s)):
		if s[i] == 'A':
			ans = ans + pre[i] * post[i]
	print(ans)
else:
	print(0)


n = str(input())
lstN = []
lenLstN = 0
for i in range(0, len(n)):
	if n[i] == 'Q' or n[i] == 'A':
		lstN += n[i]
		lenLstN += 1
strings = ''.join((str(y) for y in lstN))
countA = lstN.count('A')
soQAQ = 0
for x in range(0, lenLstN):
	if strings[x] == 'A':
		stringstemp = strings.split('A', 1)
		soQTrai = stringstemp[0].count('Q')
		soQphai = stringstemp[1].count('Q')
		stringsSau = strings.replace('A', '-', 1)
		strings = stringsSau
		soQAQ += soQphai * soQTrai
print(soQAQ)


z = [x for x in input() if x in 'QA']
sum = 0
for i in range(len(z)):
	if z[i] == 'A':
		sum += z[:i].count('Q') * z[i:].count('Q')
print(sum)


a = input()
stripped = ''.join((i for i in a if i in 'AQ'))
c = 0
for i in range(len(stripped)):
	for j in range(i, len(stripped)):
		for k in range(j, len(stripped)):
			c += stripped[i] + stripped[j] + stripped[k] == 'QAQ'
print(c)


a = input()
v = []
v1 = []
f = []
f1 = []
d = a.count('Q')
for i in range(len(a)):
	if a[i] == 'Q':
		v.append(i)
	if a[i] == 'A':
		f.append(i)
ans = 0
for i in range(len(f)):
	ans1 = 0
	ans2 = 0
	for j in range(len(v)):
		if f[i] > v[j]:
			ans1 += 1
		else:
			ans2 += 1
	ans += ans2 * ans1
print(ans)


text = input()
q = []
a = []
c = 0
for i in range(len(text)):
	if text[i] == 'Q':
		q.append(i)
	if text[i] == 'A':
		a.append(i)
for x in range(len(q)):
	for y in range(len(a)):
		if q[x] < a[y]:
			for z in range(x + 1, len(q)):
				if q[z] > a[y]:
					c += len(q) - z
					break
print(c)


z = input()
c_q = 0
all_c_q = z.count('Q')
t = 0
for i in range(len(z)):
	if z[i] == 'Q':
		c_q += 1
		all_c_q -= 1
	if z[i] == 'A':
		t += c_q * all_c_q
print(t)


def countQAQ(s):
	cnt = 0
	q = 0
	qa = 0
	for c in s:
		if c == 'Q':
			q += 1
			cnt += qa
		elif c == 'A':
			qa += q
	return cnt
s = input()
print(countQAQ(s))


string = input()
total = 0
for i in range(len(string)):
	if string[i] == 'A':
		total += string[:i].count('Q') * string[i:].count('Q')
print(total)


s = input()
totalQ = s.count('Q')
countQ = 0
ans = 0
for i in s:
	if i == 'Q':
		countQ += 1
	elif i == 'A':
		ans += countQ * (totalQ - countQ)
print(ans)


s = input()
count = 0
for i in range(len(s)):
	if s[i] == 'Q':
		while 'A' in s[i + 1:]:
			j = i + 1 + s[i + 1:].find('A')
			q = s[j + 1:].count('Q')
			count = count + q
			i = j
print(count)


s = input()
total = 0
count = 0
k = s.count('Q')
for i in s:
	if i == 'Q':
		count += 1
	elif i == 'A':
		total += count * (k - count)
print(total)


s = input()
ans = 0
pre = []
if s[0] == 'Q':
	pre.append(1)
else:
	pre.append(0)
for i in range(1, len(s)):
	if s[i] == 'Q':
		pre.append(pre[i - 1] + 1)
	else:
		pre.append(pre[i - 1])
for i in range(len(s)):
	if s[i] == 'A':
		ans += pre[i] * (pre[len(s) - 1] - pre[i])
print(ans)


n = input()
ans = 0
a = []
q = []
for i in range(len(n)):
	if n[i] == 'Q':
		q.append(i)
	if n[i] == 'A':
		a.append(i)
for i in range(len(a)):
	t = 0
	for j in range(len(q)):
		if q[j] < a[i]:
			t += 1
		else:
			ans += t * (len(q) - t)
			break
print(ans)


import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, gcd, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

def main(s):
	qa = ''
	for i in s:
		if i == 'Q' or i == 'A':
			qa += i
	n = len(qa)
	if n <= 2:
		return 0
	left = [0] * n
	for i in range(1, n):
		left[i] = left[i - 1]
		if qa[i - 1] == 'Q':
			left[i] += 1
	right = [0] * n
	for i in reversed(range(n - 1)):
		right[i] = right[i + 1]
		if qa[i + 1] == 'Q':
			right[i] += 1
	ans = 0
	for i in range(n):
		if qa[i] == 'A':
			ans += left[i] * right[i]
	return ans
s = input()
print(main(s))


from bisect import bisect_left as bs
s = input().strip()
dic = {'Q': [], 'A': []}
for i in range(len(s)):
	if s[i] == 'Q':
		dic['Q'].append(i)
	elif s[i] == 'A':
		dic['A'].append(i)
l1 = dic['Q']
l2 = dic['A']
ans = 0
for i in range(len(l1)):
	for j in range(i + 1, len(l1)):
		(l, r) = (l1[i], l1[j])
		ipl = bs(l2, l, 0, len(l2))
		ipr = bs(l2, r, 0, len(l2))
		ans += ipr - ipl
print(ans)


s = input()
n = len(s)
l = []
c1 = 0
c2 = s.count('Q')
for i in range(n):
	if s[i] == 'Q':
		c1 += 1
		c2 -= 1
	elif s[i] == 'A':
		l.append((c1, c2))
s1 = 0
for i in l:
	s1 += i[0] * i[1]
print(s1)


def calcUtil(s, sub):
	calcUtil.times += 1
	n = len(s)
	M = [[0 for i in range(4)] for j in range(n + 1)]
	for i in range(n + 1):
		M[i][0] = 1
	for i in range(1, 4):
		for j in range(1, n + 1):
			if s[j - 1] == sub[i - 1]:
				M[j][i] = M[j - 1][i] + M[j - 1][i - 1]
			else:
				M[j][i] = M[j - 1][i]
	return M[n][3]

def calc(s):
	sub = 'QAQ'
	calcUtil.times = 0
	val = calcUtil(s, sub)
	return val
s = input()
print(calc(s))


t = input()
s = ''
for i in range(len(t)):
	if t[i] == 'Q' or t[i] == 'A':
		s = s + t[i]
tab = [1]
v = s.find('Q')
u = []
comp = 1
for i in range(v + 1, len(s)):
	if s[i] == s[i - 1]:
		comp = comp + 1
		tab.append(comp)
	else:
		u.append(comp)
		comp = 1
		tab.append(comp)
if i < len(s):
	if s[i] == 'Q':
		u.append(comp)

def g(l, i):
	s = 0
	for j in range(i, len(l), 2):
		s = s + l[j]
	return s

def f(l):
	s = 0
	for i in range(0, len(l) - 1, 2):
		for j in range(i + 1, len(l) - 1, 2):
			s = s + l[i] * l[j] * g(l, j + 1)
	return s
print(f(u))


s = input()
l_q = [0 for _ in range(len(s))]
for (i, c) in enumerate(s):
	if c == 'Q':
		l_q[i] += 1
	l_q[i] += 0 if i == 0 else l_q[i - 1]
t = 0
for (i, c) in enumerate(s):
	if c == 'A' and i > 0:
		t += (l_q[len(l_q) - 1] - l_q[i - 1]) * l_q[i - 1]
print(t)


x = input()
q = 'Q'
a = 'A'
count = 0
for i in range(len(x)):
	if x[i] == q:
		if a in x[i + 1:]:
			temp_val = x[i + 1:].count(a)
			temp_arr = list(x[i + 1:])
			for j in range(temp_val):
				idx = temp_arr.index(a)
				if q in temp_arr[idx + 1:]:
					count += temp_arr[idx + 1:].count(q)
				temp_arr.remove(a)
print(count)


line = input()
sum = 0
for i in range(0, len(line)):
	if line[i] == 'Q':
		for j in range(i, len(line)):
			if line[j] == 'A':
				for k in range(j, len(line)):
					if line[k] == 'Q':
						sum += 1
print(sum)


string = str(input())
substring = 'QAQ'
stringLen = len(string)
subStringLen = len(substring)
dp = [0 for i in range(subStringLen + 1)]
dp[0] = 1
for i in range(stringLen):
	for j in reversed(range(subStringLen)):
		if substring[j] == string[i]:
			dp[j + 1] = dp[j + 1] + dp[j]
print(dp[subStringLen])


from sys import stdin, stdout, setrecursionlimit
import heapq
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007
s = is1()
dp = [0] * len(s)
for i in range(len(s)):
	if s[i] == 'A':
		dp[i] = s[:i].count('Q') * s[i + 1:].count('Q')
print(sum(dp))


s = input()
n = len(s)
freq = [0 for _ in range(n)]
count = 0
ans = 0
for (index, char) in enumerate(s):
	if char == 'A':
		count += 1
	freq[index] = count
for (index, char) in enumerate(s):
	if char == 'Q':
		for j in range(index + 1, n):
			if s[j] == 'Q':
				ans += freq[j] - freq[index]
print(ans)


s = input()
sum = 0
for i in range(s.__len__()):
	for j in range(i + 1, s.__len__()):
		for k in range(j + 1, s.__len__()):
			if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
				sum += 1
print(sum)


s = input()
target = 'QAQ'
dp = [[0 for j in range(len(s))] for i in range(3)]
for i in range(3):
	for j in range(len(s)):
		if s[j] == target[i]:
			if i == 0:
				dp[i][j] = 1
			else:
				dp[i][j] = sum(dp[i - 1][:j])
print(sum(dp[2]))


import sys
input = sys.stdin.readline

def list_input():
	return list(map(int, input().split()))

def float_compare(a, b):
	if abs(a - b) < float(1e-09):
		return True
	else:
		return False

def sub_mod(x, mod):
	x = x % mod
	if x < 0:
		x += mod
	return x

def divisor(n):
	i = 1
	dv = []
	while i * i <= n:
		if n % i == 0:
			dv.append(i)
			if i * i != n:
				dv.append(n // i)
		i += 1
	return dv
s = input().strip()
l = list(s)
for i in range(len(l)):
	if l[i] != 'Q' and l[i] != 'A':
		l[i] = '$'
while '$' in l:
	l.remove('$')
while len(l) != 0 and l[0] == 'A':
	l.pop(0)
while len(l) != 0 and l[-1] == 'A':
	l.pop(-1)
count = 0
for i in range(len(l)):
	if l[i] == 'A':
		(q1, q2) = (l[0:i].count('Q'), l[i:].count('Q'))
		count += q1 * q2
print(count)


a = input()
b = []
for i in range(100):
	b.append([0, 0])
c = 0
pre = 0
post = 0
t = 0
for i in range(len(a)):
	if a[i] == 'Q':
		c = 1
		pre += 1
		for j in range(t):
			b[j][1] += 1
	if c:
		if a[i] == 'A':
			b[t][0] = pre
			t += 1
ans = 0
for i in range(100):
	ans += b[i][0] * b[i][1]
	if b[i][0] == 0 and b[i][1] == 0:
		break
print(ans)


s = input()
print(sum((s[:i].count('Q') * s[i:].count('Q') for i in range(len(s)) if s[i] == 'A')))


n = input()
n1 = len(n)
count = 0
for i in range(0, n1, 1):
	for j in range(i + 1, n1, 1):
		for k in range(j + 1, n1, 1):
			if n[i] == 'Q' and n[j] == 'A' and (n[k] == 'Q'):
				count += 1
print(count)


s = str(input())
c = q = 0
qt = s.count('Q')
for i in range(len(s)):
	l = s[i]
	if l == 'Q':
		q += 1
	elif l == 'A':
		c += q * (qt - q)
print(c)


s = input()
(Qs, As) = ([], [])
for (i, j) in enumerate(s):
	if j == 'Q':
		Qs.append(i)
	elif j == 'A':
		As.append(i)
ans = 0
for i in Qs:
	for j in As:
		if j > i:
			for k in Qs:
				if k > j:
					ans += 1
print(ans)


s = input()
print([s[i] + s[j] + s[k] for i in range(len(s)) for j in range(i) for k in range(j)].count('QAQ'))


a = input()
aa = []
ab = []
q1 = 0
q = 0
for i in range(len(a)):
	if a[i] == 'Q':
		q += 1
	elif a[i] == 'A':
		aa.append(q)
	if a[(i + 1) * -1] == 'Q':
		q1 += 1
	elif a[(i + 1) * -1] == 'A':
		ab.append(q1)
f = 0
for i in range(len(aa)):
	f = f + aa[i] * ab[(i + 1) * -1]
print(f)


s = input()
cnt = 0
for i in range(0, len(s)):
	for j in range(i + 1, len(s)):
		for k in range(j + 1, len(s)):
			sr = s[i] + s[j] + s[k]
			if sr == 'QAQ':
				cnt += 1
print(cnt)


s = input()
c = 0
for i1 in range(len(s) - 2):
	if s[i1] != 'Q':
		continue
	for i2 in range(i1 + 1, len(s) - 1):
		if s[i2] != 'A':
			continue
		for i3 in range(i2 + 1, len(s)):
			if s[i3] == 'Q':
				c += 1
print(c)


s1 = input()
s = ''
ans = 0
for c in s1:
	if c == 'A' or c == 'Q':
		s += c
for i in range(len(s)):
	if s[i] == 'A':
		(q_before, q_after) = (0, 0)
		for c in s[0:i]:
			if c == 'Q':
				q_before += 1
		for c in s[i:len(s)]:
			if c == 'Q':
				q_after += 1
		ans += q_before * q_after
print(ans)


s = input()
n = len(s)
c = 0
for i in range(n):
	for j in range(i + 1, n):
		for k in range(j + 1, n):
			if s[i] + s[j] + s[k] == 'QAQ':
				c += 1
print(c)


s = input().strip()
n = len(s)
q = [0] * n
qa = [0] * n
qaq = [0] * n
if s[0] == 'Q':
	q[0] = 1
for i in range(1, n):
	q[i] += q[i - 1]
	qa[i] += qa[i - 1]
	qaq[i] += qaq[i - 1]
	if s[i] == 'Q':
		q[i] += 1
		qaq[i] += qa[i]
	elif s[i] == 'A':
		qa[i] += q[i]
print(qaq[n - 1])


def solution(s):
	n = len(s)
	m = 3
	cSt = 'QAQ'
	dp = [[0 for c in range(m + 1)] for r in range(n + 1)]
	for i in range(n + 1):
		dp[i][0] = 1
	for i in range(1, n + 1, 1):
		for j in range(1, m + 1, 1):
			if s[i - 1] == cSt[j - 1]:
				dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
			else:
				dp[i][j] = dp[i - 1][j]
	print(dp[n][m])
seq = input()
solution(seq)


n = list(input())
A = []
count = 0
for i in n:
	if i == 'Q' or i == 'A':
		A.append(i)
for i in range(len(A) - 2):
	for j in range(i + 1, len(A) - 1):
		for k in range(j + 1, len(A)):
			x = []
			x.append(A[i])
			x.append(A[j])
			x.append(A[k])
			x = ''.join(x)
			if x == 'QAQ':
				count += 1
print(count)


string = str(input())
n = len(string)
count = 0
arr = []
flag = 0
for i in range(n):
	if string[i] == 'A' and flag == 1:
		count += 1
	if string[i] == 'Q':
		arr.append(count)
		flag = 1
sum_ = 0
m = len(arr)
for i in range(0, m):
	for j in range(i + 1, m):
		sum_ += arr[j] - arr[i]
print(sum_)


from __future__ import division, print_function
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from math import ceil, floor, log, sqrt, factorial, pow, pi

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)
	print(l)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = {}
	for i in range(n):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans
count = 0
s = list(si())
if len(s) >= 3:
	for i in range(len(s)):
		for j in range(i + 1, len(s)):
			for k in range(j + 1, len(s)):
				if s[i] + s[j] + s[k] == 'QAQ':
					count += 1
	print(count)
else:
	print(0)


s = input()
c = 0
q = ['Q', 'A', 'Q']
j = 1
for i in range(len(s)):
	if s[i] == q[1]:
		c = c + s[:i].count(q[0]) * s[i + 1:].count(q[2])
print(c)


string = []
for char in input():
	if char == 'A':
		string.append('A')
	elif char == 'Q':
		string.append('Q')
if len(string) == 0:
	print(0)
c = 0
front = 0
back = 0
Q = []
A = []
if len(string) != 0:
	for a in range(len(string)):
		if string[a] == 'Q':
			Q.append(a)
		else:
			A.append(a)
	if len(A) == 0 or len(Q) == 0:
		print(0)
	else:
		for i in range(len(Q)):
			if Q[i] < A[0]:
				front += 1
		back = len(Q) - front
		c += front * back
		for j in range(1, len(A)):
			x = A[j] - A[j - 1] - 1
			front += x
			back -= x
			c += front * back
		print(c)


str = input()
qaq = 'AQ'
dp = [0 for i in range(len(str))]
if str[0] == 'Q':
	dp[0] = 1
for i in range(1, len(str)):
	if str[i] == 'Q':
		dp[i] = dp[i - 1] + 1
	else:
		dp[i] = dp[i - 1]
for t_letter in qaq:
	index = 0
	for letter in str:
		if index == 0:
			if t_letter == letter:
				dp[index] = dp[index]
			else:
				dp[index] = 0
		elif t_letter == letter:
			dp[index] = dp[index] + dp[index - 1]
		else:
			dp[index] = dp[index - 1]
		index = index + 1
print(dp[len(str) - 1])


class Solver:

	def __init__(self):
		self.memo = {}

	def solve(self, string, pattern):
		if len(pattern) == 0:
			return 1
		count = 0
		for (i, c) in enumerate(string):
			if c == pattern[0]:
				count += self.solve(string[i + 1:], pattern[1:])
		self.memo[string, pattern] = count
		return count
string = input()
filtered = []
for c in string:
	if c in 'AQ':
		filtered.append(c)
filtered = ''.join(filtered)
print(Solver().solve(filtered, 'QAQ'))


qaq = input()
(Q, A, co) = ([], [], 0)
for x in range(0, len(qaq)):
	if qaq[x] == 'A':
		A.append(x)
for i in A:
	co += qaq[i:].count('Q') * qaq[:i].count('Q')
print(co)


from sys import stdin
from math import sqrt, ceil
input = stdin.readline
s = input().rstrip()
ans = 0
l = len(s)
for i in range(l):
	for j in range(i + 1, l):
		for u in range(j + 1, l):
			if s[i] + s[j] + s[u] == 'QAQ':
				ans += 1
print(ans)


letters = list(input())
qaq = 0
for i in range(len(letters)):
	if letters[i] == 'A':
		l1 = letters[:i]
		l2 = letters[i + 1:]
		q1 = l1.count('Q')
		q2 = l2.count('Q')
		qaq = qaq + q1 * q2
print(qaq)


def QAQs(s):
	n = 0
	for i in range(len(s)):
		if s[i] == 'A':
			prevQs = len([l for l in s[:i + 1] if l == 'Q'])
			postQs = len([l for l in s[i:] if l == 'Q'])
			if prevQs > 0 and postQs > 0:
				n += prevQs * postQs
	return n
print(QAQs(input()))


s = input()
Q_count = []
for c in s:
	cv = (Q_count[-1] if Q_count else 0) + (1 if c == 'Q' else 0)
	Q_count.append(cv)
print(sum((Q_count[i] * (Q_count[-1] - Q_count[i]) for (i, c) in enumerate(s) if c == 'A')))


q = input()
(a, s) = ([], [])
for i in range(0, len(q)):
	if q[i] == 'Q':
		a.append(i)
	elif q[i] == 'A':
		s.append(i)
ans = 0
for i in a:
	h = list([x for x in s if x > i])
	for j in h:
		ans += len(list([y for y in a if y > j]))
print(ans)


s = input().strip()

def count(s, p):
	ns = len(s)
	np = len(p)
	dp = [[0 for _ in range(np + 1)] for _ in range(ns + 1)]
	for i in range(np + 1):
		dp[ns][i] = 0
	for i in range(ns + 1):
		dp[i][np] = 1
	for i in range(ns - 1, -1, -1):
		for j in range(np - 1, -1, -1):
			c = 0
			if s[i] == p[j]:
				c += dp[i + 1][j + 1]
			c += dp[i + 1][j]
			dp[i][j] = c
	return dp[0][0]
print(count(s, 'QAQ'))


a = list(input())
b = []
for i in range(len(a)):
	if a[i] == 'Q' or 'A':
		b.append(a[i])
out = 0
zong = ''.join(b).count('Q')
for i in range(len(b)):
	if b[i] == 'A':
		t = ''.join(b).count('Q', 0, i)
		out += t * (zong - t)
print(out)


A = input()
m = len(A)
B = 'QAQ' * 100
n = len(B)
F = [[0] * (n + 1) for i in range(m + 1)]
for i in range(1, m + 1):
	for j in range(1, n + 1):
		if A[i - 1] == B[j - 1]:
			F[i][j] = F[i - 1][j - 1] + 1
		else:
			F[i][j] = max(F[i - 1][j], F[i][j - 1])
ans = []
i = m
j = n
while i > 0 and j > 0:
	if A[i - 1] == B[j - 1]:
		ans.append(A[i - 1])
		i -= 1
		j -= 1
	elif F[i][j] == F[i - 1][j]:
		i = i - 1
	else:
		j = j - 1
ans = ans[::-1]
count = 0
for i in range(len(ans) - 2):
	if ans[i] == 'Q':
		for j in range(i + 1, len(ans) - 1):
			if ans[j] == 'A':
				for k in range(j + 1, len(ans)):
					if ans[k] == 'Q':
						count += 1
print(count)


s = input()
retl = []
for char in s:
	if char in 'QA':
		retl += [char]
s = ''.join(retl).strip('A')
ans = 0
for (i, char) in enumerate(s):
	if char == 'A':
		ans += s[:i].count('Q') * s[i:].count('Q')
print(ans)


S = input()
T = ''
for i in range(len(S)):
	if S[i] == 'A' or S[i] == 'Q':
		T = T + S[i]
Q = []
A = []
for i in range(len(T)):
	if T[i] == 'Q':
		Q.append(i)
	else:
		A.append(i)
n = 0
for i in range(len(Q)):
	for j in range(len(A)):
		for k in range(len(Q)):
			if Q[i] < A[j] and A[j] < Q[k]:
				n += 1
print(n)


i = input()
import itertools
total = 0
for x in itertools.combinations(i, 3):
	if x == ('Q', 'A', 'Q'):
		total += 1
print(total)


s = input()
ps = [0] * 105
for i in range(len(s)):
	ps[i + 1] = ps[i]
	if s[i] == 'Q':
		ps[i + 1] += 1
ans = 0
for i in range(len(s)):
	if s[i] == 'A':
		ans += ps[i] * (ps[len(s)] - ps[i])
print(ans)


string = input()
sum = 0
for i in range(len(string)):
	if string[i] == 'Q':
		for j in range(i + 1, len(string)):
			if string[j] == 'A':
				sum += string[j:len(string)].count('Q')
print(sum)


v = input()
n = len(v)
st = [0] * (n + 1)
end = [0] * (n + 1)
stc = 0
ec = 0
for i in range(n):
	st[i + 1] = st[i] + (v[i] == 'Q')
inv = v[::-1]
for i in range(n):
	end[i + 1] = end[i] + (inv[i] == 'Q')
end = end[::-1]
c = 0
for i in range(n):
	c += st[i] * end[i] if v[i] == 'A' else 0
print(c)


s = input()
u = len(s)
cont = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i, u):
			if s[j] == 'A':
				for k in range(j, u):
					if s[k] == 'Q':
						cont = cont + 1
print(cont)


a = input()
b = a.upper()
l = len(a)
c = []
sum = 0
for i in b:
	if i == 'Q' or i == 'A':
		c.append(i)
for i in range(len(c)):
	if c[i] == 'Q':
		for j in range(i + 1, len(c)):
			if c[j] == 'A':
				for k in range(j + 1, len(c)):
					if c[k] == 'Q':
						sum = sum + 1
print(sum)


s = input()
cq = s.count('Q')
k = q = 0
for c in s:
	if c == 'Q':
		q += 1
	elif c == 'A':
		k += q * (cq - q)
print(k)


n = input()
ans = 0
for i in range(len(n)):
	if n[i] == 'Q':
		for j in range(len(n) - i):
			if n[i + j] == 'A':
				for k in range(len(n) - i - j):
					if n[i + j + k] == 'Q':
						ans += 1
print(ans)


s = input()
ans = 0
n = len(s)
Q_total = sum([1 for i in s if i == 'Q'])
Q_prefix = [0 for i in range(n)]
if s[0] == 'Q':
	Q_prefix[0] = 1
for i in range(1, n):
	Q_prefix[i] = Q_prefix[i - 1]
	if s[i] == 'Q':
		Q_prefix[i] += 1
	if s[i] == 'A':
		ans += Q_prefix[i] * (Q_total - Q_prefix[i])
print(ans)


string = input()
count = 0
for x in range(len(string)):
	if string[x] == 'Q':
		for y in range(x + 1, len(string)):
			if string[y] == 'A':
				for z in range(y + 1, len(string)):
					if string[z] == 'Q':
						count += 1
print(count)


x = input()
t = len(x)
l = 0
for i in range(t):
	for j in range(i + 1, t):
		for k in range(j + 1, t):
			s = x[i] + x[j] + x[k]
			if s == 'QAQ':
				l += 1
print(l)


a = input()
dp = [0 for i in range(len(a))]
for i in range(len(a)):
	dp[i] = dp[i - 1]
	if a[i] == 'Q':
		dp[i] += 1
dp2 = [0 for i in range(len(a) + 1)]
for i in range(len(a) - 1, -1, -1):
	dp2[i] = dp2[i + 1]
	if a[i] == 'Q':
		dp2[i] += 1
print(sum([dp[i] * dp2[i] for i in range(len(a)) if a[i] == 'A']))


t = input()
sum1 = 0
r = float('-inf')
(r1, r2) = (0, 0)
while True:
	if 'A' in t:
		r = t.index('A')
	else:
		break
	if 'Q' in t[:r]:
		r1 = t[:r].count('Q') + r1
	if 'Q' in t[r:]:
		r2 = t[r:].count('Q')
	else:
		break
	s = r1 * r2
	t = t[r + 1:]
	sum1 = sum1 + s
print(sum1)


cch = {}

def find(a, bb, i):
	if cch.get(a) != None:
		if cch[a][i] != -1:
			return cch[a][i]
	else:
		ls = [-1 for i in range(110)]
		cch[a] = ls
	tot = 0
	for k in range(i, len(bb)):
		if a[0] == bb[k]:
			if len(a) == 1:
				tot += 1
			else:
				tot += find(a[1:], bb, k + 1)
	cch[a][i] = tot
	return tot
inp = input()
print(find('QAQ', inp, 0))


cin = input()
theList = list(cin)
x = 0
for c in theList:
	if c == 'Q':
		x = x + 1
tmp = 0
ans = 0
for c in theList:
	if c == 'Q':
		tmp = tmp + 1
	elif c == 'A':
		ans = ans + tmp * (x - tmp)
print(ans)


str = input()
q1 = []
idx = -1
a = []
count = 0
for i in str:
	idx += 1
	if i == 'Q':
		q1.append(idx)
	if i == 'A':
		if q1 != []:
			a.append(idx)
if len(q1) <= 1 or len(a) == 0:
	print(0)
else:
	for i in range(0, len(q1) - 1):
		for j in range(i + 1, len(q1)):
			for k in a:
				if k > q1[j]:
					break
				elif k < q1[j] and k > q1[i]:
					count += 1
	print(count)


s = input()
q = 0
res = 0
e = 0
for w in range(len(s)):
	if s[w] == 'A':
		count1 = 0
		count2 = 0
		for r in range(w):
			if s[r] == 'Q':
				count1 = count1 + 1
		for r in range(w + 1, len(s)):
			if s[r] == 'Q':
				count2 = count2 + 1
		res = res + count1 * count2
print(res)


def main():
	data = input()
	prefix = [0] * len(data)
	if data[0] == 'Q':
		prefix[0] = 1
	for i in range(1, len(data)):
		if data[i] == 'Q':
			prefix[i] = prefix[i - 1] + 1
		else:
			prefix[i] = prefix[i - 1]
	ans = 0
	for i in range(1, len(data) - 1):
		if data[i] == 'A':
			ans += prefix[i] * (prefix[-1] - prefix[i])
	print(ans)
main()


def main():
	ip = input()
	cp = 'QAQ'
	if len(ip) < 3:
		print('0')
		exit()
	bt = []
	for row in range(len(cp)):
		n = []
		for col in range(len(ip)):
			n.append(0)
		bt.append(n)
	n_rows = len(cp)
	n_cols = len(ip)
	for col in range(n_cols):
		for row in range(n_rows):
			if cp[row] == ip[col]:
				bt[row][col] = 1
	cnt = 0
	n_rows = len(cp)
	n_cols = len(ip)
	for col in range(len(ip)):
		if bt[0][col] == 1:
			for i in range(col + 1, n_cols):
				if bt[1][i] == 1:
					for j in range(i + 1, n_cols):
						if bt[2][j] == 1:
							cnt += 1
	print(cnt)
main()


q = input()
a = len(q)
t = 0
f = False
for i in range(a):
	if q[i] == 'Q':
		for j in range(i + 1, a):
			if q[j] == 'A':
				for k in range(j + 1, a):
					if q[k] == 'Q':
						t += 1
print(t)


s = input()
f = []
b = []
c = 0
for i in s:
	if i == 'Q':
		c += 1
	f.append(c)
c = 0
for i in reversed(s):
	if i == 'Q':
		c += 1
	b = [c] + b
total = 0
for (i, v) in enumerate(s):
	if v == 'A':
		total += f[i] * b[i]
print(total)


array = input()
a = len(array)
o = 0
for i in range(a):
	if array[i] == 'Q':
		for j in range(i + 1, a):
			if array[j] == 'A':
				for k in range(j + 1, a):
					if array[k] == 'Q':
						o += 1
print(o)


texto = input()
cont = 0
for i in range(0, len(texto) - 2, 1):
	if texto[i] == 'Q':
		for j in range(i + 1, len(texto) - 1, 1):
			if texto[j] == 'A':
				for k in range(j + 1, len(texto), 1):
					if texto[k] == 'Q':
						cont = cont + 1
print(cont)


string = input()
substring = 'QAQ'
res = []
count = 0
for i in range(len(string) - 1):
	if string[i] == 'Q':
		for j in range(i + 1, len(string)):
			if string[j] == 'A':
				for k in range(j, len(string)):
					if string[k] == 'Q':
						count += 1
						res.append(string[i] + string[j] + string[k])
print(len(res))


s = input()
n = len(s)
ans = 0
for i in range(n):
	if s[i] == 'Q':
		j = i + 1
		count_A = 0
		while j < n:
			if s[j] == 'A':
				count_A += 1
			elif s[j] == 'Q':
				ans += count_A
			j += 1
print(ans)


seq = input()
pf = [0 for x in range(len(seq))]
sm = 0
for i in range(len(seq)):
	s = seq[i]
	if s == 'Q':
		sm += 1
	pf[i] = sm
sf = [0 for x in range(len(pf))]
for i in range(len(pf)):
	sf[i] = pf[-1] - pf[i]
ans = 0
for i in range(len(seq)):
	s = seq[i]
	if s == 'A':
		ans += pf[i] * sf[i]
print(ans)


s = input()
prefix = [0 for i in range(len(s) + 1)]
postfix = [0 for i in range(len(s) + 1)]
for (i, v) in enumerate(s):
	prefix[i] = (v == 'Q') + prefix[i - 1]
for i in range(len(s) - 1, -1, -1):
	postfix[i] = (s[i] == 'Q') + postfix[i + 1]
res = 0
for (i, c) in enumerate(s):
	if c == 'A':
		res += prefix[i - 1] * postfix[i + 1]
print(res)


s = input()
qc = s.count('Q')
ans = 0
n = len(s)
dp = [0] * (n + 1)
c = 0
for i in range(n):
	if s[i] == 'Q':
		c += 1
	dp[i] = c
for i in range(n):
	if s[i] == 'A':
		ans += dp[i] * (qc - dp[i])
print(ans)


s = input()
count_Q = s.count('Q')
count = 0
ans = 0
for i in range(len(s)):
	if s[i] == 'A':
		ans += count * (count_Q - count)
	if s[i] == 'Q':
		count += 1
print(ans)


s = input()
l = len(s)
ans = 0
for i in range(l):
	if s[i] == 'A':
		k1 = 0
		k2 = 0
		for j in range(i):
			if s[j] == 'Q':
				k1 += 1
		for j in range(i, l):
			if s[j] == 'Q':
				k2 += 1
		ans += k1 * k2
print(ans)


s = input()
res = 0
if len(s) > 2:
	for i in range(len(s)):
		if s[i] == 'Q':
			for j in range(i + 1, len(s)):
				if s[j] == 'A':
					if s[j + 1:].count('Q') != -1:
						res += s[j + 1:].count('Q')
print(res)


import re
s = input()
s1 = re.findall('[QA]', s)
l = len(s1)
c = 0
for i in range(l):
	if s1[i] == 'Q':
		for j in range(i + 1, l):
			if s1[j] == 'A':
				for k in range(j + 1, l):
					if s1[k] == 'Q':
						c += 1
print(c)


l = list(input())
n = len(l)
arr = [0] * n
count = 0
for i in range(n):
	if l[i] == 'Q':
		count += 1
	arr[i] = count
ans = 0
for i in range(n):
	if l[i] == 'A':
		ans += arr[i] * (count - arr[i])
print(ans)


def countcombsub(s, s1):
	n = len(s)
	m = len(s1)
	dp = [[0 for i in range(m + 1)] for i in range(n + 1)]
	for i in range(n + 1):
		dp[i][0] = 1
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if s[i - 1] == s1[j - 1]:
				dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
			else:
				dp[i][j] = dp[i - 1][j]
	print(dp[n][m])
s = input()
s1 = 'QAQ'
countcombsub(s, s1)


from functools import lru_cache

def subseqsearch(string, substr):
	substrset = set(substr)
	fixs = [i for i in string if i in substrset]

	@lru_cache(maxsize=None)
	def recs(fi=0, si=0):
		if si >= len(substr):
			return 1
		r = 0
		for i in range(fi, len(fixs)):
			if substr[si] == fixs[i]:
				r += recs(i + 1, si + 1)
		return r
	return recs()
from functools import reduce

def flat(i):
	return reduce(lambda x, y: x + y, i, [])
string = input()
substr = 'QAQ'
print(subseqsearch(string, substr))


s = input()
dp = [[0 for _ in range(3)] for _ in range(len(s))]
dp[0][0] = int(s[0] == 'Q')
for i in range(1, len(s)):
	dp[i][0] = dp[i - 1][0] + int(s[i] == 'Q')
	dp[i][1] = dp[i - 1][1] + dp[i - 1][0] * int(s[i] == 'A')
	dp[i][2] = dp[i - 1][2] + dp[i - 1][1] * int(s[i] == 'Q')
print(dp[-1][2])


s = input()
is_a = [c == 'A' for c in s]
is_q = [int(c == 'Q') for c in s]
for i in range(1, len(s)):
	is_q[i] = is_q[i - 1] + is_q[i]
n_q = is_q[-1]
n_words = 0
for i in range(len(s)):
	if is_a[i]:
		n_words += is_q[i] * (n_q - is_q[i])
print(n_words)


str1 = input()
ans = 0
for (i, char1) in enumerate(str1):
	if char1 == 'A':
		count1 = 0
		count2 = 0
		for j in range(i):
			if str1[j] == 'Q':
				count1 += 1
		for j in range(i + 1, len(str1)):
			if str1[j] == 'Q':
				count2 += 1
		ans += count1 * count2
print(ans)


inp = input()
count = 0
lst = []
for i in inp:
	if i == 'Q':
		lst.append(0)
		count += sum(lst)
	elif i == 'A':
		for j in range(len(lst)):
			lst[j] += 1
print(count)


stri = list(input())
(count, arr, q) = (0, [], 0)
for i in range(len(stri)):
	if stri[i] == 'Q' or stri[i] == 'A':
		arr.append(stri[i])
for i in range(len(arr)):
	if arr[i] == 'Q':
		for j in range(i + 1, len(arr)):
			if arr[j] == 'Q':
				count += j - i - 1 - q
				q += 1
		q = 0
print(count)


s = input()
n = len(s)
q = [0] * n
c = 0
if s[0] == 'Q':
	q[0] = 1
for i in range(1, n):
	if s[i] == 'Q':
		q[i] = q[i - 1] + 1
	else:
		q[i] = q[i - 1]
for i in range(1, n):
	if s[i] == 'A':
		c += (q[-1] - q[i]) * q[i]
print(c)


s = input()
qs = []
curr = 0
for e in s:
	if e == 'Q':
		curr += 1
	elif e == 'A':
		qs.append(curr)
		curr = 0
if curr != 0:
	qs.append(curr)
total = 0
for i in range(len(qs)):
	total += sum(qs[:i]) * sum(qs[i:])
print(total)


s = input()
s1 = ''
for i in range(len(s)):
	if s[i] == 'Q' or s[i] == 'A':
		s1 += s[i]
k = 0
for i in range(len(s1)):
	if s1[i] == 'Q':
		for j in range(i + 1, len(s1)):
			if s1[j] == 'A':
				for l in range(j + 1, len(s1)):
					if s1[l] == 'Q':
						k += 1
print(k)


s = input()
n = len(s)
arr = [0] * (n + 1)
for i in range(1, n + 1):
	arr[i] = arr[i - 1] + (s[i - 1] == 'Q')
sm = 0
for i in range(n):
	if s[i] == 'A':
		a = arr[i]
		b = arr[-1] - arr[i]
		sm += a * b
print(sm)


s = input()
a = list()
for i in range(len(s)):
	if s[i] == 'A':
		a.append(i)
if len(a) == 0:
	print(0)
else:
	suum = s[a[0]:].count('Q') * s[:a[0]].count('Q')
	buf = suum
	for i in range(1, len(a)):
		if a[i] - a[i - 1] > 1:
			buf = s[a[i]:].count('Q') * s[:a[i]].count('Q')
			suum += buf
		else:
			suum += buf
	print(suum)


s = input()
q = 0
a = 0
result = 0
for (i, c) in enumerate(s):
	if c == 'A':
		qs_beforeA = s[0:i].count('Q')
		qs_afterA = s[i:].count('Q')
		result += qs_beforeA * qs_afterA
print(result)


s = input()
pre = []
count = 0
for i in s:
	if i == 'Q':
		count += 1
	if i == 'A':
		pre.append(count)
ans = 0
for i in pre:
	ans += i * (count - i)
print(ans)


s = input()
q = 0
for i in range(len(s)):
	if s[i] == 'Q':
		q = q + 1
c = 0
a = 0
for i in range(len(s)):
	if s[i] == 'Q':
		c = c + 1
	elif s[i] == 'A':
		a = a + c * (q - c)
print(a)


inputStr = input()
comb = [0, 0, 0]
count = 0
for i in range(len(inputStr)):
	if inputStr[i] == 'Q':
		comb[0] == 1
		for j in range(i, len(inputStr)):
			if inputStr[j] == 'A':
				comb[1] == 1
				for k in range(j, len(inputStr)):
					if inputStr[k] == 'Q':
						comb[2] += 1
						count += 1
print(count)


a = input()
b = [i for i in a]
c = 0
d = [0 for i in a]
e = [0 for i in a]
for i in range(len(b)):
	if b[i] == 'Q':
		c += 1
	d[i] = c
c = 0
for i in range(len(b)):
	if b[len(b) - i - 1] == 'Q':
		c += 1
	e[len(b) - i - 1] = c
c = 0
for i in range(len(b)):
	if b[i] == 'A':
		c += d[i] * e[i]
print(c)


word = input()
list1 = ''
for item in word:
	if item in 'QA':
		list1 += item
count = 0
for x in range(len(list1)):
	for y in range(x, len(list1)):
		for z in range(y, len(list1)):
			if list1[x] + list1[y] + list1[z] == 'QAQ':
				count += 1
print(count)


n = input()
length = len(n)
Jlh = 0
i = 0
while i < length - 2:
	j = i + 1
	while j < length - 1:
		k = j + 1
		while k < length:
			if n[i] == 'Q' and n[j] == 'A' and (n[k] == 'Q'):
				Jlh += 1
			k += 1
		j += 1
	i += 1
print(Jlh)


s = input()
ans = 0
sz = len(s)
for i in range(sz):
	for j in range(i + 1, sz):
		for l in range(j + 1, sz):
			if s[i] == 'Q' and s[j] == 'A' and (s[l] == 'Q'):
				ans += 1
print(ans)


string = input()
N = len(string)
dp = []
for i in range(100005):
	dp.append(0)
dp[0] = 1
for i in range(1, N + 1):
	if string[i - 1] == 'Q':
		dp[3] += dp[3 - 1]
		dp[1] += dp[1 - 1]
	elif string[i - 1] == 'A':
		dp[2] += dp[2 - 1]
print(dp[3])


s = input()
ps1 = [0] * len(s)
ps2 = [0] * len(s)
nq1 = 0
nq2 = 0
for i in range(len(s)):
	if s[i] == 'Q':
		nq1 += 1
	if s[i] == 'A':
		ps1[i] = nq1
for i in range(len(s) - 1, -1, -1):
	if s[i] == 'Q':
		nq2 += 1
	if s[i] == 'A':
		ps2[i] = nq2
ans = 0
for i in range(len(s)):
	if ps1[i] != 0 and ps2[i] != 0:
		ans += ps1[i] * ps2[i]
print(ans)


s = input()
posQ = []
posA = []
for i in range(len(s)):
	if s[i] == 'Q':
		posQ.append(i)
	if s[i] == 'A':
		posA.append(i)
ans = 0
for i in range(len(posA)):
	a = 0
	b = 0
	for j in range(len(posQ)):
		if posQ[j] > posA[i]:
			a += 1
		if posQ[j] < posA[i]:
			b += 1
	ans += a * b
print(ans)


string = input()
cout = 0
for i in range(0, len(string)):
	if i == len(string) - 1 or i == len(string) - 2:
		break
	if string[i] == 'Q':
		for j in range(i + 1, len(string) - 1):
			if string[j] == 'A':
				for k in range(j + 1, len(string)):
					if string[k] == 'Q':
						cout = cout + 1
	else:
		continue
print(cout)


line = input().strip()
for x in range(len(line)):
	if line[x] == 'Q':
		line = line[x:]
		break
for x in reversed(range(len(line))):
	if line[x] == 'Q':
		line = line[:x + 1]
		break
numQAQ = 0
for x in range(len(line)):
	if line[x] == 'Q':
		numA = 0
		for y in range(x, len(line)):
			if line[y] == 'A':
				numA += 1
			elif line[y] == 'Q':
				numQAQ += numA
print(numQAQ)


a = input()
u = 0
if len(a) < 3:
	print(0)
else:
	i = 0
	while i < len(a) - 2:
		j = i + 1
		while j < len(a) - 1:
			k = j + 1
			while k < len(a):
				if a[i] == 'Q' and a[j] == 'A' and (a[k] == 'Q'):
					u += 1
				k += 1
			j += 1
		i += 1
	print(u)


n = str(input())
number = 0
for ch in range(len(n)):
	if n[ch] == 'A':
		count = n[:ch].count('Q') * n[ch:].count('Q')
		number += count
print(number)


s = input()
i = 0
l = []
out = 0
while i < len(s):
	if s[i] == 'Q' or s[i] == 'A':
		l.append(s[i])
	i += 1
i = 0
count = 0
while i < len(l):
	q = 0
	p = 0
	while i < len(l) and l[i] != 'A':
		i += 1
	j = 0
	while j < i:
		if l[j] == 'Q':
			q += 1
		j += 1
	j = i
	while j < len(l):
		if l[j] == 'Q':
			p += 1
		j += 1
	out += q * p
	i += 1
print(out)


n = input()
l = len(n)
number1 = 0
number2 = 0
num = 0
for i in range(0, l):
	if n[i] == 'A':
		number1 = 0
		number2 = 0
		for j in range(0, i):
			if n[j] == 'Q':
				number1 += 1
		for k in range(i + 1, l):
			if n[k] == 'Q':
				number2 += 1
		num += number1 * number2
print(num)


from sys import stdin
letters = stdin.readline() + ''
Qs = 0
for i in letters:
	if i == 'Q':
		Qs += 1
totalQAQ = 0
curQs = 0
for i in letters:
	if i == 'Q':
		curQs += 1
	elif i == 'A':
		totalQAQ += curQs * (Qs - curQs)
print(totalQAQ)


s = input()
ans = 0
for i in range(len(s)):
	if s[i] == 'A':
		lcount = 0
		rcount = 0
		for l in range(i):
			if s[l] == 'Q':
				lcount += 1
		for r in range(i + 1, len(s)):
			if s[r] == 'Q':
				rcount += 1
		ans += lcount * rcount
print(ans)


st = ''
for i in input():
	if i == 'Q' or i == 'A':
		st += i
if len(st) <= 2:
	print(0)
else:
	sq = [0 for _ in range(len(st))]
	sq[-1] = 1 if st[-1] == 'Q' else 0
	for i in range(len(st) - 2, -1, -1):
		sq[i] = sq[i + 1] + (1 if st[i] == 'Q' else 0)
	ans = 0
	for i in range(len(st)):
		if st[i] == 'Q':
			for j in range(i + 1, len(st)):
				if st[j] == 'A':
					ans += sq[j]
	print(ans)


def dp(s1, s2, n, m):
	look = [[0 for i in range(m + 1)] for j in range(n + 1)]
	for i in range(m + 1):
		look[0][i] = 0
	for i in range(n + 1):
		look[i][0] = 1
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if s1[i - 1] == s2[j - 1]:
				look[i][j] = look[i - 1][j - 1] + look[i - 1][j]
			else:
				look[i][j] = look[i - 1][j]
	return look[n][m]
s1 = [i for i in input()]
s2 = ['Q', 'A', 'Q']
n = len(s1)
m = len(s2)
print(dp(s1, s2, n, m))


txt = input()
(n, i, j) = (0, 0, 0)
while i < len(txt):
	ind = txt.find('Q', i)
	if ind == -1:
		break
	i = ind
	j = i
	while j < len(txt):
		indj = txt.find('A', j + 1)
		if indj == -1:
			break
		j = indj
		n += txt.count('Q', indj + 1)
	i = i + 1
print(n)


s = input()
s = list(s)
k = []
counter = 0
for i in s:
	if i == 'Q' or i == 'A':
		k.append(i)
l = len(k)
for i in range(l):
	if k[i] == 'Q':
		for j in range(i, l):
			if k[j] == 'A':
				for m in range(j, l):
					if k[m] == 'Q':
						counter += 1
print(counter)


a = input()
b = 'QAQ'
m = len(a)
n = len(b)
mat = [[0 for i in range(n + 1)] for j in range(m + 1)]

def fun(a, b, m, n):
	if m == 0 and n == 0 or n == 0:
		return 1
	elif m == 0:
		return 0
	elif mat[m][n] != 0:
		return mat[m][n]
	elif a[m - 1] == b[n - 1]:
		mat[m][n] = fun(a, b, m - 1, n - 1) + fun(a, b, m - 1, n)
		return mat[m][n]
	else:
		mat[m][n] = fun(a, b, m - 1, n)
		return mat[m][n]
print(fun(a, b, m, n))


n = input()
(a, b) = ([], [])
t = 0
for x in range(len(n)):
	if n[x] == 'Q':
		a.append(x)
	elif n[x] == 'A':
		b.append(x)
for x in range(len(b)):
	(s, r) = (0, 0)
	for y in range(len(a)):
		if a[y] < b[x]:
			s += 1
		else:
			r += 1
	t += s * r
print(t)


def solve(s):
	prev_q = prev_qa = prev_qaq = 0
	for i in range(0, len(s)):
		curr_q = curr_qa = curr_qaq = 0
		if s[i] == 'Q':
			curr_qaq = prev_qaq + prev_qa
			curr_q = prev_q + 1
		elif s[i] == 'A':
			curr_qa = prev_qa + prev_q
		prev_q = max(prev_q, curr_q)
		prev_qa = max(prev_qa, curr_qa)
		prev_qaq = max(prev_qaq, curr_qaq)
	return prev_qaq
n = input()
print(solve(n))


s = input()
n = len(s)
Q = [0 for x in range(n)]
QA = [0 for x in range(n)]
QAQ = [0 for x in range(n)]
for i in range(n):
	if s[i] == 'Q':
		Q[i] += 1
	if i > 0:
		Q[i] += Q[i - 1]
for i in range(1, n):
	if s[i] == 'A':
		QA[i] += Q[i - 1]
	if i > 1:
		QA[i] += QA[i - 1]
for i in range(2, n):
	if s[i] == 'Q':
		QAQ[i] += QA[i - 1]
	if i > 2:
		QAQ[i] += QAQ[i - 1]
print(QAQ[n - 1])


st = input()
lst = []
for i in range(len(st)):
	if st[i] == 'Q':
		lst.append(i)
ans = 0
for i in range(len(lst)):
	for j in range(i + 1, len(lst)):
		ans += st[lst[i]:lst[j] + 1].count('A')
print(ans)


k = input('')
answer = 0
for i in range(len(k)):
	if k[i] == 'Q':
		for j in range(i + 1, len(k)):
			if k[j] == 'A':
				for z in range(j + 1, len(k)):
					if k[z] == 'Q':
						answer += 1
print(answer)


S = input()
k = 0
for i in range(len(S)):
	if S[i] == 'Q':
		k1 = 0
		for j in range(i, len(S)):
			if S[j] == 'Q':
				k += k1
			if S[j] == 'A':
				k1 += 1
print(k)


s = input()
ch = []
c = 0
for i in range(len(s)):
	if s[i] == 'Q':
		c += 1
	if s[i] == 'A':
		ch.append(c)
		c = 0
ch.append(c)
t = 0
for i in range(len(ch) - 1):
	t += sum(ch[:i + 1]) * sum(ch[i + 1:])
print(t)


string = input()
index = 1
index2 = index + 1
total = 0
for letter1 in string:
	if letter1 == 'Q':
		for letter2 in string[index:]:
			if letter2 == 'A':
				for letter3 in string[index2:]:
					if letter3 == 'Q':
						total += 1
			index2 += 1
	index += 1
	index2 = index + 1
print(total)


lst = list(input())
count = 0
for a in range(len(lst)):
	if lst[a] == 'Q':
		for b in range(a, len(lst)):
			if lst[b] == 'A':
				for c in range(b, len(lst)):
					if lst[c] == 'Q':
						count += 1
print(count)


n = input()
c = 0
qarr = [0]
count = 0
for i in range(0, len(n)):
	if n[i] == 'Q':
		qarr.append(count + 1)
		count = count + 1
	else:
		qarr.append(count)
c = 0
for i in range(1, len(n) - 1):
	if n[i] == 'A':
		c += qarr[i] * (qarr[-1] - qarr[i + 1])
print(c)


n = input()
a = []
for i in n:
	if i == 'Q' or i == 'A':
		a.append(i)
l = len(a)
sum = 0
for i in range(l):
	if a[i] == 'A':
		sum += a[0:i].count('Q') * a[i + 1:l].count('Q')
print(sum)


k = input()
sum = 0
for i in range(len(k)):
	if k[i] == 'A':
		sum += k[:i].count('Q') * k[i:].count('Q')
print(sum)


def main():
	message = input()
	n = findCombinations('QAQ', message)
	print(n)

def findCombinations(word, s):
	answer = 0
	if word == '' or s == '':
		return 0
	else:
		letter = word[0]
		n = s.find(letter)
		while n != -1 and s != '':
			s = s[n + 1:]
			if word == 'Q':
				answer += 1
			else:
				answer += findCombinations(word[1:], s)
			n = s.find(letter)
	return answer
main()


import sys
string = list(input())
q_count = string.count('Q')
if q_count == 0:
	print('0')
	sys.exit()
q_dict = []
location = string.index('Q')
q_dict.append(location)
for i in range(q_count - 1):
	q_dict.append(string.index('Q', location + 1))
	location = string.index('Q', location + 1)
a_count = string.count('A')
if a_count == 0:
	print('0')
	sys.exit()
a_dict = []
location = string.index('A')
a_dict.append(location)
for i in range(a_count - 1):
	a_dict.append(string.index('A', location + 1))
	location = string.index('A', location + 1)
qaq_count = 0
for q in q_dict:
	a_new_dict = [i for i in a_dict if i > q]
	for a in a_new_dict:
		q_new_dict = [i for i in q_dict if i > a]
		qaq_count += len(q_new_dict)
print(qaq_count)


n = input()
g = 0
for i in range(len(n)):
	if n[i] == 'Q':
		for j in range(i + 1, len(n)):
			if n[j] == 'A':
				for h in range(j + 1, len(n)):
					if n[h] == 'Q':
						g += 1
print(g)


stri = input()
ct = 0
for i in range(len(stri)):
	for j in range(i + 1, len(stri)):
		for k in range(j + 1, len(stri)):
			if stri[i] == 'Q' and stri[j] == 'A' and (stri[k] == 'Q'):
				ct += 1
print(ct)


s = input()
a = 0
q = 0
ans = 0
for c in s:
	if c == 'Q':
		ans += a
		q += 1
	if c == 'A':
		a += q
print(ans)


s = input()
t = 'QAQ'
n = len(s)
dp = [[0 for j in range(n + 1)] for i in range(3 + 1)]
for i in range(n + 1):
	dp[0][i] = 1
for i in range(1, 3 + 1):
	for j in range(1, n + 1):
		if s[j - 1] == t[i - 1]:
			dp[i][j] = dp[i - 1][j - 1]
		dp[i][j] += dp[i][j - 1]
print(dp[-1][-1])


string = input()
sA = 0
sQ = 0
k = 0
for i in string:
	if i == 'A':
		sA += sQ
	elif i == 'Q':
		sQ += 1
		k += sA
print(k)


message = input()
sum = 0
for i in range(0, len(message)):
	for j in range(i + 1, len(message)):
		for k in range(j + 1, len(message)):
			if message[i] == 'Q' and message[j] == 'A' and (message[k] == 'Q'):
				sum += 1
print(sum)


n = input()
n = list(n)
a = 0
b = 0
c = 0
for i in range(len(n)):
	if n[i] == 'Q':
		c += b
		a += 1
	if n[i] == 'A':
		b += a
print(c)


l = list(input())
l1 = []
l2 = []
sum = 0
for i in range(len(l)):
	if l[i] == 'Q':
		l1.append(i)
	elif l[i] == 'A':
		l2.append(i)
for i in range(len(l1) - 1):
	for k in range(len(l2)):
		if l2[k] > l1[i]:
			for j in range(i, len(l1)):
				if l1[j] > l2[k]:
					sum += 1
print(sum)


a = input()
co = 0
for i in range(len(a)):
	if a[i] == 'Q':
		for j in range(i + 1, len(a)):
			if a[j] == 'A':
				for k in range(j + 1, len(a)):
					if a[k] == 'Q':
						co += 1
print(co)


s = input()
for i in set(s):
	if i != 'Q' and i != 'A':
		s = s.replace(i, '')
n = 0
for i in range(len(s) - 2):
	for ii in range(i + 1, len(s) - 1):
		for iii in range(ii + 1, len(s)):
			if s[i] == 'Q' and s[ii] == 'A' and (s[iii] == 'Q'):
				n += 1
print(n)


s = input()
l = len(s)
t = 'QAQ'
m = len(t)
dp = [0] * (m + 1)
dp[0] = 1
for i in range(l):
	for x in range(m - 1, -1, -1):
		if s[i] == t[x]:
			dp[x + 1] += dp[x]
print(dp[m])


def mfilter(item):
	if item == 'Q' or item == 'A':
		return True
	return False

def count_qaqs(smstr):
	mc = 0
	qi = -1
	ai = 0
	while qi < len(smstr):
		qi = smstr.find('Q', qi + 1)
		if qi == -1:
			break
		ai = qi
		while ai < len(smstr):
			ai = smstr.find('A', ai + 1)
			if ai == -1:
				break
			mc += smstr[ai:].count('Q')
	return mc

def main():
	smstr = ''.join(list(filter(mfilter, list(input())))).strip('A')
	return count_qaqs(smstr)
print(main())


n = input()
x = len(n)
a = 0
t = 0
ans = 0
e = True
for i in range(x):
	if n[i] == 'Q':
		break
for i in range(i, x):
	k = n[i]
	if k == 'Q':
		a += 1
		if not e:
			ans += t
	elif k == 'A':
		if e:
			t = a
			e = False
		else:
			t += a
print(ans)


DP = [0, 0, 0]
for char in input():
	if char == 'Q':
		DP[2] += DP[1]
		DP[0] += 1
	if char == 'A':
		DP[1] += DP[0]
print(DP[2])


s = input()
n = 0
for q in range(len(s)):
	for a in range(len(s)):
		if q > a:
			for w in range(len(s)):
				if s[q] == 'Q' and s[a] == 'A' and (s[w] == 'Q') and (a > w):
					n = n + 1
print(n)


(q, a, o) = (0, 0, 0)
for i in input():
	if i == 'Q':
		q += 1
	elif i == 'A':
		o += q ** 2
		a += q
print(q * a - o)


s = input()
count = 0
for i in range(len(s)):
	if s[i] == 'A':
		(a, b) = (0, 0)
		for j in range(i - 1, -1, -1):
			if s[j] == 'Q':
				a += 1
		for j in range(i + 1, len(s)):
			if s[j] == 'Q':
				b += 1
		count += a * b
print(count)


q = list(input())
summ = 0
a = []
k = 0
while k < len(q) - 1 and q[k] != 'Q':
	k += 1
count = 1
for i in range(k + 1, len(q)):
	if k == len(q) - 1:
		summ = 0
		break
	elif q[i] != 'Q' and q[i] != 'A':
		continue
	elif q[i] == 'Q':
		if len(a) % 2 == 0:
			count += 1
		else:
			a.append(count)
			count = 1
	elif q[i] == 'A':
		if len(a) % 2 > 0:
			count += 1
		else:
			a.append(count)
			count = 1
a.append(count)
for i in range((len(a) - 1) // 2):
	for j in range(i, (len(a) - 1) // 2):
		for m in range(j, (len(a) - 1) // 2):
			summ += a[2 * i] * a[2 * j + 1] * a[2 * m + 2]
print(summ)


def main():
	s = input()
	counter = 0
	for i in range(len(s)):
		if s[i] == 'Q':
			for j in range(i, len(s)):
				if s[j] == 'A':
					for k in range(j, len(s)):
						if s[k] == 'Q':
							counter += 1
	print(counter)
main()


st = input()
Qcount = 0
QAcount = 0
QAQcount = 0
for ch in st:
	if ch == 'Q':
		Qcount += 1
		QAQcount += QAcount
	elif ch == 'A':
		QAcount += Qcount
print(QAQcount)


S = input()
N = len(S)
cnt = 0
for i in range(N):
	if S[i] == 'Q':
		for j in range(i + 1, N):
			if S[j] == 'A':
				for k in range(j + 1, N):
					if S[k] == 'Q':
						cnt += 1
print(cnt)


s = input()
l = []
temp = 0
i = 0
dp = [0] * (len(s) + 1)
while i < len(s):
	if s[i] == 'Q':
		temp += 1
	dp[i + 1] = temp
	i += 1
dp.pop(0)
qc = s.count('Q')
j = 1
dp2 = [0] * len(s)
temp = 0
if s[0] == 'A':
	temp = 1
while j < len(s):
	if s[j] == 'A':
		temp += 1
	else:
		dp2[j - 1] = temp
		temp = 0
	j += 1
ans = 0
for i in range(len(s)):
	if dp2[i] > 0:
		ans += dp2[i] * dp[i - 1] * (qc - dp[i - 1])
print(ans)


a = str(input())
b = len(a)
c = ''
x = k = int(0)
while x < b:
	if a[x] == 'Q' or a[x] == 'A':
		c = c + a[x]
	x = x + 1
c = c.lstrip('A').rstrip('A')
j = len(c)
qw = op = int(0)
while qw < j - 2:
	qe = qw + 1
	while qe < j - 1:
		qr = qe + 1
		while qr < j:
			qt = str(c[qw]) + str(c[qe]) + str(c[qr])
			if qt == 'QAQ':
				op = op + 1
			qr = qr + 1
		qe = qe + 1
	qw = qw + 1
print(op)


x = input()
z = []
k = 0
for i in x:
	if i == 'Q' or i == 'A':
		z.append(i)
for i in range(len(z)):
	if z[i] == 'Q':
		for b in range(i, len(z)):
			if z[b] == 'A':
				for c in range(b, len(z)):
					if z[c] == 'Q':
						k += 1
print(k)


cry = input()
n = len(cry)
key = []
ql = []
qr = []
count = 0
if cry[0] == 'Q':
	ql.append(1)
else:
	ql.append(0)
if cry[n - 1] == 'Q':
	qr.append(1)
else:
	qr.append(0)
for i in range(1, n):
	if cry[i] == 'Q':
		ql.append(1 + ql[i - 1])
	else:
		ql.append(ql[i - 1])
	if cry[n - i - 2] == 'Q':
		qr.append(1 + qr[i - 1])
	else:
		qr.append(qr[i - 1])
tears = 0
for i in range(1, n - 1):
	if cry[i] == 'A':
		tears += ql[i] * (ql[n - 1] - ql[i])
print(tears)


s = input()
m = []
a = 0
num = 0
for i in range(len(s)):
	if s[i] == 'Q' or s[i] == 'A':
		m.append(s[i])
	if s[i] == 'A':
		a += 1
for i in range(len(m)):
	if m[i] == 'A':
		num += m[:i].count('Q') * m[i:].count('Q')
print(num)


def answer():
	a = [x for x in list(input()) if x in 'AQ']
	i = 0
	ans = 0
	while i < len(a):
		if a[i] == 'Q':
			j = i + 1
			while j < len(a):
				if a[j] == 'A':
					k = j + 1
					while k < len(a):
						if a[k] == 'Q':
							ans += 1
						k += 1
				j += 1
		i += 1
	print(ans)
answer()


str = input()
l = len(str)
w = 0
for i in range(l):
	if str[i] == 'Q':
		for j in range(i, l):
			if str[j] == 'A':
				for k in range(j, l):
					if str[k] == 'Q':
						w += 1
print(w)


import sys
import heapq
import math
from enum import Enum
lines = sys.stdin.read().splitlines()
s = lines[0]
qs = 0
qas = 0
count = 0
for i in s:
	if i == 'Q':
		count += qas
		qs += 1
	elif i == 'A':
		qas += qs
print(count)


Line = [i for i in list(input()) if i in ['A', 'Q']]
num = 0
for i in range(len(Line)):
	if Line[i] == 'Q':
		for j in range(len(Line[i + 1:])):
			if Line[i + 1:][j] == 'A':
				num += Line[i + 1:][j + 1:].count('Q')
print(num)


x = input()
count1 = 0
n = len(x)
i = 0
while i < n:
	if x[i] == 'Q':
		i += 1
		j = i
		while j < n:
			if x[j] == 'Q':
				count1 += x[i:j].count('A')
			j += 1
	else:
		i += 1
print(count1)


s = input()
a = 'QAQ'
n = len(s)
l = []
for i in range(n):
	if s[i] == 'A':
		l.append(i)
e = 0
for i in l:
	e += s[:i + 1].count('Q') * s[i + 1:].count('Q')
print(e)


s = list(input())
total_q = s.count('Q')
ans = 0
q_l = 0
q_r = total_q
for c in s:
	if c == 'Q':
		q_l += 1
		q_r -= 1
	if c == 'A':
		ans += q_l * q_r
print(ans)


s = input()
n = len(s)
ans = 0
for i in range(n):
	if s[i] == 'A':
		j = i
		f = 0
		r = 0
		while j >= 0:
			if s[j] == 'Q':
				f += 1
			j -= 1
		j = i
		while j < n:
			if s[j] == 'Q':
				r += 1
			j += 1
		ans += f * r
print(ans)


class CodeforcesTask894ASolution:

	def __init__(self):
		self.result = ''
		self.string = ''

	def read_input(self):
		self.string = input()

	def process_task(self):
		qaqs = 0
		newseq = [x for x in self.string if x in 'QA']
		for x in range(len(newseq)):
			if newseq[x] == 'A':
				qaqs += newseq[:x].count('Q') * newseq[x:].count('Q')
		self.result = str(qaqs)

	def get_result(self):
		return self.result
Solution = CodeforcesTask894ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


qaq = input()
length = len(qaq)
num = 0
for a in range(length):
	if qaq[a] == 'Q':
		for b in range(a + 1, length):
			if qaq[b] == 'A':
				for c in range(b + 1, length):
					if qaq[c] == 'Q':
						num = num + 1
print(num)


word = input()
wordlen = len(word)
cnt = 0
i = 0
for i in range(0, wordlen):
	if word[i] == 'Q':
		k = i + 1
		for k in range(i + 1, wordlen):
			if word[k] == 'A':
				j = k + 1
				for j in range(k + 1, wordlen):
					if word[j] == 'Q':
						cnt = cnt + 1
print(cnt)


s = input()
count = {'Q': 0, 'A': 0, 'QA': 0}
ans = 0
for i in range(len(s)):
	if s[i] == 'Q':
		ans += count['QA']
		count['Q'] += 1
	elif s[i] == 'A':
		count['QA'] += count['Q']
print(ans)


t = input()
s = [0] * len(t)
point = 0
sol = 0
for i in range(len(t)):
	if t[i] == 'Q':
		point += 1
	if t[i] == 'A':
		s[i] = point
for i in range(len(t)):
	sol += s[i] * (point - s[i])
print(sol)


string = input()
cnt = 0
left = [0] * len(string)
right = [0] * len(string)
if string[0] == 'Q':
	left[0] = 1
if string[-1] == 'Q':
	right[-1] = 1
for idx in range(1, len(string)):
	c = string[idx]
	if c == 'Q':
		left[idx] += 1
	left[idx] += left[idx - 1]
for idx in range(len(string) - 2, -1, -1):
	c = string[idx]
	if c == 'Q':
		right[idx] += 1
	right[idx] += right[idx + 1]
ans = 0
for idx in range(len(string)):
	if string[idx] == 'A':
		ans += left[idx] * right[idx]
print(ans)


s = input()
n = len(s)
i = 0
presum = []
postsum = []
flag = 0
count1 = 0
count = 0
t = 0
for i in range(n):
	if s[i] == 'Q':
		count += 1
		if flag == 1:
			count1 += 1
	if s[i] == 'A':
		t = i
		presum.append(count)
		if flag == 1:
			postsum.append(count1)
			count1 = 0
		flag = 1
if t != n - 1 and count1 != 0:
	postsum.append(count1)
t = 0
n = len(postsum)
for i in range(n - 2, -1, -1):
	postsum[i] += postsum[i + 1]
for i in range(n):
	t += presum[i] * postsum[i]
print(t)


string = str(input())
QAQ = 0
for i in range(0, len(string)):
	if string[i] == 'Q':
		for x in range(i + 1, len(string)):
			if string[x] == 'A':
				for z in range(x + 1, len(string)):
					if string[z] == 'Q':
						QAQ += 1
print(QAQ)


main_string = input()
col = len(main_string)
count = 0
for i in range(col):
	if main_string[i] != 'Q':
		continue
	for j in range(i + 1, col):
		if main_string[j] != 'A':
			continue
		for k in range(j + 1, col):
			if main_string[k] == 'Q':
				count += 1
print(count)


x = input()
a = []
for s in range(0, len(x)):
	if x[s] == 'Q':
		a.append(s)
sum_A = 0
for i in range(0, len(a)):
	for j in range(i, len(a) - 1):
		sum_A = sum_A + x[a[i]:a[j + 1]].count('A')
print(sum_A)


a = [i for i in input()]
n = len(a)
pre = [0]
for i in range(n):
	if a[i] == 'Q':
		pre.append(1)
	else:
		pre.append(0)
for i in range(1, n + 1):
	pre[i] += pre[i - 1]
tot = pre[n]
ans = 0
for i in range(n):
	if a[i] == 'A':
		ans += pre[i] * (tot - pre[i])
print(ans)


a = str(input())
c = 0
for i in range(len(a)):
	if a[i] == 'A':
		Q1 = a[:i].count('Q')
		Q2 = a[i:].count('Q')
		c = c + Q1 * Q2
print(c)


x = input()
tot = 0
for i in range(len(x) - 2):
	if x[i] == 'Q':
		for j in range(i + 1, len(x) - 1):
			if x[j] == 'A':
				for k in range(j + 1, len(x)):
					if x[k] == 'Q':
						tot += 1
print(tot)


from collections import Counter
text = input().strip()
count = 0
lst = []
for i in text:
	if i == 'Q':
		count += 1
	elif i == 'A':
		lst.append(count)
ans = 0
for i in lst:
	ans += i * (count - i)
print(ans)


s = input()
(p, q, c) = (0, 0, 0)
for i in s:
	if i == 'Q':
		p += 1
for i in s:
	if i == 'Q':
		q += 1
		p -= 1
	if i == 'A':
		c += p * q
print(c)


message = [char for char in list(input()) if char == 'Q' or char == 'A']
before = 0
num_q = message.count('Q')
count = 0
for char in message:
	if char == 'Q':
		before += 1
	else:
		count += before * (num_q - before)
print(count)


from itertools import *
ans = 0
for i in combinations(input(), 3):
	ans += ''.join(i) == 'QAQ'
print(ans)


st = input()
i = 0
i2 = 0
i3 = 0
ANS = 0
while i < len(st):
	if st[i] == 'Q':
		while i2 < len(st[i:]):
			if st[i:][i2] == 'A':
				while i3 < len(st[i:][i2:]):
					if st[i:][i2:][i3] == 'Q':
						ANS += 1
					i3 += 1
				i3 = 0
			i2 += 1
		i2 = 0
	i += 1
	i2 = 0
print(ANS)


n = input()
o = []
for i in n:
	if i in ['A', 'Q']:
		o.append(i)
	else:
		continue
m = int(len(o))
if m == 0:
	print(0)
else:
	j = 0
	c = -1
	s = []
	for i in range(m - 1):
		if o[i] != o[i + 1]:
			j = i - c
			s.append((o[i], j))
			c = i
	if o[m - 2] == o[m - 1]:
		j = m - 1 - c
		s.append((o[m - 1], j))
	else:
		s.append((o[m - 1], 1))
	k = int(len(s))
	sum = 0
	for i in range(k):
		if s[i][0] == 'Q':
			for j in range(i, k):
				if s[j][0] == 'A':
					for p in range(j, k):
						if s[p][0] == 'Q':
							sum = sum + s[i][1] * s[j][1] * s[p][1]
						else:
							continue
				else:
					continue
		else:
			continue
	print(sum)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def solve():
	c = 0
	s = inp()
	n = len(s)
	for i in range(n):
		for j in range(i + 1, n):
			for k in range(j + 1, n):
				if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
					c += 1
	print(c)
solve()


s = input()
if 'Q' in s:
	a = []
	cur = 0
	for i in range(s.index('Q'), len(s)):
		if s[i] == 'A':
			cur += 1
		if s[i] == 'Q':
			a.append(cur)
	ans = 0
	n = len(a)
	for i in range(n):
		ans += a[i] * i - a[i] * (n - 1 - i)
	print(ans)
else:
	print(0)


total = 0
str = input()
for i in range(0, len(str)):
	if str[i] == 'Q':
		for j in range(i + 1, len(str)):
			if str[j] == 'A':
				for k in range(j + 1, len(str)):
					if str[k] == 'Q':
						total += 1
print(total)


def main():
	s = input()
	ret = 0
	for i in range(len(s)):
		if s[i] != 'Q':
			continue
		for j in range(i + 1, len(s)):
			if s[j] != 'A':
				continue
			for k in range(j + 1, len(s)):
				if s[k] != 'Q':
					continue
				ret += 1
	return ret
print(main())


from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
mp = lambda : list(map(int, cin().split()))

def chars():
	s = cin()
	return list(s[:len(s) - 1])

def pl(a):
	for val in a:
		cout(val)
	cout('\n')
s = chars()
n = len(s)
ans = 0
for i in range(n):
	if s[i] == 'Q':
		for j in range(i + 1, n):
			if s[j] == 'A':
				for k in range(j + 1, n):
					if s[k] == 'Q':
						ans += 1
cout(str(ans))


def qaq(s):
	k = 0
	for x in range(len(s) - 2):
		if s[x] == 'Q':
			for y in range(x + 1, len(s) - 1):
				if s[y] == 'A':
					for z in range(y + 1, len(s)):
						if s[z] == 'Q':
							k += 1
	return k
print(qaq(input()))


import re
input_str = input().upper()
count = 0
m = re.findall('[QA]', input_str)
for i in range(len(m) - 2):
	if m[i] == 'Q':
		for j in range(i + 1, len(m) - 1):
			if m[j] == 'A':
				for k in range(j + 1, len(m)):
					if m[k] == 'Q':
						count = count + 1
print(count)


from itertools import combinations
s = input()
s = ''.join((c for c in s if c == 'A' or c == 'Q'))
cnt = 0
for i in combinations(s, 3):
	cnt += 1 if i[0] == 'Q' and i[1] == 'A' and (i[2] == 'Q') else 0
print(cnt)


s = input()
q_tot = s.count('Q')
arr = []
q_left = 0
for i in s:
	if i == 'Q':
		q_left += 1
	if i == 'A':
		arr.append((q_left, q_tot - q_left))
	if q_left == q_tot:
		break
res = 0
for (r, l) in arr:
	res += r * l
print(res)


inp = input()
prev = 0
next = 0
res = 0
n = len(inp)
preprocessed = []
for i in range(n):
	if inp[i] == 'Q':
		prev += 1
	preprocessed.append(prev)
for i in range(n - 1, -1, -1):
	if inp[i] == 'Q':
		next += 1
	preprocessed[i] = (preprocessed[i], next)
for i in range(n):
	if inp[i] == 'A':
		(prev, next) = preprocessed[i]
		res += prev * next
print(res)


s = input()
sum = one = two = 0
N = len(s)
for i in range(N):
	if s[i] == 'Q':
		one += 1
for i in range(N):
	if s[i] == 'Q':
		two += 1
		one -= 1
	if s[i] == 'A':
		sum += one * two
print(sum)


s = input()
p = 0
q = s.count('Q')
ans = 0
l = len(s)
for i in range(0, l):
	if s[i] == 'Q':
		p += 1
		q -= 1
	if s[i] == 'A':
		ans += p * q
print(ans)


s = input()
n = len(s)
dpl = [0] * n
dpr = [0] * n
dpl[0] = 0 if s[0] != 'Q' else 1
for i in range(1, n):
	if s[i] == 'Q':
		dpl[i] = dpl[i - 1] + 1
	else:
		dpl[i] = dpl[i - 1]
dpr[-1] = 0 if s[-1] != 'Q' else 1
for i in range(n - 2, -1, -1):
	if s[i] == 'Q':
		dpr[i] = dpr[i + 1] + 1
	else:
		dpr[i] = dpr[i + 1]
c = 0
for i in range(n):
	if s[i] == 'A':
		c += dpl[i] * dpr[i]
print(c)


import sys
from functools import lru_cache
import re
regex = re.compile('[^AQ]')
inp = regex.sub('', sys.stdin.readline())

@lru_cache(maxsize=None)
def dp(l, score):
	if score == 3:
		return 1
	if len(l) == 0:
		return 0
	if ord(l[0]) == 16 * (score - 1) ** 2 + 65:
		return dp(l[1:], score + 1) + dp(l[1:], score)
	return dp(l[1:], score)
print(dp(inp, 0))


s = input()
n = len(s)
ans = 0
for z in range(n):
	if s[z] == 'Q':
		for i in range(z + 1, n):
			if s[i] == 'A':
				for j in range(i + 1, n):
					if s[j] == 'Q':
						ans += 1
print(ans)


s = input()
c = 0
for i in s:
	if i != 'Q' and i != 'A':
		s = s.replace(i, '!')
s = ''.join(s.split('!'))
while True:
	if s.count('Q') < 2 or s.count('A') < 1:
		print(c)
		break
	else:
		sa = s.index('A')
		L = s[:sa].count('Q')
		R = s[sa:].count('Q')
		c += L * R
		s = s[:sa] + s[sa + 1:]


s = input()
t = 0
t1 = 0
ans = 0
for i in s:
	if i == 'Q':
		t += 1
for i in s:
	if i == 'Q':
		t1 += 1
	if i == 'A':
		ans += t1 * (t - t1)
print(ans)


def main():
	q1 = a = q2 = 0
	for s in input():
		if s == 'Q':
			q1 += 1
			q2 += a
		elif s == 'A':
			a += q1
	return q2
print(main())


string = input()

def solve_one(string):
	n = len(string)
	count = 0
	for i in range(n):
		if string[i] == 'Q':
			for j in range(i, n):
				if string[j] == 'Q':
					count += string[i:j].count('A')
	print(count)
	return
solve_one(string)


import math
arr = list(input())
cnt = 0
for i in range(len(arr)):
	for j in range(i + 1, len(arr)):
		for k in range(j + 1, len(arr)):
			if arr[i] + arr[j] + arr[k] == 'QAQ':
				cnt += 1
print(cnt)


string = input()
(Q, A) = ([], [])
count = 0
for i in range(len(string)):
	if string[i] == 'Q':
		Q.append(i)
	elif Q != [] and string[i] == 'A':
		A.append(i)
ltcount = [0] * len(A)
gtcount = [0] * len(A)
for ai in range(len(A)):
	if A[ai] < Q[len(Q) - 1]:
		for qi in range(len(Q)):
			if A[ai] < Q[qi]:
				ltcount[ai] += 1
			if A[ai] > Q[qi]:
				gtcount[ai] += 1
		count = count + ltcount[ai] * gtcount[ai]
print(count)


n = input()
m = 0
for k in range(len(n)):
	if n[k] == 'A':
		m += n[:k].count('Q') * n[k:].count('Q')
	else:
		continue
print(m)


from itertools import combinations as c
print(sum(map(lambda x: x == ('Q', 'A', 'Q'), c((i for i in input() if i in 'QA'), 3))))


import itertools
s = input()
print(sum([1 for v in itertools.combinations(s, 3) if v == ('Q', 'A', 'Q')]))


l = [0, 0, 0]
s = input()
for i in s:
	if i == 'Q':
		l[0] += 1
	elif i == 'A':
		l[1] += l[0]
	if i == 'Q':
		l[2] += l[1]
print(l[2])


s = input()
qaq = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for j in range(i, len(s)):
			if s[j] == 'A':
				for x in range(j, len(s)):
					if s[x] == 'Q':
						qaq += 1
print(qaq)


st = input()
s = []
totalSum = 0
for i in st:
	if i == 'Q' or i == 'A':
		s.append(i)
for i in range(len(s)):
	if s[i] == 'A':
		a = s[:i].count('Q')
		b = s[i:].count('Q')
		totalSum += a * b
print(totalSum)


a = list(input())
out = 0
for i in range(len(a)):
	if a[i] == 'Q':
		for j in range(i, len(a)):
			if a[j] == 'A':
				for k in range(j, len(a)):
					if a[k] == 'Q':
						out += 1
print(out)


import re
from functools import lru_cache
message = input()
message = re.sub('[^QAQ]', '', message)

@lru_cache(maxsize=None)
def seq(i, sub):
	if i == len(message):
		return 0
	elif len(sub) == 1:
		return message[i:].count(sub)
	elif message[i] == sub[0]:
		return seq(i + 1, sub[1:]) + seq(i + 1, sub)
	else:
		return seq(i + 1, sub)
print(seq(0, 'QAQ'))


str = input()
n = len(str)
sum = 0
for i in range(0, n):
	for j in range(i + 1, n):
		for k in range(j + 1, n):
			if str[i] + str[j] + str[k] == 'QAQ':
				sum += 1
print(sum)


def qaqcount(n, s):
	total = 0
	for i in range(n):
		if s[i] == 'Q':
			for j in range(i + 1, n):
				if s[j] == 'A':
					for k in range(j + 1, n):
						if s[k] == 'Q':
							total += 1
	return total
s = input()
print(qaqcount(len(s), s))


n = list(input())
ans = 0
for i in range(len(n)):
	if n[i] == 'A':
		ans += n[:i].count('Q') * n[i:].count('Q')
print(ans)


from itertools import combinations
a = input().strip()
result = 0
s = ''
for i in a:
	if i in 'QA':
		s += i
i = 0
j = 1
k = 2
for i in list(combinations(s, 3)):
	t = ''.join(i)
	if t == 'QAQ':
		result += 1
print(result)


s = input()
q = [0]
qa = [0]
qaq = [0]
for i in range(len(s)):
	char = s[i]
	if char == 'Q':
		q += [q[i] + 1]
		qaq += [qa[i] + qaq[i]]
	else:
		q += [q[i]]
		qaq += [qaq[i]]
	if char == 'A':
		qa += [qa[i] + q[i]]
	else:
		qa += [qa[i]]
print(qaq[-1])


Cad = input()
cont = 0
S = 'QAQ'
C = ''
for i in range(len(Cad)):
	if Cad[i] == 'Q':
		C += Cad[i]
		for j in range(i + 1, len(Cad)):
			if Cad[j] == 'A':
				C += Cad[j]
				for k in range(j + 1, len(Cad)):
					if Cad[k] == 'Q':
						C += Cad[k]
						cont += 1
						C = ''
print(cont)


st = input()
q = 0
qa = 0
qaq = 0
for i in range(len(st)):
	if st[i] == 'Q':
		q += 1
		qaq = qa + qaq
	elif st[i] == 'A':
		qa = qa + q
print(qaq)


s = list(input())
for i in range(len(s) - 1, 0, -1):
	if s[i] is not 'Q' and s[i] is not 'A':
		del s[i]
ans = 0
for i in range(0, len(s)):
	if s[i] is 'Q':
		for j in range(i + 1, len(s)):
			if s[j] is 'A':
				for k in range(j + 1, len(s)):
					if s[k] is 'Q':
						ans += 1
print(ans)


t = input()
r = 0
et = str(t)
su = 0
for i in t:
	if i == 'A':
		su = su + et[:r].count('Q') * et[r:].count('Q')
	r = r + 1
print(su)


a = input()
ans = 0
n = len(a)
for j in range(n - 2):
	for i in range(j + 1, n - 1):
		for t in range(i + 1, n):
			if a[j] == 'Q' and a[i] == 'A' and (a[t] == 'Q'):
				ans += 1
print(ans)


s = input()
dp = [[0, 0, 0]]
for c in s:
	(Q, QA, QAQ) = dp[-1]
	if c == 'Q':
		Q += 1
		QAQ += QA
	if c == 'A':
		QA += Q
	dp.append([Q, QA, QAQ])
print(dp[-1][2])


s = input()
q = 0
a = 0
qa = 0
temp = 0
for i in s:
	if i == 'Q':
		q += 1
for i in s:
	if i == 'Q':
		temp += 1
	if i == 'A':
		qa += temp * (q - temp)
print(qa)


s = str(input())
new_s = ''
for i in range(len(s)):
	if s[i] == 'Q':
		new_s += 'Q'
	elif s[i] == 'A':
		new_s += 'A'
answer = 0
for i in range(len(new_s)):
	for x in range(i + 1, len(new_s)):
		for j in range(x + 1, len(new_s)):
			if new_s[i] + new_s[x] + new_s[j] == 'QAQ':
				answer += 1
print(answer)


s = input()
cnt = 0
lst = []
n = len(s)
for i in s:
	if i == 'Q':
		cnt += 1
	lst.append(cnt)
ans = 0
for i in range(n):
	if s[i] == 'A':
		ans += lst[i] * (lst[n - 1] - lst[i])
print(ans)


a = list(input())
b = list()
c = 0
for i in a:
	if i == 'Q' or i == 'A':
		b.append(i)
for i in range(len(b)):
	if b[i] == 'Q':
		for j in range(i, len(b)):
			if b[j] == 'A':
				for k in range(j, len(b)):
					if b[k] == 'Q':
						c += 1
print(c)


s = input()
ans = 0
for (i, j) in enumerate(s):
	if j == 'A':
		Qleft = s[:i].count('Q')
		Qright = s[i + 1:].count('Q')
		ans += Qleft * Qright
print(ans)


s = input()
d = 0
for i in range(len(s)):
	for j in range(i, len(s)):
		if s[i] == 'Q' and s[j] == 'Q':
			if 'A' in s[i:j]:
				d = d + s[i:j].count('A')
print(d)


n = input()
Qs = [i for i in range(len(n)) if n[i] == 'Q']
As = [i for i in range(len(n)) if n[i] == 'A']
QAQ = 0
for i in range(len(Qs)):
	for j in range(len(As)):
		for k in range(i + 1, len(Qs)):
			if Qs[i] < As[j] < Qs[k]:
				QAQ += 1
print(QAQ)


s = input()
zero = 0
for i in range(len(s)):
	for j in range(i + 1, len(s)):
		for k in range(j + 1, len(s)):
			if s[i] == s[k] and s[i] == 'Q' and (s[k] == 'Q') and (s[j] == 'A'):
				zero += 1
print(zero)


n = input()
n = n.lower()
t = 0
for i in range(len(n) - 2):
	for j in range(i + 1, len(n) - 1):
		for k in range(j + 1, len(n)):
			if n[i] == 'q' and n[j] == 'a' and (n[k] == 'q'):
				t += 1
print(t)


s = input()
res = 0
for i in range(len(s) - 2):
	for j in range(i + 1, len(s) - 1):
		for k in range(j + 1, len(s)):
			if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
				res += 1
print(res)


a = [char for char in input()]
r = 1
c = 0
while r < len(a) - 1:
	if a[r] == 'A':
		c1 = a[0:r].count('Q')
		c2 = a[r + 1:len(a)].count('Q')
		c = c + c1 * c2
	r = r + 1
print(c)


word = input()
Q = [0] * len(word)
for i in range(len(word)):
	if word[i] == 'Q':
		if i == 0:
			Q[i] = 1
		else:
			Q[i] = Q[i - 1] + 1
	elif i != 0:
		Q[i] = Q[i - 1]
total = 0
for i in range(len(word)):
	if word[i] == 'A' and i != 0 and (i != len(word) - 1):
		total += Q[i - 1] * (Q[len(word) - 1] - Q[i])
print(total)


string = str(input())
size = len(string)
cnt = 0
for x in range(size):
	for y in range(x):
		for z in range(y):
			if string[x] + string[y] + string[z] == 'QAQ':
				cnt += 1
print(cnt)


a = list(input())
sum = 0
for i in range(len(a)):
	if a[i] == 'A':
		sum += a[:i].count('Q') * a[i:].count('Q')
print(sum)


s = list(input())
q = []
a = []
ans = 0
for i in range(len(s)):
	if s[i] == 'Q':
		q.append(i)
	elif s[i] == 'A':
		a.append(i)
for i in a:
	bc = 0
	ac = 0
	for j in q:
		if j < i:
			bc += 1
		else:
			ac += 1
	ans += ac * bc
print(ans)


s = input()
qcnt = [0] * len(s)
cnt = 0
for i in range(len(s)):
	if s[i] == 'Q':
		cnt += 1
		qcnt[i] = cnt
	else:
		qcnt[i] = cnt
ans = 0
for j in range(1, len(s) - 1):
	if s[j] == 'A':
		ans += qcnt[j] * (qcnt[-1] - qcnt[j])
print(ans)


def single():
	return input()

def multiple():
	return list(map(int, input().split()))
s = single()
word = 'QAQ'
col = len(s)
dp = [[0 for j in range(col + 1)] for i in range(3)]
for i in range(1, col + 1):
	if word[0] == s[i - 1]:
		dp[0][i] = dp[0][i - 1] + 1
	else:
		dp[0][i] = dp[0][i - 1]
for i in range(1, 3):
	for j in range(i + 1, col + 1):
		if word[i] == s[j - 1]:
			dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
		else:
			dp[i][j] = dp[i][j - 1]
print(dp[-1][-1])


dp = []
s = input()
for (i, j) in enumerate(s):
	if j == 'Q':
		dp.append(i)
res = 0
for i in range(len(dp) - 1):
	res += s[dp[i] + 1:dp[i + 1]].count('A') * (i + 1) * (len(dp) - (i + 1))
print(res)


import re
str1 = input()
sum1 = 0
c = [x.start() for x in re.finditer('A', str1)]
for i in c:
	d = str1[0:i]
	e = str1[i + 1:]
	sum1 += d.count('Q') * e.count('Q')
print(sum1)


def main(s):
	if 'Q' in s and 'A' in s:
		s = s[s.index('Q'):]
	else:
		return 0
	mat = list((i.count('A') for i in s.split('Q') if i.count('A') > 0))
	sat = list((i.count('Q') for i in s.split('A') if i.count('Q') > 0))
	for i in range(len(sat)):
		sat[i] = sum((sat[i] * sum(mat[i:j]) * sat[j] for j in range(i, len(sat))))
	return sum(sat)
print(main(input()))


s = input()
ans = 0
a_index = [i for i in range(len(s)) if s[i] == 'A']
for i in range(len(s)):
	if s[i] == 'Q':
		for a in a_index:
			if a > i:
				ans += s.count('Q', a)
print(ans)


S = input()
A = 0
for i in range(len(S)):
	for j in range(i + 1, len(S)):
		if S[i] == 'Q' and S[j] == 'A':
			A += S[j + 1:].count('Q')
print(A)


def qaq(inp):
	found = 0
	for x in range(len(inp)):
		if inp[x] == 'Q':
			for y in range(x, len(inp)):
				if inp[y] == 'A':
					for z in range(y, len(inp)):
						if inp[z] == 'Q':
							found += 1
	print(found)
qaq(input().upper())


s = input()
(res, len) = (0, len(s))
for i in range(0, len):
	for y in range(i + 1, len):
		for j in range(y + 1, len):
			if s[i] == 'Q' and s[y] == 'A' and (s[j] == 'Q'):
				res += 1
print(res)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def LS():
	return sys.stdin.readline().split()

def S():
	return input()

def main():
	s = S()
	if len(s) < 3:
		return 0
	a = 0
	b = 1
	c = 2
	ans = 0
	while True:
		if a >= len(s):
			return ans
		while True:
			if b >= len(s):
				a += 1
				b = a + 1
				c = b + 1
				break
			while True:
				if c >= len(s):
					b += 1
					c = b + 1
					break
				if s[a] == 'Q' and s[b] == 'A' and (s[c] == 'Q'):
					ans += 1
				c += 1
print(main())


s = list(input())
d = len(s)
count = 0
for i in range(d - 2):
	for j in range(i + 1, d - 1):
		for k in range(j + 1, d):
			if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
				count += 1
print(count)


s = input()
n = len(s)
arr = [0] * n
if s[0] == 'Q':
	arr[0] = 1
for i in range(1, n):
	if s[i] == 'Q':
		arr[i] = arr[i - 1] + 1
	else:
		arr[i] = arr[i - 1]
total = 0
for i in range(2, n):
	if s[i] == 'Q':
		for j in range(i - 1, 0, -1):
			if s[j] == 'A':
				total += arr[j]
print(total)


line = input()
(s, s1, s2) = (0, 0, 0)
for i in range(len(line)):
	if line[i] == 'A':
		(s1, s2) = (0, 0)
		for j in range(i):
			if line[j] == 'Q':
				s1 += 1
		for j in range(i + 1, len(line)):
			if line[j] == 'Q':
				s2 += 1
		s += s1 * s2
print(s)


n = str(input())
q = []
a = []
count = 0
for i in n:
	if i == 'A':
		a.append(count)
	if i == 'Q':
		q.append(count)
	count += 1
nq = len(q)
na = len(a)
total = 0
prev = 0
for i in range(nq):
	for j in range(prev, na):
		if q[i] > a[j]:
			continue
		for u in range(i + 1, nq):
			if q[u] < a[j]:
				continue
			else:
				total += 1
print(total)


b = input()
q = 0
a = 0
n = 0
c = []
b = b[b.find('Q'):b.rfind('Q') + 1]
for i in range(len(b)):
	if b[i] == 'Q':
		q += 1
		c.append(b[i])
	elif b[i] == 'A' and q > 0:
		c.append(b[i])
q = 0
n = 0
for i in range(len(c)):
	if len(c) > 0:
		for i in range(c.index('Q') + 1):
			c.pop(0)
			n = 0
			q = 0
		for j in range(len(c)):
			if c[j - n] == 'A' and q == 0:
				a += c.count('Q')
				c.pop(j - n)
				n += 1
			elif c[j - n] == 'A':
				a += c.count('Q') - q
			else:
				q += 1
	else:
		break
print(a)


char_list = [char for char in input()]
q_before = [0] * len(char_list)
q_before[0] = 1 if char_list[0] == 'Q' else 0
for i in range(1, len(char_list)):
	q_before[i] = q_before[i - 1]
	if char_list[i] == 'Q':
		q_before[i] += 1
qa_before = [0] * len(char_list)
for i in range(1, len(char_list)):
	qa_before[i] = qa_before[i - 1]
	if char_list[i] == 'A':
		qa_before[i] += q_before[i - 1]
qaq_count = 0
for i in range(2, len(char_list)):
	if char_list[i] == 'Q':
		qaq_count += qa_before[i]
print(qaq_count)


list = input()
c = f = p = 0
for i in range(len(list)):
	if list[i] == 'Q':
		p = i
		for j in range(p + 1, len(list)):
			if list[j] == 'A':
				f = j
				for k in range(f + 1, len(list)):
					if list[k] == 'Q':
						c += 1
print(c)


s = input()
a = [0] * 105
b = [0] * 105
(n, q, ans) = (len(s), 0, 0)
for i in range(n):
	if s[i] == 'Q':
		q += 1
	a[i] = q
q = 0
for i in range(n - 1, -1, -1):
	if s[i] == 'Q':
		q += 1
	b[i] = q
for i in range(n):
	if s[i] == 'A':
		ans += a[i] * b[i]
print(ans)


x = str(input())
count = 0
length = len(x)
for i in range(length):
	if x[i] != 'Q':
		continue
	for j in range(i, length):
		if x[j] != 'A':
			continue
		for k in range(j, length):
			if x[k] == 'Q':
				count = count + 1
print(count)


st = input().strip()
n = len(st)
s = 'QAQ'
e = 0
for i in range(n):
	for j in range(i + 1, n):
		for k in range(j + 1, n):
			if st[i] == 'Q' and st[j] == 'A' and (st[k] == 'Q'):
				e += 1
print(e)


S = input()
Q = []
count_Q = 0
ans = 0
for i in range(0, len(S)):
	if S[i] == 'Q':
		count_Q += 1
	Q.append(count_Q)
for i in range(0, len(S)):
	if S[i] == 'A':
		ans += (Q[-1] - Q[i]) * Q[i]
print(ans)


s = input()
ans = 0
for i in range(len(s)):
	if s[i] == 'Q':
		for z in range(i, len(s)):
			if s[z] == 'A':
				for k in range(z, len(s)):
					if s[k] == 'Q':
						ans += 1
print(ans)


w = input()
n = len(w)
ans = 0
for i in range(n - 2):
	if w[i] != 'Q':
		continue
	for j in range(i + 1, n - 1):
		if w[j] != 'A':
			continue
		ans += w[j:].count('Q')
print(ans)


A = input()
right = A.count('Q')
left = 0
sum = 0
for i in A:
	if i == 'A':
		sum += left * right
	if i == 'Q':
		left += 1
		right -= 1
	if right == 0:
		break
print(sum)


Q = [0]
A = [0]
for i in input():
	if i == 'A':
		A.append(A[-1] + 1)
		Q.append(Q[-1])
	elif i == 'Q':
		Q.append(Q[-1] + 1)
		A.append(A[-1])
	else:
		A.append(A[-1])
		Q.append(Q[-1])
d = 0
ans = 0
for (v, i) in enumerate(A):
	if d != i:
		d = i
		ans += Q[v] * (Q[-1] - Q[v])
print(ans)


n = list(input())
m = len(n)
k = []
s = 0
for i in range(0, m):
	if n[i] == 'A':
		k.append(i)
for i in k:
	s += n[:i].count('Q') * n[i:].count('Q')
print(s)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	s = S()
	a = [1, 0, 0, 0]
	for c in s:
		if c == 'A':
			a[2] += a[1]
		elif c == 'Q':
			a[3] += a[2]
			a[1] += a[0]
	return a[3]
print(main())


import itertools
f = lambda : map(int, input().split())
print(sum((i == ('Q', 'A', 'Q') for i in itertools.combinations(input(), 3))))


q1 = 0
a = 0
q2 = 0
s = input()
for i in s:
	if i == 'Q':
		q1 += 1
		q2 += a
	if i == 'A':
		a += q1
print(q2)


s = input()
start = 0
end = s.count('Q')
ans = 0
for x in s:
	if x == 'Q':
		start += 1
		end -= 1
	if x == 'A':
		ans += start * end
print(ans)


s = input()
e = ''
for ch in s:
	if ch == 'Q' or ch == 'A':
		e = e + ch
n = len(e)
if n == 0:
	print(0)
else:
	dQ = [0] * n
	dQA = [0] * n
	d = [0] * n
	if e[0] == 'Q':
		dQ[0] = 1
	for i in range(1, len(e)):
		dQ[i] = dQ[i - 1] + (e[i] == 'Q')
		dQA[i] = dQA[i - 1] + dQ[i - 1] * (e[i] == 'A')
		d[i] = d[i - 1] + dQA[i - 1] * (e[i] == 'Q')
	print(d[-1])


str = input()
place = 0
A_list = []
while place < len(str):
	if str[place] == 'A':
		A_list.append(place)
	place = place + 1
sum = 0
for ele in A_list:
	Qfront = str.count('Q', 0, ele)
	Qend = str.count('Q', ele, len(str))
	sum = sum + Qfront * Qend
print(sum)


def find_max(a, low, mid, high):
	leftsum = -1
	s = 0
	for i in range(mid, low, -1):
		s = s + a[i]
		if s > leftsum:
			leftsum = s
			maxleft = i
	rightsum = -1
	s = 0
	for j in range(mid + 1, high):
		s = s + a[j]
		if s > rightsum:
			rightsum = s
			maxright = j
	return (maxleft, maxright, leftsum + rightsum)

def find(a, low, high):
	if high == low:
		return (low, high, a[low])
	else:
		mid = (low + high) // 2
		(leftlow, lefthigh, leftsum) = find(a, low, mid)
		(rightlow, righthigh, rightsum) = find(a, mid, high)
		(crosslow, crosshigh, crossum) = find_max(a, low, mid, high)
		if leftsum >= rightsum and leftsum >= crossum:
			return (leftlow, lefthigh, leftsum)
		elif rightsum >= leftsum and rightsum >= crossum:
			return (rightlow, righthigh, rightsum)
		else:
			return (crosslow, crosshigh, crossum)
s = input()
ans = 0
for i in range(len(s)):
	for j in range(i + 1, len(s)):
		for k in range(j + 1, len(s)):
			if s[i] == 'Q' and s[j] == 'A' and (s[k] == 'Q'):
				ans += 1
print(ans)


n = input()
count = 0
lista = []
if n[0] == 'Q':
	lista.append(1)
else:
	lista.append(0)
for x in range(1, len(n)):
	if n[x] == 'Q':
		lista.append(1 + lista[x - 1])
	else:
		lista.append(lista[x - 1])
count = 0
qLeft = 0
qRight = 0
for x in range(len(n)):
	if n[x] == 'A':
		qLeft = lista[x - 1]
		qRight = lista[-1] - qLeft
		if qRight > 0:
			count += qRight * qLeft
print(count)


s = input()
(qs_, as_) = ([i for i in range(len(s)) if s[i] == 'Q'], [i for i in range(len(s)) if s[i] == 'A'])
before = lambda x, arr: sum([1 for i in arr if i < x])
after = lambda x, arr: sum([1 for i in arr if i > x])
print(sum([before(x, qs_) * after(x, qs_) for x in as_]))


from collections import namedtuple
t = namedtuple('t', ['q', 'aq', 'qaq'])
s = input()
res = [t(0, 0, 0) for _ in range(len(s) + 1)]
for i in range(len(s) - 1, -1, -1):
	res[i] = t(res[i + 1].q + (1 if s[i] == 'Q' else 0), res[i + 1].aq + (res[i + 1].q if s[i] == 'A' else 0), res[i + 1].qaq + (res[i + 1].aq if s[i] == 'Q' else 0))
print(res[0].qaq)


s = input()
a = []
oc = 0
for i in range(1, len(s)):
	if s[i] == 'A':
		a.append(i)
st = 0
for j in a:
	q1 = s[0:j].count('Q')
	q2 = s[j + 1:].count('Q')
	oc += q1 * q2
print(oc)


L = input()
n = len(L)
V = 0

def BuscarC(i, L, n):
	c = 0
	for j in range(i, n):
		if L[j] == 'A':
			for k in range(j, n):
				if L[k] == 'Q':
					c += 1
	return c
for i in range(n):
	if L[i] == 'Q':
		V += BuscarC(i, L, len(L))
print(V)


s = input()
n = len(s)
ans = 0
for i in range(n):
	for j in range(i + 1, n):
		for x in range(j + 1, n):
			if s[i] + s[j] + s[x] == 'QAQ':
				ans += 1
print(ans)


a = input()
l = []
w = 0
s = 0
for i in range(len(a)):
	if a[i] == 'Q':
		w += 1
	if a[i] == 'A':
		l.append(w)
		w = 0
l.append(w)
for i in range(len(l)):
	j = i + 1
	while j < len(l):
		s += l[i] * l[j] * (j - i)
		j += 1
print(s)


a = input()
dict = {'Q': 0, 'QA': 0, 'QAQ': 0}
for i in range(len(a)):
	if a[i] == 'Q':
		dict['Q'] += 1
		if dict['QA'] > 0:
			dict['QAQ'] += dict['QA']
	if a[i] == 'A':
		if dict['Q'] > 0:
			dict['QA'] += dict['Q']
print(dict['QAQ'])


import os, sys
from io import BytesIO, IOBase
from sys import setrecursionlimit
import heapq
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
setrecursionlimit(100000)
mod = 1000000007

def main():
	s = input()
	dp = [0] * len(s)
	for i in range(len(s)):
		if s[i] == 'A':
			dp[i] = s[:i].count('Q') * s[i + 1:].count('Q')
	print(sum(dp))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


s = input()
n = len(s)
if n == 1 or n == 2:
	print(0)
else:
	q1 = s.count('Q')
	res = 0
	q2 = 0
	for i in range(n):
		if s[i] == 'Q':
			q2 += 1
			q1 -= 1
		if s[i] == 'A':
			res += q1 * q2
	print(res)


from itertools import combinations
n = input()
c = 0
for i in combinations(n, 3):
	if i == ('Q', 'A', 'Q'):
		c += 1
print(c)


s = input()
s1 = []
s2 = []
l = 0
if s.count('A') == 0:
	print(0)
else:
	for (i, elem) in enumerate(s):
		if elem in ['Q', 'A']:
			s1.append(elem)
			if elem == 'Q':
				if s2 == []:
					s2.append(1)
					l += 1
				else:
					s2.append(s2[l - 1] + 1)
					l += 1
			elif s2 == []:
				s2.append(0)
				l += 1
			else:
				s2.append(s2[l - 1])
				l += 1
	s = s1
	res = 0
	for i in range(len(s)):
		if s[i] == 'A':
			res += s2[i] * (s2[len(s) - 1] - s2[i])
	print(res)


inp = input()
ans = 0
for i in range(len(inp) - 2):
	for j in range(i + 1, len(inp) - 1):
		for k in range(j + 1, len(inp)):
			if inp[i] == 'Q' and inp[j] == 'A' and (inp[k] == 'Q'):
				ans += 1
print(ans)


import sys
s = input()

def num_subsequences(seq, sub):
	if not sub:
		return 1
	elif not seq:
		return 0
	result = num_subsequences(seq[1:], sub)
	if seq[0] == sub[0]:
		result += num_subsequences(seq[1:], sub[1:])
	return result
print(num_subsequences(s, 'QAQ'))


def ss(arr, n, key):
	left = 0
	right = n
	mid = 0
	while left < right:
		mid = (right + left) // 2
		if arr[mid] == key:
			return arr[mid]
		elif arr[mid] > key:
			right = mid
		else:
			left = mid + 1
	while mid > -1 and arr[mid] > key:
		mid -= 1
	return arr[mid]
from collections import defaultdict
al = defaultdict(int)
Q = []
A = []
ans = 0
s = input()
for i in range(len(s)):
	if s[i] == 'Q':
		Q.append(i)
	elif s[i] == 'A':
		A.append(i)
if len(Q) != 0:
	al[Q[0]] = 1
	for i in range(1, len(Q)):
		al[Q[i]] = al[Q[i - 1]] + 1
	total = al[Q[-1]]
for i in range(len(A)):
	if len(Q) != 0:
		x = ss(Q, len(Q), A[i])
		ans = ans + al[x] * (total - al[x])
print(ans)


s = input()
sum = 0
for i in s:
	if i != 'Q' and i != 'A':
		s = s.replace(i, '')
while s.find('A') != -1:
	sum += s[:s.find('A')].count('Q') * s[s.find('A'):len(s)].count('Q')
	s = s.replace('A', '', 1)
print(sum)


s = input()
pairs = []
i = s.find('Q')
while i != -1:
	pairs.append(i)
	i = s.find('Q', i + 1)
sum = 0
for i in range(len(pairs) - 1):
	start = pairs[i]
	for j in range(i + 1, len(pairs)):
		end = pairs[j]
		if s.find('A', start, end):
			sum += s.count('A', start, end)
print(sum)


n = input()
(q, a) = ([], [])
for (i, letter) in enumerate(n):
	if letter == 'Q':
		q.append(i)
	elif letter == 'A':
		a.append(i)
res = 0
for k in a:
	j = 0
	while j < len(q) and q[j] < k:
		j += 1
	res += j * (len(q) - j)
print(res)


def qaq_count(word):
	count = 0
	for i in range(len(word)):
		for j in range(i + 1, len(word)):
			for k in range(j + 1, len(word)):
				if word[i] == 'Q' and word[j] == 'A' and (word[k] == 'Q'):
					count += 1
	return count
print(qaq_count(input()))


a = input()
b = a.count('Q')
cnt = 0
c = 0
for i in a:
	if i == 'Q':
		b = b - 1
		cnt += 1
	elif i == 'A':
		c += b * cnt
print(c)


s = input()
cntQ = 0
for i in range(len(s)):
	if s[i] == 'Q':
		cntQ += 1
sn = 0
cQ = 0
for i in range(len(s)):
	if s[i] == 'A':
		sn += cQ * (cntQ - cQ)
	elif s[i] == 'Q':
		cQ += 1
print(sn)


txt = input()
(Cst, Cmid, Cend) = (0, 0, 0)
for i in range(len(txt)):
	if txt[i] == 'Q':
		Cst += 1
		Cend += Cmid
	if txt[i] == 'A':
		Cmid += Cst
print(Cend)


s = input()
count = 0
for (i, letter) in enumerate(s):
	if letter == 'A':
		count += s[:i].count('Q') * s[i + 1:].count('Q')
print(str(count))


def qaq(values):
	count = 0
	for i in range(len(values)):
		if values[i] == 'Q':
			for j in range(i + 1, len(values)):
				if values[j] == 'A':
					for k in range(j + 1, len(values)):
						if values[k] == 'Q':
							count = count + 1
	return count
values = input()
print(qaq(values))


def count(a, b):
	(m, n) = (len(a), len(b))
	k = [[0] * (n + 1) for i in range(m + 1)]
	for i in range(n + 1):
		k[0][i] = 0
	for i in range(m + 1):
		k[i][0] = 1
	for i in range(1, m + 1):
		for j in range(1, n + 1):
			if a[i - 1] == b[j - 1]:
				k[i][j] = k[i - 1][j - 1] + k[i - 1][j]
			else:
				k[i][j] = k[i - 1][j]
	return k[m][n]
a = input()
print(count(a, 'QAQ'))


import sys
from random import choice, randint
inp = sys.stdin.readline
out = sys.stdout.write
flsh = sys.stdout.flush
sys.setrecursionlimit(10 ** 9)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def MI():
	return map(int, inp().strip().split())

def LI():
	return list(map(int, inp().strip().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines().strip()]

def LI_():
	return [int(x) - 1 for x in inp().strip().split()]

def LF():
	return [float(x) for x in inp().strip().split()]

def LS():
	return inp().strip().split()

def I():
	return int(inp().strip())

def F():
	return float(inp().strip())

def S():
	return inp().strip()

def pf(s):
	return out(s + '\n')

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	a = S()
	b = 'QAQ'
	m = len(a)
	l = [[0] * 4] * (m + 1)
	for i in range(4):
		l[0][i] = 0
	for i in range(m + 1):
		l[i][0] = 1
	for i in range(1, m + 1):
		for j in range(1, 4):
			if a[i - 1] == b[j - 1]:
				l[i][j] = l[i - 1][j - 1] + l[i - 1][j]
			else:
				l[i][j] = l[i - 1][j]
	pf(str(l[m][3]))
main()


word = input()
l = len(word)
count = 0
countQ = 0
for i in reversed(range(2, l)):
	if not (word[i] == 'Q' or word[i] == 'A'):
		word = word[:i] + word[i + 1:]
l = len(word)
for i in range(l):
	if word[i] == 'Q':
		countQ += 1
tempQ = 0
for i in range(l):
	if word[i] == 'Q':
		tempQ += 1
	if word[i] == 'A':
		count += tempQ * (countQ - tempQ)
print(count)


(line, qs, pos, total) = (input(), 0, 0, 0)
for l in line:
	if l == 'A':
		pos += qs
	elif l == 'Q':
		qs += 1
		total += pos
print(total)


from itertools import combinations
n = input()
z = combinations(n, 3)
y = sum(map(lambda x: x == ('Q', 'A', 'Q'), z))
print(y)


s = input()
k = 0
for x in range(0, len(s) - 2):
	for y in range(x + 1, len(s) - 1):
		for z in range(y + 1, len(s)):
			if s[x] == 'Q' and s[y] == 'A' and (s[z] == 'Q'):
				k += 1
print(k)


a = str(input())
line = []
com = []
cn = []
s = 0
d = 0
e = 0
for let in list(a):
	if let == 'A':
		line.append(let)
	elif let == 'Q':
		line.append(let)
for i in range(len(line)):
	if line[i] == 'Q':
		com.append(i)
for c in range(len(line)):
	if line[c] == 'A':
		cn.append(c)
for c in cn:
	x = c
	for i in com:
		if x >= i:
			s = s + 1
		else:
			d = d + 1
	e = e + s * d
	s = 0
	d = 0
print(e)


m = list(map(str, input()))
k = 0
for x in range(len(m)):
	for y in range(x, len(m)):
		for z in range(y, len(m)):
			if m[x] == 'Q':
				if m[y] == 'A':
					if m[z] == 'Q':
						k += 1
print(k)


from sys import stdin, stdout

def main():
	line = stdin.readline()
	cont = 0
	i = 0
	while i < len(line):
		if line[i] == 'Q':
			j = i + 1
			while j < len(line):
				if line[j] == 'A':
					k = j + 1
					while k < len(line):
						if line[k] == 'Q':
							cont += 1
						k += 1
				j += 1
		i += 1
	stdout.write(str(cont))
main()


s = input()
q = 0
qs = 0
ans = 0
for i in s:
	qs += int(i is 'Q')
c = 0
for i in range(len(s)):
	c += int(s[i] is 'Q')
	if s[i] is 'A':
		ans += c * (qs - c)
print(ans)