#include #include #define rep(i, n) for (int i = 0; i < n; i++) #define ll long long using namespace atcoder; using namespace std; #define mint modint998244353 int main() { int n; cin >> n; int nn = n << 1; vector two(nn + 1); two[0] = 1; for (int i = 1; i <= nn; i++) two[i] = 2 * two[i - 1]; vector f(n + 1), g(n + 1); for (int i = 1; i <= n; i++) { f[i] = (i + 3) * two[i] + (i - 3) * two[i << 1]; g[i] = (i - 2) * two[i] + 2; } string u, v; cin >> u >> v; if (u == v) { cout << f[n].val() << endl; return 0; } int lca = 0; for (int i = 0, maxx = min(u.length(), v.length()); i < maxx; i++) { if (u[i] != v[i]) break; lca++; } const int m = u.length() + v.length() - 2 * lca + 1; vector p(m); p[0] = u.length(); int cnt = 0; while (p[cnt] != lca) { p[cnt + 1] = p[cnt] - 1; cnt++; } const int lca_cnt = cnt; while (cnt != m - 1) { p[cnt + 1] = p[cnt] + 1; cnt++; } for (int i = 0; i < m; i++) p[i]--; vector a(m), b(m), c(m); a[0] = two[n - p[0]] - 1; a[m - 1] = two[n - p[m - 1]] - 1; a[lca_cnt] = 1; for (int k = 1, d = p[lca_cnt]; k <= d; k++) a[lca_cnt] += two[n - k]; if (lca_cnt == 0 || lca_cnt == m - 1) a[lca_cnt] += two[n - p[lca_cnt] - 1] - 1; for (int i = 1; i < m - 1; i++) { if (i == lca_cnt) continue; a[i] = two[n - 1 - p[i]]; } b[0] = g[n - p[0]]; b[m - 1] = g[n - p[m - 1]]; b[lca_cnt] = 0; for (int k = 1, d = p[lca_cnt]; k <= d; k++) b[lca_cnt] += g[n - k] + (two[n - k] - 1) * (d + 2 - k) + k; if (lca_cnt == 0 || lca_cnt == m - 1) b[lca_cnt] += g[n - p[lca_cnt] - 1] + two[n - p[lca_cnt] - 1] - 1; for (int i = 1; i < m - 1; i++) { if (i == lca_cnt) continue; b[i] = g[n - 1 - p[i]] + two[n - 1 - p[i]] - 1; } c[0] = f[n - p[0]]; c[m - 1] = f[n - p[m - 1]]; c[lca_cnt] = 0; for (int k = 1, d = p[lca_cnt]; k <= d; k++) { c[lca_cnt] += f[n - k]; c[lca_cnt] += g[n - k] * (a[lca_cnt] - two[n - k] + 1); c[lca_cnt] += (two[n - k] - 1) * (a[lca_cnt] - two[n - k] + 1); c[lca_cnt] += (two[n] - two[n - k]) * (a[lca_cnt] - two[n] + two[n - k]); } if (lca_cnt == 0 || lca_cnt == m - 1) { c[lca_cnt] += f[n - p[lca_cnt] - 1]; c[lca_cnt] += g[n - p[lca_cnt] - 1] * (a[lca_cnt] - two[n - p[lca_cnt] - 1] + 1); c[lca_cnt] += (two[n - p[lca_cnt] - 1] - 1) * (a[lca_cnt] - two[n - p[lca_cnt] - 1] + 1); } for (int i = 1; i < m - 1; i++) { if (i == lca_cnt) continue; c[i] = f[n - 1 - p[i]] + g[n - 1 - p[i]] + two[n - 1 - p[i]] - 1; } mint sum_a = 0, sum_b = 0, sum_c = 0, sum_ab = 0; for (mint i : a) sum_a += i; for (mint i : b) sum_b += i; for (mint i : c) sum_c += i; for (int i = 0; i < m; i++) sum_ab += a[i] * b[i]; mint ans = sum_c + sum_a * sum_b - sum_ab; vector aa(m); for (int i = 0; i < m; i++) aa[m - 1 - i] = a[i]; vector tt = convolution(a, aa); mint sum = 0; for (int j = 1; j < m; j++) sum += min(j, m - j) * (tt[m - 1 - j] + tt[2 * m - 1 - j]); ans += sum / 2; cout << ans.val() << endl; return 0; }