#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; 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; for (int l=0; l<26; l++){ bool x = (l == S[j%L]-'a'), y = (l == T[k%L]-'a'); if (j+x<=K && k+y<=K) pd[j+x][k+y] += dp[j][k] * p[l]; } } } 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; }