MOD = 998244353 def main(): import sys input = sys.stdin.read().split() N = int(input[0]) S = input[1] # Precompute dp[k][t] where t is 0,1,2 max_k = N dp = [[0]*3 for _ in range(max_k+1)] dp[0][0] = 1 for k in range(1, max_k+1): for t in range(3): total = 0 for s in range(3): cnt = 4 if s == 0 else 3 prev_t = (t - s) %3 total += dp[k-1][prev_t] * cnt total %= MOD dp[k][t] = total # Precompute prefix sum mod3 and prefix count of '?' prefix_s = [0]*(N+1) prefix_q = [0]*(N+1) for i in range(1, N+1): c = S[i-1] if c == '?': prefix_q[i] = prefix_q[i-1] + 1 prefix_s[i] = prefix_s[i-1] else: d = int(c) prefix_s[i] = (prefix_s[i-1] + d) %3 prefix_q[i] = prefix_q[i-1] # Now, use a dictionary to track (s, q) counts from collections import defaultdict total = 0 counter = defaultdict(int) counter[(0, 0)] = 1 # initial state: prefix 0, s=0, q=0 for i in range(1, N+1): current_s = prefix_s[i] current_q = prefix_q[i] # Compute contribution for all possible s_prev and q_prev for s_prev in range(3): for q_prev in range(current_q +1): t = (3 - (current_s - s_prev) %3 ) %3 k = current_q - q_prev if k <0: continue if k > max_k: continue # The number of substrings ending at i with s_sub and k '?' cnt = counter.get( (s_prev, q_prev), 0 ) if cnt ==0: continue contribution = (dp[k][t] * cnt) % MOD total = (total + contribution) % MOD # Update the counter with current_s and current_q key = (current_s, current_q) counter[key] = (counter.get(key, 0) + 1) % MOD print(total % MOD) if __name__ == '__main__': main()