Problem p01558 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

ll pow_mod(ll x, ll n, ll md) {
  ll r = 1;
  while (n) {
    if (n & 1)
      r = (r * x) % md;
    x = (x * x) % md;
    n >>= 1;
  }
  return r;
}

ll invp(ll x, ll md) { return pow_mod(x, md - 2, md); }

const int MN = 400100;
ll powB[MN];
ll res[2][MN];

int n, m;
string s;
string T[MN];
void solve(int id, ll B, ll MD) {
  ll invB = invp(B, MD);
  powB[0] = 1;
  for (int i = 1; i < MN; i++) {
    powB[i] = powB[i - 1] * B % MD;
  }
  int l = 0, r = 1;
  ll hs = s[0];
  for (int i = 0; i < m; i++) {
    string t = T[i];
    if (t == "L--") {
      l--;
      hs *= B;
      hs += s[l];
      hs %= MD;
    } else if (t == "L++") {
      hs -= s[l];
      hs *= invB;
      hs %= MD;
      hs += MD;
      hs %= MD;
      l++;
    } else if (t == "R--") {
      r--;
      hs -= powB[r - l] * s[r] % MD;
      hs %= MD;
      hs += MD;
      hs %= MD;
    } else if (t == "R++") {
      hs += powB[r - l] * s[r] % MD;
      hs %= MD;
      r++;
    }
    //        cout << "hash: " << hs << endl;
    res[id][i] = hs;
  }
}

const ll MD = 1e9 + 7;
const ll B = 454519190;
const ll MD2 = 1e9 + 9;
const ll B2 = 124598427;
typedef pair<ll, ll> P;

int main() {
  cin >> n >> m;
  cin >> s;
  for (int i = 0; i < m; i++) {
    cin >> T[i];
  }
  solve(0, B, MD);
  solve(1, B2, MD2);
  set<P> st;
  for (int i = 0; i < m; i++) {
    st.insert(P(res[0][i], res[1][i]));
  }
  cout << st.size() << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MD = 1e9 + 7;
const ll B = 454519190;

ll pow_mod(ll x, ll n, ll md) {
  ll r = 1;
  while (n) {
    if (n & 1)
      r = (r * x) % md;
    x = (x * x) % md;
    n >>= 1;
  }
  return r;
}

ll invp(ll x, ll md) { return pow_mod(x, md - 2, md); }

const int MN = 100100;
ll powB[MN];

int main() {
  ll invB = invp(B, MD);
  powB[0] = 1;
  for (int i = 1; i < MN; i++) {
    powB[i] = powB[i - 1] * B % MD;
  }
  int n, m;
  scanf("%d %d", &n, &m);
  string s;
  cin >> s;
  set<ll> sub;
  int l = 0, r = 1;
  ll hs = s[0];
  for (int i = 0; i < m; i++) {
    string t;
    cin >> t;
    if (t == "L--") {
      l--;
      hs *= B;
      hs += s[l];
      hs %= MD;
    } else if (t == "L++") {
      hs -= s[l];
      hs *= invB;
      hs %= MD;
      hs += MD;
      hs %= MD;
      l++;
    } else if (t == "R--") {
      r--;
      hs -= powB[r - l] * s[r] % MD;
      hs %= MD;
      hs += MD;
      hs %= MD;
    } else if (t == "R++") {
      hs += powB[r - l] * s[r] % MD;
      hs %= MD;
      r++;
    }
    //        cout << "hash: " << hs << endl;
    sub.insert(hs);
  }
  cout << sub.size() << endl;
  return 0;
}