Problem:
Score : 500 points

Problem Statement

You have a string A = A_1 A_2 ... A_n consisting of lowercase English letters.

You can choose any two indices i and j such that 1 \leq i \leq j \leq n and reverse substring A_i A_{i+1} ... A_j.

You can perform this operation at most once.

How many different strings can you obtain?

Constraints

1 \leq |A| \leq 200,000

A consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

A

Output

Print the number of different strings you can obtain by reversing any substring in A at most once.

Sample Input 1

aatt

Sample Output 1

5

You can obtain aatt (don't do anything), atat (reverse A[2..3]), atta (reverse A[2..4]), ttaa (reverse A[1..4]) and taat (reverse A[1..3]).

Sample Input 2

xxxxxxxxxx

Sample Output 2

1

Whatever substring you reverse, you'll always get xxxxxxxxxx.

Sample Input 3

abracadabra

Sample Output 3

44

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

#define x first
#define y second

#ifdef ONLINE_JUDGE
#define DEBUG(x)
#else
#define DEBUG(x) cerr << #x << ": " << x << endl;
#endif

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;

const int mod=1000000000+7;

int addm(int& a,int b) {return (a+=b)<mod?a:a-=mod;}

template<class T,class U> bool smin(T& a,U b) {return a>b?(a=b,1):0;}
template<class T,class U> bool smax(T& a,U b) {return a<b?(a=b,1):0;}

void is(string& s) {//insert spaces
	int N=s.size();
	s.resize(2*N+1,' ');
	for (int c=N-1;c>=0;c--) {s[2*c+1]=s[c];s[2*c]=' ';}
}

vi lps(string s,string t) {
	int N=s.size();
	is(s);is(t);//Comment this out if only want odd length palindromes
	//and interpret the answer differently.
	N=2*N+1;
	vi r(N);
	
	int Mi=0;
	for (int c=1;c<N;c++) {
		int Mw=r[Mi]+Mi-c;
		int w=r[c]=(Mw>=0?min(Mw,r[2*Mi-c]):0);
		if (w>=Mw) {
			int i=c-w,j=c+w;
			while (++j<N && --i>=0 && s[i]==t[j] && s[j]==t[i]);//Only place where t used.
			r[Mi=c]=j-1-c;
		}
	}

	return r;
}

string A;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> A;
	ll ans=(ll)(A.size())*(A.size()-1)/2+1;

	vi rad = lps(A,A);

	for (int i:rad) ans-=i/2;

	cout << ans << endl;
}