#include using namespace std; #include using namespace atcoder; using mint = modint998244353; using vm = vector; using vvm = vector; inline ostream &operator<<(ostream &os, const mint x) { return os << x.val(); }; inline istream &operator>>(istream &is, mint &x) { long long v; is >> v; x = v; return is; }; struct Fast { Fast() { std::cin.tie(nullptr); ios::sync_with_stdio(false); cout << setprecision(10); } } fast; #define all(a) (a).begin(), (a).end() #define contains(a, x) ((a).find(x) != (a).end()) #define rep(i, a, b) for (int i = (a); i < (int)(b); i++) #define rrep(i, a, b) for (int i = (int)(b) - 1; i >= (a); i--) #define YN(b) cout << ((b) ? "YES" : "NO") << "\n"; #define Yn(b) cout << ((b) ? "Yes" : "No") << "\n"; #define yn(b) cout << ((b) ? "yes" : "no") << "\n"; template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } using ll = long long; using vb = vector; using vvb = vector; using vi = vector; using vvi = vector; using vl = vector; using vvl = vector; template ostream &operator<<(ostream &os, pair &p) { os << "(" << p.first << "," << p.second << ")"; return os; } template ostream &operator<<(ostream &os, vector &vec) { for (int i = 0; i < (int)vec.size(); i++) { os << vec[i] << (i + 1 == (int)vec.size() ? "" : " "); } return os; } template istream &operator>>(istream &is, vector &vec) { for (int i = 0; i < (int)vec.size(); i++) is >> vec[i]; return is; } ll floor(ll a, ll b) { return a >= 0 ? a / b : (a + 1) / b - 1; } ll ceil(ll a, ll b) { return a > 0 ? (a - 1) / b + 1 : a / b; } void solve() { int l, k; cin >> l >> k; string s, t; cin >> s >> t; vi a(26); cin >> a; int sum = 0; for (auto v : a) sum += v; mint q = mint(sum).inv(); vvm dp_leq(l, vm(l, 0)), dp_gt(l, vm(l, 0)); dp_leq[0][0] = 1; mint ans_l = 0, ans_r = 0; rep(loop, 0, k) { vvm ndp_leq(l, vm(l, 0)), ndp_gt(l, vm(l, 0)); rep(u, 0, 26) { mint p = a[u] * q; rep(i, 0, l) rep(j, 0, l) { { int x = i, y = j; if (x > y) y += l; if (s[i] - 'a' == u) x++; if (t[j] - 'a' == u) y++; mint w = dp_leq[i][j] * p; if (x - y >= l) ans_l += w; else if (y - x >= l) ans_r += w; else { if (x <= y) ndp_leq[x % l][y % l] += w; else ndp_gt[x % l][y % l] += w; } } { int x = i, y = j; if (x <= y) x += l; if (s[i] - 'a' == u) x++; if (t[j] - 'a' == u) y++; mint w = dp_gt[i][j] * p; if (x - y >= l) ans_l += w; else if (y - x >= l) ans_r += w; else { if (x <= y) ndp_leq[x % l][y % l] += w; else ndp_gt[x % l][y % l] += w; } } } } dp_leq = ndp_leq; dp_gt = ndp_gt; } cout << ans_l << " " << ans_r << "\n"; } int main() { int t = 1; // cin >> t; while (t--) solve(); }