#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll MOD = 998244353;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

#define PI 3.141592653589793238462643383279 
typedef pair<double, double> com;
struct FFT{ //PIをdefineしておく, complex<double> com　をtypedefしておく
    int n;
    vector<com> a, b;
    vector<com> f[2];
    void init(vector<int>& p, vector<int>& q, int x){
        n = 1;
        while(n < x) n *= 2;
        a.resize(n, com(0,0));
        b.resize(n, com(0,0));
        rep(i,2) f[i].resize(n, com(0,0));
        //rep(i,n) a.push_back(com(0,0));
        //rep(i,n) b.push_back(com(0,0));
        //rep(i,2) rep(j,n) f[i].push_back(com(0,0));
        rep(i,p.size()) a[i] = com(p[i],0);
        rep(i,q.size()) b[i] = com(q[i],0);
    }

    inline com com_add (com a, com b) {
        return com(a.first+b.first, a.second+b.second);
    }

    inline com com_mul (com a, com b) {
        return com(a.first*b.first-a.second*b.second, a.first*b.second+a.second*b.first);
    }
    
    vector<com> dft(vector<com>& v, bool rev){
        int x = n, u = 0;
        f[0] = v;
        int ho = 0, ge = 1;
        for(int i = 2; i <= n; i <<= 1){
            u++; x >>= 1;
            com zeta;
            if(!rev) zeta = com(cos(2*PI/i),sin(2*PI/i));
            else zeta = com(cos(2*PI/i),-sin(2*PI/i));
            com pz;
            rep(j,x){
                pz = com(1,0);
                rep(k,i){
                    if(k>=i/2) f[ge][j*i+k] = com_add(f[ho][j*(i/2)+k-i/2],com_mul(pz,f[ho][j*(i/2)+n/2+k-i/2]));
                    else f[ge][j*i+k] = com_add(f[ho][j*(i/2)+k],com_mul(pz,f[ho][j*(i/2)+n/2+k]));
                    pz = com_mul(pz, zeta);
                }
            }
            swap(ho,ge);
        }
        return f[ho];
    }
    
    vector<com> multiply(vector<int>& p, vector<int>& q, int x){ //ｘは要素数の合計
        a.clear(); b.clear(); f[0].clear(); f[1].clear();
        init(p,q,x);
        a = dft(a,0);
        b = dft(b,0);
        rep(i,n) a[i] = com_mul(a[i],b[i]);
        a = dft(a,1);
        rep(i,n) a[i] = com(a[i].first/n, a[i].second/n);
        return a;
    }
};

string s;
ll cnt = 0;
vector<int> U, G;
ll p3[200001];
ll ans;
ll R3;
FFT fft;

void solve (int st, int en) {
    if (en-st <= 2) return;
    int mid = (st + en) / 2;
    vector<int> u, g;
    for (int i = st; i < mid; i++) u.push_back(U[i]);
    for (int i = mid; i < en; i++) g.push_back(G[i]);
    vector<com> rec = fft.multiply(u, g, en-st);
    rep(i,rec.size()) {
        ll val = ll(round(rec[i].first));
        if ((st+mid+i)%2 == 0) {
            if (s[(st+mid+i)/2] == 'M') ans = (ans + val*3) % MOD;
            if (s[(st+mid+i)/2] == '?') ans = (ans + val) % MOD;
        }
    }
    solve(st, mid); solve(mid, en);
}

ll mod_pow (ll x, ll y) {
    ll ret = 1;
    while (y) {
        if (y&1) ret = ret * x % MOD;
        x = x * x % MOD;
        y /= 2;
    }
    return ret;
}

int main() {
    cin >> s;
    R3 = mod_pow(3, MOD-2);
    U.resize(s.size()); G.resize(s.size());
    rep(i,s.size()) {
        if (s[i] == 'U') U[i] = 3;
        if (s[i] == 'G') G[i] = 3;
        if (s[i] == '?') { U[i] = 1; G[i] = 1; }
    }
    solve(0, s.size());
    ans = ans * mod_pow(27, MOD-2) % MOD;
    rep(i,s.size()) {
        if (s[i] == '?') ans = ans * 3 % MOD;
    }
    cout << ans << endl;
}



Measured memory_bytes: 31324