#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int nos = 0, ql = 0, qr = 0;
  int i = 0;
  while (i < s.length() && s[i] != 'A') {
    if (s[i] == 'Q') ql++;
    i++;
  }
  int j = i;
  for (; i < s.length(); i++) {
    if (s[i] == 'Q') qr++;
  }
  i = j;
  for (; i < s.length(); i += 1) {
    if (s[i] == 'A') {
      nos += ql * qr;
    }
    if (s[i] == 'Q') {
      ql++;
      qr--;
    }
  }
  cout << nos << endl;
  return 0;
}
