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

#include <bits/stdc++.h>

#if DEBUG
template <typename T> void debug(const T &value) { std::cerr << value; }
template <typename T, typename... Ts>
void debug(const T &value, const Ts &...args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << #__VA_ARGS__ << ": ";                                              \\
    debug(__VA_ARGS__);                                                        \\
    cerr << " (L" << __LINE__ << ")\\n";                                        \\
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...)                                                          \\
  type __VA_ARGS__;                                                            \\
  read_from_cin(__VA_ARGS__);

template <typename T> void write_to_cout(const T &value) {
  std::cout << value << '\\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T &value, const Ts &...args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define wt(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;

using namespace std;

struct Trie {
  array<int, 26> value = {};
  array<unique_ptr<Trie>, 26> children;
};

class RollingHash {
public:
  void Add(char c) {
    const unsigned long long base = 100000007;
    hash_ = hash_ * base + c;
  }
  size_t hash() const { return hash_ ^ (hash_ >> 32); }
  bool operator==(const RollingHash &h) const { return hash_ == h.hash_; }

private:
  unsigned long long hash_ = 0;
};

namespace std {
template <> class hash<RollingHash> {
public:
  size_t operator()(const RollingHash &h) const { return h.hash(); }
};
} // namespace std

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  rd(int, n);

  V<string> s(n);
  rep(i, n) {
    cin >> s[i];
    reverse(all(s[i]));
  }
  sort(all(s),
       [](const string &a, const string &b) { return a.size() > b.size(); });

  ll ans = 0;
  unordered_map<RollingHash, array<int, 26>> m;
  for (const auto &si : s) {
    vector<bitset<26>> cum(si.size() + 1);
    for (int i = si.size() - 1; i >= 0; --i) {
      cum[i] = cum[i + 1];
      cum[i].set(si[i] - 'a');
    }
    RollingHash h;
    rep(i, si.size() - 1) {
      rep(j, 26) {
        if (cum[i][j]) {
          m[h][j]++;
        }
      }
      h.Add(si[i]);
    }
    ans += m[h][si.back() - 'a'];
  }
  wt(ans);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#if DEBUG
template <typename T> void debug(const T &value) { std::cerr << value; }
template <typename T, typename... Ts>
void debug(const T &value, const Ts &...args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << #__VA_ARGS__ << ": ";                                              \\
    debug(__VA_ARGS__);                                                        \\
    cerr << " (L" << __LINE__ << ")\\n";                                        \\
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...)                                                          \\
  type __VA_ARGS__;                                                            \\
  read_from_cin(__VA_ARGS__);

template <typename T> void write_to_cout(const T &value) {
  std::cout << value << '\\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T &value, const Ts &...args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define wt(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;

using namespace std;

struct Trie {
  array<int, 26> value = {};
  array<unique_ptr<Trie>, 26> children;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  rd(int, n);

  V<string> s(n);
  rep(i, n) {
    cin >> s[i];
    reverse(all(s[i]));
  }
  sort(all(s),
       [](const string &a, const string &b) { return a.size() > b.size(); });

  ll ans = 0;
  rep(i, 26) {
    char c = 'a' + i;
    unordered_map<string, int> m;
    for (const auto &si : s) {
      if (si.back() == c) {
        ans += m[si.substr(0, si.size() - 1)];
      }
      bool have = false;
      for (int i = si.size() - 1; i >= 0; --i) {
        if (si[i] == c)
          have = true;
        if (have)
          ++m[si.substr(0, i)];
      }
    }
  }
  wt(ans);
}