#include #include using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); /* dp(i,j,k) = i回目でP_N=j, P_M=k(|k-j|<=L)である確率 */ ll L, K, x, c, d; mint ans1=0, ans2=0, sm=0; string S, T; cin >> L >> K >> S >> T; vector p(26); for (int i=0; i<26; i++){ cin >> x; p[i] = x; sm += p[i]; } sm = sm.inv(); for (int i=0; i<26; i++) p[i] *= sm; vector dp(K+1, vector(K+1)); dp[0][0] = 1; for (int i=0; i(K+1)); for (int j=0; j<=K; j++){ for (int k=0; k<=K; k++){ if (abs(j-k)>=L) continue; c = S[j%L]-'a'; d = T[k%L]-'a'; if (c == d){ if (j+1<=K && k+1<=K) pd[j+1][k+1] += dp[j][k] * p[c]; pd[j][k] += dp[j][k] * (-p[c]+1); } else{ if (j+1<=K) pd[j+1][k] += dp[j][k] * p[c]; if (k+1<=K) pd[j][k+1] += dp[j][k] * p[d]; pd[j][k] += dp[j][k] * (-p[c]-p[d]+1); } } } swap(dp, pd); for (int j=0; j<=K; j++){ if (j-L>=0){ ans1 += dp[j][j-L]; ans2 += dp[j-L][j]; } } } cout << ans1.val() << " " << ans2.val() << endl; return 0; }