1081_H. Palindromic Magic

After learning some fancy algorithms about palindromes, Chouti found palindromes very interesting, so he wants to challenge you with this problem.

Chouti has got two strings A and B. Since he likes [palindromes](https://en.wikipedia.org/wiki/Palindrome), he would like to pick a as some non-empty palindromic substring of A and b as some non-empty palindromic substring of B. Concatenating them, he will get string ab.

Chouti thinks strings he could get this way are interesting, so he wants to know how many different strings he can get.

Input

The first line contains a single string A (1 ≤ |A| ≤ 2 ⋅ 10^5).

The second line contains a single string B (1 ≤ |B| ≤ 2 ⋅ 10^5).

Strings A and B contain only lowercase English letters.

Output

The first and only line should contain a single integer — the number of possible strings.

Examples

Input

aa
aba


Output

6


Input

aaba
abaa


Output

15

Note

In the first example, attainable strings are 

  * "a" + "a" = "aa", 
  * "aa" + "a" = "aaa", 
  * "aa" + "aba" = "aaaba", 
  * "aa" + "b" = "aab", 
  * "a" + "aba" = "aaba", 
  * "a" + "b" = "ab". 



In the second example, attainable strings are "aa", "aaa", "aaaa", "aaaba", "aab", "aaba", "ab", "abaa", "abaaa", "abaaba", "abab", "ba", "baa", "baba", "bb".

Notice that though "a"+"aa"="aa"+"a"="aaa", "aaa" will only be counted once.

Example input:
aa
aba

aaba
abaa


Example output:
6

15


Tags: data structures
hashing
strings
Difficulty rating: 3500

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 200005, P1 = 1000000007, P2 = 998244353;
using ll = long long;
ll qpow(ll a, ll b, int P) {
  ll ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % P;
    a = a * a % P;
    b >>= 1;
  }
  return ret;
}
struct HashVal {
  ll x, y;
} pw[N], ipw[N], hs[2][N];
const HashVal B = {37, 33}, iB = {qpow(37, P1 - 2, P1), qpow(33, P2 - 2, P2)};
inline HashVal operator+(const HashVal &a, const HashVal &b) {
  return {(a.x + b.x) % P1, (a.y + b.y) % P2};
};
inline HashVal operator-(const HashVal &a, const HashVal &b) {
  return {(a.x - b.x + P1) % P1, (a.y - b.y + P2) % P2};
};
inline HashVal operator*(const HashVal &a, const HashVal &b) {
  return {a.x * b.x % P1, a.y * b.y % P2};
};
inline bool operator<(const HashVal &a, const HashVal &b) {
  return a.x == b.x ? a.y < b.y : a.x < b.x;
};
char s[2][N];
ll ans;
struct PAM {
  int t[N][26], pre[N], idx, len[N], anc[20][N], lst = 1;
  HashVal h[N];
  void build(char s[]) {
    idx = 1;
    len[1] = -1;
    pre[0] = 1;
    for (int i = 1; s[i] != 0; i++) {
      int x = s[i] - 'a';
      int p = lst;
      while (s[i - len[p] - 1] != s[i]) p = pre[p];
      if (!t[p][x]) {
        int q = pre[p];
        while (s[i - len[q] - 1] != s[i]) q = pre[q];
        len[++idx] = len[p] + 2;
        pre[idx] = t[q][x];
        t[p][x] = idx;
      }
      lst = t[p][x];
    }
    for (int i = 0; i <= idx; i++)
      for (int j = 0; j < 26; j++)
        if (t[i][j] > 1)
          h[t[i][j]] = i == 1 ? HashVal{j + 1, j + 1}
                              : h[i] * B + HashVal{j + 1, j + 1} *
                                               (pw[0] + pw[len[t[i][j]] - 1]);
    memcpy(anc[0], pre, sizeof(int) * (idx + 1));
    for (int i = 1; i < 20; i++)
      for (int j = 0; j <= idx; j++) anc[i][j] = anc[i - 1][anc[i - 1][j]];
  }
  int go1(char s[], int p, int pos, char x) {
    while (s[p - len[pos] - 1] != x) pos = pre[pos];
    return t[pos][x - 'a'];
  }
  int go2(char s[], int p, int pos, char x) {
    while (s[p + len[pos] + 1] != x) pos = pre[pos];
    return t[pos][x - 'a'];
  }
  int lift(int pos, int l) {
    for (int i = 19; i >= 0; --i)
      if (len[anc[i][pos]] > l) pos = anc[i][pos];
    return pre[pos];
  }
  inline HashVal geth(int fl, int pos) {
    return !fl ? h[pos] - h[pre[pos]] * pw[len[pos] - len[pre[pos]]]
               : (h[pos] - h[pre[pos]]) * ipw[len[pre[pos]]];
  }
} pa[2];
int l[2], idd[N], lt[2][N];
void solve(int fl) {
  static int pos[N], rpos[N], id[N];
  cin >> (s[fl] + 1);
  l[fl] = strlen(s[fl] + 1);
  pa[fl].build(s[fl]);
  for (int i = 1; i <= l[fl]; i++)
    pos[i] = pa[fl].go1(s[fl], i, pos[i - 1], s[fl][i]), id[pos[i]] = i;
  rpos[l[fl] + 1] = 1;
  for (int i = l[fl]; i > 0; --i)
    rpos[i] = pa[fl].go2(s[fl], i, rpos[i + 1], s[fl][i]);
  for (int i = 2; i <= pa[fl].idx; i++)
    if (pa[fl].len[i] > 1) {
      int r = id[i], l = r - pa[fl].len[i] + 1;
      if (fl == 1)
        r -= pa[fl].len[pa[fl].pre[i]];
      else
        l += pa[fl].len[pa[fl].pre[i]];
      if (l >= r) continue;
      int j = pa[fl].lift(rpos[l], r - l), z = pa[fl].len[j], kk = -1;
      if (j > 1 && pa[fl].len[kk = pa[fl].lift(pos[r], r - l + 1 - z)] ==
                       r - l + 1 - z) {
        ++idd[fl];
        if (!fl)
          hs[fl][idd[fl]] =
              pa[fl].h[j] * (pw[r - l + 1] + pw[0]) + pa[fl].h[kk] * pw[z],
          lt[fl][idd[fl]] = z;
        else
          hs[fl][idd[fl]] = pa[fl].h[kk] * (pw[r - l + 1] + pw[0]) +
                            pa[fl].h[j] * pw[r - l + 1 - z],
          lt[fl][idd[fl]] = r - l + 1 - z;
        continue;
      }
      j = pa[fl].lift(pos[r], r - l);
      z = pa[fl].len[j];
      if (j > 1 && pa[fl].len[kk = pa[fl].lift(rpos[l], r - l + 1 - z)] ==
                       r - l + 1 - z) {
        swap(j, kk);
        z = r - l + 1 - z;
        ++idd[fl];
        if (!fl)
          hs[fl][idd[fl]] =
              pa[fl].h[j] * (pw[r - l + 1] + pw[0]) + pa[fl].h[kk] * pw[z],
          lt[fl][idd[fl]] = z;
        else
          hs[fl][idd[fl]] = pa[fl].h[kk] * (pw[r - l + 1] + pw[0]) +
                            pa[fl].h[j] * pw[r - l + 1 - z],
          lt[fl][idd[fl]] = r - l + 1 - z;
      }
    }
}
void solve2(int fl) {
  map<HashVal, int> mp;
  for (int i = 2; i <= pa[fl].idx; i++)
    if (pa[fl].len[i] > 1) mp[pa[fl].h[i]] = i;
  for (int i = 1; i <= idd[!fl]; i++) {
    auto it = mp.find(hs[!fl][i]);
    if (it == mp.end()) continue;
    ans -= pa[fl].len[pa[fl].pre[it->second]] > lt[!fl][i];
  }
}
int main() {
  ios::sync_with_stdio(false);
  pw[0] = ipw[0] = {1, 1};
  for (int i = 1; i < N; i++) pw[i] = pw[i - 1] * B, ipw[i] = ipw[i - 1] * iB;
  solve(0);
  solve(1);
  ans = 1ll * (pa[0].idx - 1) * (pa[1].idx - 1);
  map<HashVal, int> mp;
  for (int i = 2; i <= pa[0].idx; i++)
    if (pa[0].len[i] > 1) ++mp[pa[0].geth(0, i)];
  for (int i = 2; i <= pa[1].idx; i++)
    if (pa[1].len[i] > 1) ans -= mp[pa[1].geth(1, i)];
  solve2(0);
  solve2(1);
  cout << ans << endl;
  return 0;
}