#include<bits/stdc++.h>

using namespace std;

#define loop(i, l, r) for(int i = (int) l; i <= (int) r; i++)
#define rloop(i, r, l) for(int i = (int) r; i >= (int) l; i--)

#define vi vector<int>
#define ii pair<int, int>
#define eb emplace_back
#define all(x) begin(x), end(x)
#define fi first
#define se second

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define rand rng
#define endl '\n'
#define sp ' '


#define int long long

const int maxN = 4e5 + 5, mod = 1e9 + 7, p = 31;

int lg2[maxN];

int N, M;
int ans; 
string S[maxN];
int euler[maxN];
vector<int> r[maxN];
int e[maxN];

int eucnt;
int spa[20][maxN];

void prepsparse(){
	loop(bit, 1, 20){
		int len = 1 << bit;
		loop(i, 1, eucnt - len + 1){
			spa[bit][i] = min(spa[bit - 1][i], spa[bit - 1][i + len / 2]);
		}
	}
}
int getmin(int l, int r){
    if(l > r) swap(l, r);
	int bit = lg2[r - l + 1], len = 1 << bit;
	return min(spa[bit][l], spa[bit][r - len + 1]);
}

struct node{
	int p, child[26], d = 0, in = 0; 
	vector<int> list;
	node(int p = 0):p(p){
		fill(all(child), -1);
	}
};
struct trie{
	vector<node> T;
	trie(){
	}
	void clear(){
		T.clear(); T.eb(node());
		eucnt = 0;
	}
	void insert(int id, bool rev){
		string s = S[id];
		if(rev) reverse(all(s));
		int n = 0;
		for(char c: s){
			int e = c - 'a';
			if(T[n].child[e] == -1){
				T[n].child[e] = T.size();
				T.eb(node(n));
			}
			n = T[n].child[e];
		}
		T[n].list.eb(id);
	}
	void dfs(int n){
		T[n].in = ++eucnt;
		spa[0][T[n].in] = T[n].d;
		loop(e, 0, 25){
			int v = T[n].child[e];
			if(v != -1) {
				T[v].d = T[n].d + 1;
				dfs(v);
				spa[0][++eucnt] = T[n].d;
			}
		}
		for(int i: T[n].list){
			euler[i] = T[n].in;
		}
	}
	vector<ii> list(int n){
		vector<ii> all;
		loop(e, 0, 25){
			int v = T[n].child[e];
			if(v != -1) {
				T[v].d = T[n].d + 1;
				vector<ii> tmp = list(v);
				for(auto t: tmp) all.eb(t);
			}
		}
		for(int i: T[n].list){
			all.eb(euler[i], i);
		}
		sort(all(all));
		loop(idx, 0, all.size() - 1){
			int i = all[idx].se;
			if(r[i][T[n].d]){
				int req = M - 1 - r[i][T[n].d];
				int num = 0;
				int low = 0, high = idx;
				while(low < high){
					int m = (low + high + 1) / 2;
					int j = all[idx - m].se;
					int lcs = getmin(euler[i], euler[j]);
					if(lcs >= req) low = m;
					else high = m - 1;
				}
				num += low;
				low = 0, high = all.size() - 1 - idx;
				while(low < high){
					int m = (low + high + 1) / 2;
					int j = all[idx + m].se;
					int lcs = getmin(euler[i], euler[j]);
					if(lcs >= req) low = m;
					else high = m - 1;
				}
				num += low;
				ans -= 2 * (num - e[i]);
			}
		}
		return all;
	}
} pref, suf;

void solve(vector<int> t){
	pref.clear();
	suf.clear();
	for(int i: t){
		pref.insert(i, 0); // map node to string, too
		suf.insert(i, 1);
	}
	suf.dfs(0); // map, sparse
	prepsparse();
	pref.list(0);
}

int gethash(string &s){
	int r = 0;
	for(char c: s){
		r = (r * p + c - 'a') % mod;
	}
	return r;
}

int getclass(string &s){
	int r = 0;
	vector<int> cnt(26, 0);
	for(char c: s){
		cnt[c - 'a']++;
	}
	loop(i, 0, 25){
		r = (r * p + cnt[i]) % mod;
	}
	return r;
}

signed main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	lg2[1] = 0;
	loop(i, 2, maxN - 1){
		lg2[i] = lg2[i / 2] + 1;
	}
	cin >> N;
	loop(i, 1, N){
		cin >> S[i];
	}
	M = S[1].length();
	vector<ii> cl, el;
	loop(i, 1, N){
		r[i].resize(M + 1, 0);
		loop(j, 0, M - 1){
//			cout << "j: " << j << endl;
			r[i][j] = j;
			while(r[i][j] < M - 1 and S[i][r[i][j] + 1] >= S[i][r[i][j]]) ++r[i][j];
//			cout << i << sp << j <<sp << r[i][j] << endl;
			j = r[i][j];
		}
		cl.eb(getclass(S[i]), i);
		el.eb(gethash(S[i]), i);
	}
	sort(all(cl));
	sort(all(el));
	int sum = N * (N - 1);
	loop(i, 0, N - 1){
		int j = i, cnt = 1;
		while(j + 1 < N and el[j + 1].fi == el[i].fi) {
		    j++;
		    ++cnt;
		}
		vector<int> tmp;
		loop(k, i, j){
			e[cl[k].se] = cnt - 1;
		}
		ans -= 2 * cnt * (cnt - 1);
		i = j;
	}
	loop(i, 0, N - 1){
		int j = i;
		while(j + 1 < N and cl[j + 1].fi == cl[i].fi) ++j;
		vector<int> tmp;
		loop(k, i, j){
			tmp.eb(cl[k].se);
		}
		ans += 2 * tmp.size() * (tmp.size() - 1);
		solve(tmp);
		sum -= tmp.size() * (tmp.size() - 1);
		i = j;
	}
	ans += sum * 1337;
	ans /= 2;
	cout << ans;
}

