#include <bits/stdc++.h>
#define arr array 
#define pb push_back
#define fs first 
#define sc second
#define eb emplace_back
#define vt vector
#define all(x) (x).begin(), (x).end()
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

using namespace std;
typedef long long ll;
const int maxn=1e5;
const ll MAXLL=9223372036854775807;

void solve(){
    int N;
    cin >> N;

    auto solve_pair=[&](string a, string b) -> int{
        int len=(int)a.size();
        int pref=0, suff=0;
        while(a[pref]==b[pref]) pref++;
        while(a[len-1-suff]==b[len-1-suff]) suff++;
        return (is_sorted(a.begin()+pref, a.end()-suff)||is_sorted(b.begin()+pref, b.end()-suff))?1:2;
    };

    auto solve=[&](vt<string> group) -> long long{
        int n=(int)group.size();
        int len=(int)group[0].size();

        ll res=0;
        if (N<=4000){
            rep(i, 0, n)
                rep(j, i+1, n){
                    int tmp=solve_pair(group[i], group[j]);
                    res+=tmp;
                }
            return res;
        }

        res=2*n*(n-1ll)/2;
        sort(all(group));
        rep(i, 0, n){
            string s=group[i];
            for (int i=0; i<len; i++)
                for (int j=i+2; j<=len; j++){
                    string other=s;
                    sort(other.begin()+i, other.begin()+j);
                    res-=(other[i]!=s[i])&&(other[j-1]!=s[j-1])&&(binary_search(group.begin(), group.end(), other));
                }
        }
        return res;
    };

    
    map<string, vt<string>> mp;
    rep(i, 0, N){
        string s;
        cin >> s;
        string sorted=s;
        sort(all(sorted));
        mp[sorted].pb(s);
    }   
    ll ans= 1337*N*(N-1ll)/2;
    for (auto gr:mp){
        vt<string> v=gr.second;
        int n=(int)v.size();
        ans-=1337*n*(n-1ll)/2;
        ans+=solve(v);
    }
    cout << ans << endl;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    solve();
}
