#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
 
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
 
const int N = 200005;
const int SQ = 111;
 
int n, cnt_all[27][3 * N], cnt_sorted[27][3 * N];
ll p[N], base = 137, mod = 29996224275833;
vector <string> a;
vector <vector <int> > sor;
vector <vector <ll> > h[2];
 
bool is_sorted(int idx, int l, int r){
    r = min(r, (int)a[0].size() - 1);
    l = max(l, 0);
    if(l == r) return 1;
    l++;
    return sor[idx][r] - sor[idx][l - 1] == r - l + 1;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    p[0] = 1;
    for(int i = 1 ; i < N ; i++){
        p[i] = 1LL * p[i - 1] * base % mod;
    }
    cin >> n;
    a.resize(n);
    for(auto &i : a) cin >> i;
    map <string, int> mp;
    vector <int> id(n);
    for(int i = 0 ; i < n ; i++){
        string b = a[i];
        sort(b.begin(), b.end());
        if(mp.find(b) == mp.end()){
            mp[b] = mp.size();
        }
        id[i] = mp[b];
    }
    vector <int> all;
    for(int i = 0 ; i < n ; i++){
        all.push_back(i);
    }
    sort(all.begin(), all.end(), [&](int i, int j){
        if(id[i] == id[j]) return a[i] < a[j];
        return id[i] < id[j];
    });
    vector <string> new_a(n);
    vector <int> new_id(n);
    for(int i = 0 ; i < n ; i++){
        new_a[i] = a[all[i]];
        new_id[i] = id[all[i]];
    }
    swap(a, new_a);
    swap(id, new_id);
    sor.resize(n, vector <int>(a[0].size()));
    for(int i = 0 ; i < n ; i++){
        for(int j = 1 ; j < (int)sor[i].size() ; j++){
            sor[i][j] = sor[i][j - 1] + (a[i][j] >= a[i][j - 1]);
        }
    }
    for(int itt = 0 ; itt < 2 ; itt++){
        h[itt].resize(n, vector <ll>(a[0].size()));
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < (int)a[i].size() ; j++){
                if(j > 0) h[itt][i][j] = h[itt][i][j - 1];
                h[itt][i][j] = (h[itt][i][j] + 1LL * (a[i][j] - 'a' + 1) * p[j]) % mod;
            }
            reverse(a[i].begin(), a[i].end());
        }
    }
    for(int i = 0 ; i < n ; i++){
        reverse(h[1][i].begin(), h[1][i].end());
        h[1][i].push_back(mod);
    }
    map <ll, int> cm;
    for(auto &i : h){
        for(auto &j : i){
            for(auto &k : j){
                cm[k];
            }
        }
    }
    int x = 0;
    for(auto &i : cm) i.second = x++;
    for(auto &i : h){
        for(auto &j : i){
            for(auto &k : j){
                k = cm[k];
            }
        }
    }
    ll ans = 0;
    if(a[0].size() > SQ){
        for(int i = 0 ; i < n ; i++){
            for(int j = i + 1 ; j < n ; j++){
                if(id[i] != id[j]){
                    ans += 1337;
                    continue;
                }
                int L = -1;
                {
                    int l = 0, r = (int)a[0].size() - 1;
                    while(l <= r){
                        int mid = (l + r) / 2;
                        if(h[0][i][mid] == h[0][j][mid]) l = mid + 1;
                        else r = mid - 1;
                    }
                    L = r;
                }
                int R = n;
                {
                    int l = 0, r = (int)a[0].size() - 1;
                    while(l <= r){
                        int mid = (l + r) / 2;
                        if(h[1][i][mid] == h[1][j][mid]) r = mid - 1;
                        else l = mid + 1;
                    }
                    R = l;
                }
                if(is_sorted(i, L + 1, R - 1) || is_sorted(j, L + 1, R + 1)) ans++;
                else ans += 2;
            }
        }
    }
    else{
        ll ones = 0;
        for(int p = -1 ; p < (int)a[0].size() - 1 ; p++){
            for(int iii = 0 ; iii < n ;){
                int jjj = iii;
                while(jjj < n && id[iii] == id[jjj]) jjj++;
                for(int ii = iii ; ii < jjj ;){
                    int jj = ii;
                    while(jj < jjj && (p == -1 || h[0][ii][p] == h[0][jj][p])) jj++;
                    for(int s = (int)a[0].size() ; s > p + 1 ; s--){
                        for(int i = ii ; i < jj ;){
                            int j = i;
                            while(j < jj && a[i][p + 1] == a[j][p + 1]) j++;
                            for(int k = i ; k < j ; k++){
                                if(is_sorted(k, p + 1, s - 1)) ones += cnt_all[26][h[1][k][s]] - cnt_all[a[k][s - 1] - 'a'][h[1][k][s]];
                                else ones += cnt_sorted[26][h[1][k][s]] - cnt_sorted[a[k][s - 1] - 'a'][h[1][k][s]];
                            }
                            for(int k = i ; k < j ; k++){
                                cnt_all[26][h[1][k][s]]++;
                                cnt_all[a[k][s - 1] - 'a'][h[1][k][s]]++;
                                if(is_sorted(k, p + 1, s - 1)){
                                    cnt_sorted[26][h[1][k][s]]++;
                                    cnt_sorted[a[k][s - 1] - 'a'][h[1][k][s]]++;
                                }
                            }
                            i = j;
                        }
                        for(int k = ii ; k < jj ; k++){
                            cnt_all[26][h[1][k][s]] = 0;
                            cnt_all[a[k][s - 1] - 'a'][h[1][k][s]] = 0;
                            cnt_sorted[26][h[1][k][s]] = 0;
                            cnt_sorted[a[k][s - 1] - 'a'][h[1][k][s]] = 0;
                        }
                    }
                    ii = jj;
                }
                iii = jjj;
            }
        }
        map <int, int> groups;
        for(auto &i : id) groups[i]++;
        ll pairs = 0;
        for(auto &i : groups){
            pairs += 1LL * i.second * (n - i.second);
        }
        pairs /= 2;
        ans += 1337 * pairs;
        pairs = 1LL * n * (n - 1) / 2 - pairs;
        ans += ones;
        pairs -= ones;
        ans += 2 * pairs;
    }
    cout << ans << endl;
}
