#include #include #include #include #include #include #include using namespace std; #include using mint = atcoder::modint998244353; // Rolling Hash (Rabin-Karp), 1dim template struct rolling_hash { int N; const V B; std::vector hash; // hash[i] = s[0] * B^(i - 1) + ... + s[i - 1] static std::vector power; // power[i] = B^i void _extend_powvec() { if (power.size() > 1 and power.at(1) != B) power = {V(1)}; while (static_cast(power.size()) <= N) { auto tmp = power.back() * B; power.push_back(tmp); } } template rolling_hash(const std::vector &s, V b = V::randgen()) : N(s.size()), B(b), hash(N + 1) { for (int i = 0; i < N; i++) hash[i + 1] = hash[i] * B + s[i]; _extend_powvec(); } rolling_hash(const std::string &s = "", V b = V::randgen()) : N(s.size()), B(b), hash(N + 1) { for (int i = 0; i < N; i++) hash[i + 1] = hash[i] * B + s[i]; _extend_powvec(); } void addchar(const char &c) { V hnew = hash[N] * B + c; N++, hash.emplace_back(hnew); _extend_powvec(); } V get(int l, int r) const { // s[l] * B^(r - l - 1) + ... + s[r - 1] return hash[r] - hash[l] * power[r - l]; } int lcplen(int l1, int l2) const { return longest_common_prefix(*this, l1, *this, l2); } }; template std::vector rolling_hash::power{V(1)}; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; vector A(N); for (auto &x : A) { int y; cin >> y; x = y; } const rolling_hash rh(A, mint(2).inv()); vector last(N); for (int l = 0, i = 1; i < N; ++i) { if (A.at(i - 1).val() >= A.at(i).val()) l = i; last.at(i) = l; } while (Q--) { int l, r; cin >> l >> r; --l; int ll = max(l, last.at(r - 1)); int len = r - ll; cout << (A.at(ll) + (len >= 2 ? rh.get(ll + 1, r) * mint(2).pow(len - 2) : 0)).val() << '\n'; } }