#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } using mint = atcoder::modint998244353; ostream& operator<<(ostream& os, const mint& m){ os << m.val(); return os; } mint pow(mint a, ll n) { assert(n >= 0); mint ans = 1; while (n > 0) { if (n&1) ans = ans*a; a = a*a; n >>= 1; } return ans; } /** * f(x) = g(x)*h(x) + r(x)となる(h(x), r(x))を求めます */ template pair, vector> naive_divide(vector f, vector g){ int n = f.size()-1; int m = g.size()-1; if(n < m) return make_pair(vector(1, T(0)), f); int k = n-m; vector h(k+1); T iv = T(1)/g[m]; for(int i = k; i >= 0; i--){ h[i] = f[i+m]*iv; for(int j = 0; j <= m; j++){ f[i+j] -= g[j]*h[i]; } } return make_pair(h, f); } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n; cin >> n; string s; cin >> s; auto dp = vec2d(n+1, 3, mint(0)); dp[0][0] = 1; vector cnt = {4, 3, 3}; mint ans = 0; for(int i = 0; i < n; i++){ if(s[i] == '?'){ for(int j = 0; j < 3; j++){ for(int k = 0; k < 3; k++){ dp[i+1][(j+k)%3] += dp[i][j]*cnt[k]; } } }else{ int r = (s[i]-'0')%3; for(int j = 0; j < 3; j++){ dp[i+1][(j+r)%3] += dp[i][j]; } } ans += dp[i+1][0]; dp[i+1][0] += 1; } cout << ans << endl; }