#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

template<unsigned P> struct ModInt {
  using M = ModInt;
  unsigned v;
  ModInt() : v(0) {}
  ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
  constexpr ModInt(unsigned v, int) : v(v) {}
  static constexpr unsigned p() { return P; }
  M operator+() const { return *this; }
  M operator-() const { return {v ? P - v : 0, 0}; }
  explicit operator bool() const noexcept { return v; }
  bool operator!() const noexcept { return !(bool)*this; }
  M operator*(M r) const { return M(*this) *= r; }
  M operator/(M r) const { return M(*this) /= r; }
  M operator+(M r) const { return M(*this) += r; }
  M operator-(M r) const { return M(*this) -= r; }
  bool operator==(M r) const { return v == r.v; }
  bool operator!=(M r) const { return !(*this == r); }
  M& operator*=(M r) { v = (uint64_t)v * r.v % P; return *this; }
  M& operator/=(M r) { return *this *= r.inv(); }
  M& operator+=(M r) { if ((v += r.v) >= P) v -= P; return *this; }
  M& operator-=(M r) { if ((v += P - r.v) >= P) v -= P; return *this; }
  M inv() const {
    int a = v, b = P, x = 1, u = 0;
    while (b) {
      int q = a / b;
      swap(a -= q * b, b);
      swap(x -= q * u, u);
    }
    assert(a == 1);
    return x;
  }
  M pow(auto n) const {
    if (n < 0) return pow(-n).inv();
    M res = 1;
    for (M a = *this; n; a *= a, n >>= 1) if (n & 1) res *= a;
    return res;
  }
  friend M operator*(auto l, M r) { return M(l) *= r; }
  friend M operator/(auto l, M r) { return M(l) /= r; }
  friend M operator+(auto l, M r) { return M(l) += r; }
  friend M operator-(auto l, M r) { return M(l) -= r; }
  friend ostream& operator<<(ostream& os, M r) { return os << r.v; }
  friend istream& operator>>(istream& is, M& r) { lint x; is >> x; r = x; return is; }
  friend bool operator==(auto l, M r) { return M(l) == r; }
  friend bool operator!=(auto l, M r) { return !(l == r); }
};
using Mint = ModInt<998244353>;

template<unsigned P, unsigned g> void ntt(V< ModInt<P> >& a, bool inv = false) {
  int n = a.size();
  int j = 0;
  for (int i = 1; i < n; ++i) {
    int w = n >> 1;
    while (j >= w) j -= w, w >>= 1;
    j += w;
    if (i < j) swap(a[i], a[j]);
  }
  assert((P - 1) % n == 0);
  auto xi = ModInt<P>(g).pow((P - 1) / n);
  if (inv) xi = xi.inv();
  for (int k = 1; k < n; k <<= 1) {
    auto dt = xi.pow((n >> 1) / k);
    for (int i0 = 0; i0 < n; i0 += k << 1) {
      ModInt<P> t = 1;
      for (int i = i0; i < i0 + k; ++i) {
        j = i + k;
        a[j] *= t, t *= dt;
        tie(a[i], a[j]) = make_pair(a[i] + a[j], a[i] - a[j]);
      }
    }
  }
}
template<unsigned P, unsigned g = 6420> void multiply(V< ModInt<P> >& a, V< ModInt<P> >& b) {
  if (a.empty() or b.empty()) {
    a = {};
    return;
  }
  int sz = a.size() + b.size() - 1, n = 1 << __lg(2 * sz - 1);
  if (min(a.size(), b.size()) < 100) {
    V< ModInt<P> > c(sz);
    for (int i = 0; i < (int)a.size(); ++i) {
      for (int j = 0; j < (int)b.size(); ++j) {
        c[i + j] += a[i] * b[j];
      }
    }
    a = c;
    return;
  }
  a.resize(n), b.resize(n);
  ntt<P, g>(a), ntt<P, g>(b);
  for (int i = 0; i < n; ++i) a[i] *= b[i];
  ntt<P, g>(a, true);
  a.resize(sz);
  auto inv_n = ModInt<P>(n).inv();
  for (auto&& e : a) e *= inv_n;
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  string s; cin >> s;
  int n = s.size();
  V<Mint> c(n);
  auto rec = [&](const auto& rec, int L, int R) -> void {
    if (R - L + 1 < 3) return;
    int M = L + R >> 1;
    rec(rec, L, M);
    rec(rec, M + 1, R);
    V<Mint> a, b;
    for (int i = L; i <= R; ++i) {
      if (i <= M) a.push_back(s[i] == 'U' ? 1 : s[i] == '?' ? Mint(3).inv() : 0);
      else b.push_back(s[i] == 'G' ? 1 : s[i] == '?' ? Mint(3).inv() : 0);
    }
    multiply(a, b);
    for (int i = 0; i < (int)a.size(); ++i) {
      if (L + M + 1 + i & 1) continue;
      c[L + M + 1 + i >> 1] += a[i];
    }
  };
  rec(rec, 0, n - 1);
  Mint res;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'M') res += c[i];
    if (s[i] == '?') res += c[i] / 3;
  }
  res *= Mint(3).pow(count(begin(s), end(s), '?'));
  cout << res << '\n';
}


Measured memory_bytes: 6240