#include <bits/stdc++.h>
// mrrrow meeow :3
// go play vivid/stasis now! it's free on steam
#define fo(i, a, b) for (auto i = (a); i < (b); i++)
#define of(i, a, b) for (auto i = (b); i-- > (a);)
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define be(a) a.begin(), a.end()
using namespace std;
int ____init = [] {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
int main() {
map<int, map<char, map<char, set<string>>>> data;
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
data[s.size()][s[0]][s[s.size() - 1]].insert(s);
reverse(be(s));
data[s.size()][s[0]][s[s.size() - 1]].insert(s);
}
long long res = 0;
for (auto [_, m] : data) {
map<tuple<char, char, char, char>, long long> lm;
for (auto [a, bs] : m) {
for (auto [b, bss] : bs) {
long long n1 = bss.size();
for (auto [c, css] : m[b]) {
long long n2 = (n1 * css.size()) % 998244353;
for (auto [d, dss] : m[c]) {
long long n3 = (n2 * css.size()) % 998244353;
if (m[d].count(a)) lm[make_tuple(a, b, c, d)] = (n3 * m[d][a].size());
}
}
}
}
for (auto [s1, n1] : lm) {
for (auto [s2, n2] : lm) {
if (m[get<0>(s1)].count(get<0>(s2)) && m[get<1>(s1)].count(get<1>(s2)) && m[get<2>(s1)].count(get<2>(s2)) &&
m[get<3>(s1)].count(get<3>(s2))) {
res = (res + n1 * n2 % 998244353 * m[get<0>(s1)][get<0>(s2)].size() % 998244353 * m[get<1>(s1)][get<1>(s2)].size() %
998244353 * m[get<2>(s1)][get<2>(s2)].size() % 998244353 * m[get<3>(s1)][get<3>(s2)].size() %
998244353) %
998244353;
}
}
}
}
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |