#line 1 "test/yj_queue_operate_all_composite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/queue_operate_all_composite"
#include <cstdint>
#include <cstdio>
#line 1 "utility/monoid/composite.cpp"
/**
* @brief 一次関数の合成を得る演算のモノイド
* @author えびちゃん
*/
#include <algorithm>
#include <utility>
#ifndef H_composite_monoid
#define H_composite_monoid
template <typename Tp>
class composite_monoid {
public:
using value_type = Tp;
private:
value_type M_a = 1;
value_type M_b = 0;
public:
composite_monoid() = default; // identity
composite_monoid(value_type a, value_type b): M_a(a), M_b(b) {};
composite_monoid& operator +=(composite_monoid that) {
M_a *= that.M_a;
M_b *= that.M_a;
M_b += that.M_b;
return *this;
}
composite_monoid operator +(composite_monoid const& that) const {
return composite_monoid(*this) += that;
}
composite_monoid operator +(composite_monoid&& that) const {
return composite_monoid(*this) += std::move(that);
}
bool operator ==(composite_monoid const& that) const {
return (M_a == that.M_a && M_b == that.M_b);
}
bool operator !=(composite_monoid const& that) const { return !(*this == that); }
auto get() const { return std::make_pair(M_a, M_b); }
value_type operator ()(value_type x) const { return M_a * x + M_b; }
};
#endif /* !defined(H_composite_monoid) */
#line 1 "ModularArithmetic/modint.cpp"
/**
* @brief 合同算術用クラス
* @author えびちゃん
*/
#line 10 "ModularArithmetic/modint.cpp"
#include <limits>
#include <type_traits>
#line 13 "ModularArithmetic/modint.cpp"
template <intmax_t Modulo>
class modint {
public:
using value_type = typename std::conditional<
(0 < Modulo && Modulo < std::numeric_limits<int>::max() / 2), int, intmax_t
>::type;
private:
static constexpr value_type S_cmod = Modulo; // compile-time
static value_type S_rmod; // runtime
value_type M_value = 0;
static constexpr value_type S_inv(value_type n, value_type m) {
value_type x = 0;
value_type y = 1;
value_type a = n;
value_type b = m;
for (value_type u = y, v = x; a;) {
value_type q = b / a;
std::swap(x -= q*u, u);
std::swap(y -= q*v, v);
std::swap(b -= q*a, a);
}
if ((x %= m) < 0) x += m;
return x;
}
static value_type S_normalize(intmax_t n, value_type m) {
if (n >= m) {
n %= m;
} else if (n < 0) {
if ((n %= m) < 0) n += m;
}
return n;
}
public:
modint() = default;
modint(intmax_t n): M_value(S_normalize(n, get_modulo())) {}
modint& operator =(intmax_t n) {
M_value = S_normalize(n, get_modulo());
return *this;
}
modint& operator +=(modint const& that) {
if ((M_value += that.M_value) >= get_modulo()) M_value -= get_modulo();
return *this;
}
modint& operator -=(modint const& that) {
if ((M_value -= that.M_value) < 0) M_value += get_modulo();
return *this;
}
modint& operator *=(modint const& that) {
intmax_t tmp = M_value;
tmp *= that.M_value;
M_value = tmp % get_modulo();
return *this;
}
modint& operator /=(modint const& that) {
intmax_t tmp = M_value;
tmp *= S_inv(that.M_value, get_modulo());
M_value = tmp % get_modulo();
return *this;
}
modint& operator ++() {
if (++M_value == get_modulo()) M_value = 0;
return *this;
}
modint& operator --() {
if (M_value-- == 0) M_value = get_modulo()-1;
return *this;
}
modint operator ++(int) { modint tmp(*this); ++*this; return tmp; }
modint operator --(int) { modint tmp(*this); --*this; return tmp; }
friend modint operator +(modint lhs, modint const& rhs) { return lhs += rhs; }
friend modint operator -(modint lhs, modint const& rhs) { return lhs -= rhs; }
friend modint operator *(modint lhs, modint const& rhs) { return lhs *= rhs; }
friend modint operator /(modint lhs, modint const& rhs) { return lhs /= rhs; }
modint operator +() const { return *this; }
modint operator -() const {
if (M_value == 0) return *this;
return modint(get_modulo() - M_value);
}
friend bool operator ==(modint const& lhs, modint const& rhs) {
return lhs.M_value == rhs.M_value;
}
friend bool operator !=(modint const& lhs, modint const& rhs) {
return !(lhs == rhs);
}
value_type get() const { return M_value; }
static value_type get_modulo() { return ((S_cmod > 0)? S_cmod: S_rmod); }
template <int M = Modulo, typename Tp = typename std::enable_if<(M <= 0)>::type>
static Tp set_modulo(value_type m) { S_rmod = m; }
};
template <intmax_t N>
constexpr typename modint<N>::value_type modint<N>::S_cmod;
template <intmax_t N>
typename modint<N>::value_type modint<N>::S_rmod;
#line 1 "DataStructure/foldable_queue.cpp"
/**
* @brief fold 可能キュー
* @author えびちゃん
*/
#include <cstddef>
#include <stack>
#line 12 "DataStructure/foldable_queue.cpp"
template <class Monoid>
class foldable_queue {
public:
using size_type = size_t;
using value_type = Monoid;
private:
std::stack<value_type> M_front, M_back;
value_type M_back_folded{};
void M_rotate_to_front() {
if (!M_back.empty()) {
M_front.push(std::move(M_back.top()));
M_back.pop();
}
while (!M_back.empty()) {
M_back.top() += M_front.top();
M_front.push(std::move(M_back.top()));
M_back.pop();
}
M_back_folded = value_type{};
}
public:
size_type size() const { return M_front.size() + M_back.size(); }
bool empty() const noexcept { return M_front.empty() && M_back.empty(); }
void push(value_type const& x) {
M_back.push(x);
M_back_folded += M_back.top();
}
template <typename... Args>
void emplace(Args&&... args) {
M_back.emplace(std::forward<Args>(args)...);
M_back_folded += M_back.top();
}
void pop() {
if (M_front.empty()) M_rotate_to_front();
M_front.pop();
}
value_type fold() const {
if (M_front.empty()) return M_back_folded;
return M_front.top() + M_back_folded;
}
};
#line 9 "test/yj_queue_operate_all_composite.test.cpp"
constexpr intmax_t mod = 998244353;
using mi = modint<mod>;
int main() {
size_t q;
scanf("%zu", &q);
foldable_queue<composite_monoid<mi>> fq;
for (size_t i = 0; i < q; ++i) {
int t;
scanf("%d", &t);
if (t == 0) {
intmax_t a, b;
scanf("%jd %jd", &a, &b);
fq.emplace(a, b);
} else if (t == 1) {
fq.pop();
} else if (t == 2) {
intmax_t x;
scanf("%jd", &x);
printf("%d\n", fq.fold()(x).get());
}
}
}