#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 9;
int power(int n, int k, const int mod)
{
int ans = 1 % mod;
n %= mod;
if (n < 0)
n += mod;
while (k)
{
if (k & 1)
ans = ans * n % mod;
n = n * n % mod;
k >>= 1;
}
return ans;
}
int rng(int maxm) {
static std::mt19937 gen(
std::chrono::steady_clock::now().time_since_epoch().count());
return std::uniform_int_distribution<int>(0, maxm-1)(gen);
}
const int MOD1 = 1e9 + 7, MOD2 = 1e9 + 9;
const int p1 = rng(MOD1), p2 = rng(MOD2);
// See which primes to use :)
int ip1, ip2;
pair<int, int> pw[N], ipw[N];
void prec()
{
pw[0] = {1, 1};
for (int i = 1; i < N; i++)
{
pw[i].first = 1LL * pw[i - 1].first * p1 % MOD1;
pw[i].second = 1LL * pw[i - 1].second * p2 % MOD2;
}
ip1 = power(p1, MOD1 - 2, MOD1);
ip2 = power(p2, MOD2 - 2, MOD2);
ipw[0] = {1, 1};
for (int i = 1; i < N; i++)
{
ipw[i].first = 1LL * ipw[i - 1].first * ip1 % MOD1;
ipw[i].second = 1LL * ipw[i - 1].second * ip2 % MOD2;
}
}
struct Hashing
{
int n;
string s; // 0 - indexed
vector<pair<int, int>> hs; // 1 - indexed
Hashing() {}
Hashing(string _s)
{
n = _s.size();
s = _s;
hs.emplace_back(0, 0);
for (int i = 0; i < n; i++)
{
pair<int, int> p;
p.first = (hs[i].first + 1LL * pw[i].first * s[i] % MOD1) % MOD1;
p.second = (hs[i].second + 1LL * pw[i].second * s[i] % MOD2) % MOD2;
hs.push_back(p);
}
}
pair<int, int> get_hash(int l, int r)
{ // 1 - indexed : index from 1 to n : get hash of a substring
pair<int, int> ans;
ans.first = (hs[r].first - hs[l - 1].first + MOD1) * 1LL * ipw[l - 1].first % MOD1;
ans.second = (hs[r].second - hs[l - 1].second + MOD2) * 1LL * ipw[l - 1].second % MOD2;
return ans;
}
pair<int, int> get_hash()
{
return get_hash(1, n);
}
};
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
prec();
// Change the max length N at top
// Do Hashing hash(sting)
// hash.get_hash(l,r) : 1 based indexing [1,n]
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgaW50IGxvbmcgbG9uZwoKY29uc3QgaW50IE4gPSAxZTYgKyA5OwoKaW50IHBvd2VyKGludCBuLCBpbnQgaywgY29uc3QgaW50IG1vZCkKewogICAgaW50IGFucyA9IDEgJSBtb2Q7CiAgICBuICU9IG1vZDsKICAgIGlmIChuIDwgMCkKICAgICAgICBuICs9IG1vZDsKICAgIHdoaWxlIChrKQogICAgewogICAgICAgIGlmIChrICYgMSkKICAgICAgICAgICAgYW5zID0gYW5zICogbiAlIG1vZDsKICAgICAgICBuID0gbiAqIG4gJSBtb2Q7CiAgICAgICAgayA+Pj0gMTsKICAgIH0KICAgIHJldHVybiBhbnM7Cn0KCmludCBybmcoaW50IG1heG0pIHsKCXN0YXRpYyBzdGQ6Om10MTk5MzcgZ2VuKAoJICAgIHN0ZDo6Y2hyb25vOjpzdGVhZHlfY2xvY2s6Om5vdygpLnRpbWVfc2luY2VfZXBvY2goKS5jb3VudCgpKTsKCXJldHVybiBzdGQ6OnVuaWZvcm1faW50X2Rpc3RyaWJ1dGlvbjxpbnQ+KDAsIG1heG0tMSkoZ2VuKTsKfQoKY29uc3QgaW50IE1PRDEgPSAxZTkgKyA3LCBNT0QyID0gMWU5ICsgOTsKY29uc3QgaW50IHAxID0gcm5nKE1PRDEpLCBwMiA9IHJuZyhNT0QyKTsKLy8gU2VlIHdoaWNoIHByaW1lcyB0byB1c2UgOikKaW50IGlwMSwgaXAyOwpwYWlyPGludCwgaW50PiBwd1tOXSwgaXB3W05dOwp2b2lkIHByZWMoKQp7CiAgICBwd1swXSA9IHsxLCAxfTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDwgTjsgaSsrKQogICAgewogICAgICAgIHB3W2ldLmZpcnN0ID0gMUxMICogcHdb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