#include<bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (n); i++)
#define forab(i, a, b) for (int i = (a); i <= (b); i++)
#define forba(i, b, a) for (int i = (b); i >= (a); i--)
#define mset(a, x, n) memset(a, x, sizeof(a[0]) * (n))
#define updiv(x, y) (((x) + (y) - 1) / (y)) // y > 0
#define pb(x) push_back(x)
#define eb emplace_back
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#ifdef hat
#define fast
#define de(x) cerr  << #x << '=' << (x) << ' '
#define dee(x) cerr  << #x << '=' << (x) << endl
#else
#define endl '\n'
#define fast ios::sync_with_stdio(0), cin.tie(0)
#define de(x) ((void) 0)
#define dee(x) ((void) 0)
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> VI;

const int maxn = 3e5 + 5;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const ll llINF = 0x3f3f3f3f3f3f3f3f;
ll make_compiler_happy() {return INF & maxn & mod & llINF;}
ll fpow(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll inv(ll x) {return fpow(x, mod-2);}
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());

struct Trie
{
    vector<array<int, 26>> ch;
    vector<vector<int>> id;

    Trie() {ch.push_back({}); id.push_back({});}
    vector<int> rev_insert(const string &s, int x)
    {
        int n = sz(s);
        int u = 0;
        vector<int> pos;
        id[u].pb(x);
        pos.pb(u);
        forba(i, n - 1, 0)
        {
            int v = s[i] - 'a';
            if(!ch[u][v]) {
                ch[u][v] = sz(ch);
                ch.push_back({});
                id.push_back({});
            }
            u = ch[u][v];
            id[u].pb(x);
            pos.pb(u);
        }
        reverse(all(pos));
        return pos;
    }
    int query(int p, int l, int r)
    {
        return lower_bound(all(id[p]), r) - lower_bound(all(id[p]), l);
    }
};

int main()
{
    fast;
    int n;
    cin >> n;
    vector<string> s(n);
    map<string, vector<string>> mp;
    forn(i, n)
    {
        cin >> s[i];
        string t = s[i];
        sort(all(t));
        mp[t].push_back(s[i]);
    }
    int m = sz(s[0]);
    ll ans = 0, sumg = 0;
    for(auto &t : mp)
    {
        dee(t.fi);
        auto &g = t.se;
        ans += (ll)sz(g) * (sz(g) - 1);
        ans += sumg * sz(g) * 1337;
        sumg += sz(g);
        sort(all(g));
        Trie trie;
        vector<VI> pos(sz(g));
        forn(i, sz(g))
        {
            pos[i] = trie.rev_insert(g[i], i);
        }
        vector<int> lcp(sz(g));
        forab(i, 1, sz(g) - 1)
        {
            forn(j, m)
            {
                if(g[i][j] == g[i - 1][j]) lcp[i]++;
                else break;
            }
        }
        vector<pii> stk;
        stk.eb(sz(g), -1);
        forba(i, sz(g) - 1, 0)
        {
            vector<int> dec;
            forn(j, m)
            {
                if(!j || g[i][j] < g[i][j - 1]) dec.pb(j);
            }
            dec.pb(m);
            forab(j, 1, sz(stk) - 1)
            {
                auto tt = upper_bound(all(dec), stk[j].se);
                int r = *tt;
                ans -= trie.query(pos[i][r], stk[j].fi, stk[j - 1].fi);
            }
            while(stk.back().se >= lcp[i]) stk.pop_back();
            stk.eb(i, lcp[i]);
        }
    }
    cout << ans << endl;
}