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: Time Limit Exceeded)

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 200000 * 2 + 20;
char str[maxn];
int p[maxn];
int manacher(char str[], int lenstr) { //数组记得要开2*maxn，多次使用不需要清空。
    str[0] = '*'; //表示一个不可能的值
    //目标要插入lenstr+1个'#'，所以长度变成2*lenstr+1
    for (int i = lenstr; i >= 0; i--) { //str[lenstr+1]是'\0'
        //i=lenstr时，i+i+2那个值要赋为'\0';
        //总长度只能是lenstr+lenstr+2,所以i从lenstr开始枚举
        str[i + i + 2] = str[i + 1];
        str[i + i + 1] = '#';
    }
    int id = 0, maxlen = 0; //现在开始在str[2]了
    for (int i = 2; i <= 2 * lenstr + 1; i++) { //2*lenstr+1是'#'没用
        if (p[id] + id > i) { //没取等号的，只能去到p[id]+id-1
            //p[id]+id是越界的，减去i即为区间长度
            //p[id]+id-i,这个是所有可能中的最大值了
            p[i] = min(p[id] + id - i, p[2 * id - i]);
        } else p[i] = 1; //记得修改的是p[i]
        while (str[i + p[i]] == str[i - p[i]]) ++p[i];
        if (p[id] + id < p[i] + i) id = i;
        maxlen = max(maxlen, p[i]);
//为什么maxlen是p[i]而不是2*p[i]-1  呢??
//因为*#a#b#a#在b中，开始时候p[i]是1(有效值)，扩展p[i]=2
//但是这个增加是无效值，又p[i]=3(有效值),.....每次扩展一个有效值，必能扩展一个无效值，//而且一定是无效值结尾，所以ans=maxlen-1减去1是为了减去最后一个无效值。
//p[i]是包括#号字符串的回文串的半径（包括自己），扩展一个有效值后，又扩展一个无效
//值(相当于数量增加的是左边的有效值)，所以p[i] - 1就是回文串长度
    }
    return maxlen - 1;
}
char sub[maxn];
map<string, bool> mp;
bool is(int one, int two) {
    return p[(one + two)] - 1 >= two - one + 1;
}
vector<int> vc[222];
void work() {
    scanf("%s", str + 1);
    strcpy(sub + 1, str + 1);
    int lenstr = strlen(str + 1);
    for (int i = 1; i <= lenstr; ++i) vc[str[i]].push_back(i);
//    LL t = 1;
//    mp[sub + 1] = true;
//    for (int i = 1; i <= lenstr; ++i) {
//        for (int j = 1; j <= i; ++j) {
//            reverse(sub + j, sub + 1 + i);
//            if (!mp[sub + 1]) {
//                mp[sub + 1] = true;
//                t++;
//            } else {
//                printf("%d %d %s\n", j, i, sub + 1);
//            }
//            reverse(sub + j, sub + 1 + i);
//        }
//    }
//    cout << t << endl;
    LL ans = 1LL * lenstr * (1 + lenstr) / 2 + 1;
    manacher(str, lenstr);
    for (int i = 2; i <= 2 * lenstr; ++i) {
        LL res = p[i] - 1;
        ans -= (res + 1) / 2;
    }
    if (ans == 1) {
        cout << ans << endl;
        return;
    }
    for (int i = 'a'; i <= 'z'; ++i) {
        for (int j = 0; j < vc[i].size(); ++j) {
            for (int k = j + 1; k < vc[i].size(); ++k) {
                if (is(vc[i][j], vc[i][k])) continue;
                ans--;
            }
        }
    }
    cout << ans << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    work();
    return 0;
}