This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/deque_operate_all_composite"
#include "../Utility/template.hpp"
#include "../Datastructure/swag.hpp"
#include "../Utility/modint.hpp"
using mint = modint998244353;
struct S {
mint a, b;
S() {}
S(mint c, mint d) : a(c), b(d) {}
};
S op(S l, S r) {
l.a *= r.a;
l.b *= r.a;
l.b += r.b;
return l;
}
int main() {
SWAG<S, op> swag;
int q;
cin >> q;
while(q--) {
int t;
cin >> t;
if(t == 0) {
mint a, b;
cin >> a >> b;
swag.push_front(S(a, b));
}
else if(t == 1) {
mint a, b;
cin >> a >> b;
swag.push_back(S(a, b));
}
else if(t == 2) {
swag.pop_front();
}
else if(t == 3) {
swag.pop_back();
}
else {
mint x;
cin >> x;
if(!swag.empty()) {
auto [a, b] = swag.get();
cout << a * x + b << '\n';
}
else {
cout << x << '\n';
}
}
}
}
#line 1 "verify/Datastructure_swag_more.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/deque_operate_all_composite"
#line 1 "Utility/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define TT template <typename T>
TT using vec = vector<T>;
template <class T1, class T2> bool chmin(T1 &x, T2 y) {
return x > y ? (x = y, true) : false;
}
template <class T1, class T2> bool chmax(T1 &x, T2 y) {
return x < y ? (x = y, true) : false;
}
struct io_setup {
io_setup() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(15);
}
} io_setup;
/*
@brief verify用テンプレート
*/
#line 1 "Datastructure/swag.hpp"
template<class S, S (*op)(S, S)> struct SWAG {
struct foldable_stack {
stack<S> data;
stack<S> res;
foldable_stack(){}
void push(S a, int type) {//type == 1 : 配列の右に追加、つまりR type == 0 : 配列の左に追加、つまりL
data.push(a);
if(!res.empty()) {
if(type == 0) res.push(op(a, res.top()));//resが空ならそれを入れる
else res.push(op(res.top(), a));
}
else res.push(a);
return;
}
void pop() {
assert(!data.empty());
data.pop();
res.pop();
return;
}
S top() const {
assert(!data.empty());
return data.top();
}
S get() const {
assert(!data.empty());
return res.top();
}
bool empty() {return data.empty();}
int size() {return data.size();}
};
SWAG() {}
foldable_stack L, R;
private:
void move(foldable_stack& s, foldable_stack& t, int type) {//sの要素を半分tの要素に移す。ここで、type == 0 : s = L, t = R type == 1 s = R, t = L
assert(t.empty());
int oth = 1 - type;
stack<S> tmp;
while(int(s.size()) - int(tmp.size()) > 1) tmp.push(s.top()), s.pop();
while(!s.empty()) t.push(s.top(), oth), s.pop();
while(!tmp.empty()) s.push(tmp.top(), type), tmp.pop();
}
public:
void push_front(S a) {
L.push(a, 0);
}
void push_back(S a) {
R.push(a, 1);
}
void pop_front() {
assert(!L.empty() || !R.empty());
if(L.empty()) move(R, L, 1);
L.pop();
}
void pop_back() {
assert(!L.empty() || !R.empty());
if(R.empty()) move(L, R, 0);
R.pop();
}
S get() {
assert(!L.empty() || !R.empty());
if(L.empty()) return R.get();
if(R.empty()) return L.get();
return op(L.get(), R.get());
}
int size() {
return L.size() + R.size();
}
bool empty() {
return size() == 0;
}
};
/*
@brief SWAG
@docs doc/swag.md
*/
#line 1 "Utility/modint.hpp"
// 動的mod : template<int mod> を消して、上の方で変数modを宣言
template <uint32_t mod> struct modint {
using mm = modint;
uint32_t x;
modint() : x(0) {}
TT modint(T a = 0) : x((ll(a) % mod + mod)) {
if (x >= mod) x -= mod;
}
friend mm operator+(mm a, mm b) {
a.x += b.x;
if (a.x >= mod) a.x -= mod;
return a;
}
friend mm operator-(mm a, mm b) {
a.x -= b.x;
if (a.x >= mod) a.x += mod;
return a;
}
mm operator-() const { return mod - x; }
//+と-だけで十分な場合、以下は省略して良いです。
friend mm operator*(mm a, mm b) { return (uint64_t)(a.x) * b.x; }
friend mm operator/(mm a, mm b) { return a * b.inv(); }
friend mm &operator+=(mm &a, mm b) { return a = a + b; }
friend mm &operator-=(mm &a, mm b) { return a = a - b; }
friend mm &operator*=(mm &a, mm b) { return a = a * b; }
friend mm &operator/=(mm &a, mm b) { return a = a * b.inv(); }
mm inv() const {
assert(x != 0);
return pow(mod - 2);
}
mm pow(ll y) const {
mm res = 1;
mm v = *this;
while (y) {
if (y & 1) res *= v;
v *= v;
y /= 2;
}
return res;
}
friend istream &operator>>(istream &is, mm &a) {
ll t;
cin >> t;
a = mm(t);
return is;
}
friend ostream &operator<<(ostream &os, mm a) { return os << a.x; }
bool operator==(mm a) { return x == a.x; }
bool operator!=(mm a) { return x != a.x; }
bool operator<(const mm &a) const { return x < a.x; }
};
using modint998244353 = modint<998244353>;
using modint1000000007 = modint<1'000'000'007>;
/*
@brief modint
*/
#line 5 "verify/Datastructure_swag_more.test.cpp"
using mint = modint998244353;
struct S {
mint a, b;
S() {}
S(mint c, mint d) : a(c), b(d) {}
};
S op(S l, S r) {
l.a *= r.a;
l.b *= r.a;
l.b += r.b;
return l;
}
int main() {
SWAG<S, op> swag;
int q;
cin >> q;
while(q--) {
int t;
cin >> t;
if(t == 0) {
mint a, b;
cin >> a >> b;
swag.push_front(S(a, b));
}
else if(t == 1) {
mint a, b;
cin >> a >> b;
swag.push_back(S(a, b));
}
else if(t == 2) {
swag.pop_front();
}
else if(t == 3) {
swag.pop_back();
}
else {
mint x;
cin >> x;
if(!swag.empty()) {
auto [a, b] = swag.get();
cout << a * x + b << '\n';
}
else {
cout << x << '\n';
}
}
}
}