MOD = 998244353 def main(): import sys input = sys.stdin.read().split() N = int(input[0]) S = input[1] cnt = [4, 3, 3] # cnt[0] = 4 digits (0,3,6,9), cnt[1] = 3 (1,4,7), cnt[2] = 3 (2,5,8) prev_dp = [0, 0, 0] total = 0 for c in S: current_dp = [0, 0, 0] if c == '?': # For each previous remainder, distribute to new remainders based on t for r_prev in range(3): for t in range(3): new_r = (r_prev + t) % 3 current_dp[new_r] = (current_dp[new_r] + prev_dp[r_prev] * cnt[t]) % MOD # Add the single character case: all possible digits for r in range(3): current_dp[r] = (current_dp[r] + cnt[r]) % MOD else: d = int(c) for r_prev in range(3): new_r = (r_prev + d) % 3 current_dp[new_r] = (current_dp[new_r] + prev_dp[r_prev]) % MOD # Add single character case: d mod 3 new_r = d % 3 current_dp[new_r] = (current_dp[new_r] + 1) % MOD # Add the number of valid substrings ending at this position to the total total = (total + current_dp[0]) % MOD # Update previous DP prev_dp = current_dp.copy() print(total % MOD) if __name__ == '__main__': main()