#line 2 "library/KowerKoint/base.hpp" #include using namespace std; #line 4 "library/KowerKoint/stl-wrapper/pair.hpp" template struct Pair : public std::pair { constexpr Pair() : std::pair() {} constexpr Pair(const T1& first, const T2& second) : std::pair(first, second) {} template constexpr Pair(U1&& first, U2&& second) : std::pair(std::forward(first), std::forward(second)) {} template constexpr Pair(const std::pair& other) : std::pair(other) {} template constexpr Pair(std::pair&& other) : std::pair(std::move(other)) {} template Pair(std::piecewise_construct_t, std::tuple first_args, std::tuple second_args) : std::pair(std::piecewise_construct, first_args, second_args) {} friend std::istream& operator>>(std::istream& is, Pair& p) { return is >> p.first >> p.second; } friend std::ostream& operator<<(std::ostream& os, const Pair& p) { return os << p.first << ' ' << p.second; } }; namespace std { template struct hash> { size_t operator()(const Pair& p) const { size_t seed = 0; seed ^= hash()(p.first) + 0x9e3779b9 + (seed << 6) + (seed >> 2); seed ^= hash()(p.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2); return seed; } }; } #line 5 "library/KowerKoint/stl-wrapper/vector.hpp" template struct Vector : std::vector { Vector() noexcept : std::vector() {} explicit Vector(size_t count) : std::vector(count, T()) {} Vector(size_t count, const T& value) : std::vector(count, value) {} template Vector(InputIt first, InputIt last) : std::vector(first, last) {} Vector(const std::vector& other) : std::vector(other) {} Vector(std::vector&& other) noexcept : std::vector(std::move(other)) {} Vector(std::initializer_list init) : std::vector(init) {} const T& operator[](size_t i) const { assert(i < this->size()); return std::vector::operator[](i); } T& operator[](size_t i) { assert(i < this->size()); return std::vector::operator[](i); } const T& front() const { assert(!this->empty()); return std::vector::front(); } T& front() { assert(!this->empty()); return std::vector::front(); } const T& back() const { assert(!this->empty()); return std::vector::back(); } T& back() { assert(!this->empty()); return std::vector::back(); } friend std::istream& operator>>(std::istream& is, Vector& v) { for (auto& x : v) is >> x; return is; } friend std::ostream& operator<<(std::ostream& os, const Vector& v) { for (size_t i = 0; i < v.size(); ++i) { if (i) os << ' '; os << v[i]; } return os; } }; namespace std { template struct hash> { size_t operator()(const Vector& v) const { size_t seed = 0; for (const auto& x : v) seed ^= hash{}(x) + 0x9e3779b9 + (seed << 6) + (seed >> 2); return seed; } }; }; #line 4 "library/KowerKoint/stl-wrapper/set.hpp" #include #include #line 7 "library/KowerKoint/stl-wrapper/set.hpp" template > using pbds_set = __gnu_pbds::tree; template > struct Set : public pbds_set { Set() : pbds_set() {} explicit Set(const Compare& comp) : pbds_set(comp) {} template Set(It first, It last, const Compare& comp = Compare()) : pbds_set(first, last, comp) {} Set(const pbds_set& other) : pbds_set(other) {} Set(const std::set& other) : pbds_set(other.begin(), other.end()) {} Set(pbds_set&& other) : pbds_set(std::move(other)) {} Set(std::initializer_list init, const Compare& comp = Compare()) : pbds_set(init, comp) {} typename Set::const_iterator cbegin() const { return this->begin(); } typename Set::const_iterator cend() const { return this->end(); } typename Set::const_reverse_iterator crbegin() const { return this->rbegin(); } typename Set::const_reverse_iterator crend() const { return this->rend(); } template std::pair equal_range(const K& value) { return std::make_pair(lower_bound(value), upper_bound(value)); } template std::pair equal_range(const K& value) const { return std::make_pair(lower_bound(value), upper_bound(value)); } template size_t count(const K& x) const { return this->find(x) != this->end(); } decltype(Compare()) key_comp() const { return Compare(); } template std::pair emplace(Args&&... args) { return this->insert(T(std::forward(args)...)); } template typename Set::iterator emplace_hint(typename Set::const_iterator hint, Args&&... args) { return this->insert(hint, T(std::forward(args)...)); } friend std::ostream& operator<<(std::ostream& os, const Set& set) { Vector vector(set.begin(), set.end()); return os << vector; } }; namespace _set_util { template struct CompareEqual { bool operator()(const T& lhs, const T& rhs) const { return !Compare()(rhs, lhs); } }; } template > using pbds_multiset = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; template > struct MultiSet : public pbds_multiset { MultiSet() : pbds_multiset() {} explicit MultiSet(const Compare& comp) : pbds_multiset(comp) {} template MultiSet(It first, It last, const Compare& comp = Compare()) : pbds_multiset(first, last, comp) {} MultiSet(const pbds_multiset& other) : pbds_multiset(other) {} MultiSet(const std::multiset& other) : pbds_multiset(other.begin(), other.end()) {} MultiSet(pbds_multiset&& other) : pbds_multiset(std::move(other)) {} MultiSet(std::initializer_list init, const Compare& comp = Compare()) : pbds_multiset(init, comp) {} typename MultiSet::const_iterator cbegin() const { return this->begin(); } typename MultiSet::const_iterator cend() const { return this->end(); } typename MultiSet::const_reverse_iterator crbegin() const { return this->rbegin(); } typename MultiSet::const_reverse_iterator crend() const { return this->rend(); } template std::pair equal_range(const K& value) { return std::make_pair(lower_bound(value), upper_bound(value)); } template std::pair equal_range(const K& value) const { return std::make_pair(lower_bound(value), upper_bound(value)); } template size_t count(const K& x) const { auto range = equal_range(x); return std::distance(range.first, range.second); } decltype(Compare()) key_comp() const { return Compare(); } template typename MultiSet::iterator emplace(Args&&... args) { return this->insert(T(std::forward(args)...)); } template typename MultiSet::iterator emplace_hint(typename MultiSet::const_iterator hint, Args&&... args) { return this->insert(hint, T(std::forward(args)...)); } friend std::ostream& operator<<(std::ostream& os, const MultiSet& set) { Vector vector(set.begin(), set.end()); return os << vector; } }; namespace std { template struct hash> { size_t operator()(const Set& set) const { Vector vec(set.begin(), set.end()); return hash>()(vec); } }; template struct hash> { size_t operator()(const MultiSet& set) const { Vector vec(set.begin(), set.end()); return hash>()(vec); } }; }; #line 7 "library/KowerKoint/stl-wrapper/map.hpp" template using pbds_map = __gnu_pbds::tree; template > struct Map : pbds_map { Map() : pbds_map() {} explicit Map(const Compare& comp) : pbds_map(comp) {} template Map(It first, It last, const Compare& comp = Compare()) : pbds_map(first, last, comp) {} Map(const pbds_map& other) : pbds_map(other) {} Map(const std::map& other) : pbds_map(other.begin(), other.end()) {} Map(pbds_map&& other) : pbds_map(std::move(other)) {} Map(std::initializer_list> init, const Compare& comp = Compare()) : pbds_map(init, comp) {} typename Map::const_iterator cbegin() const { return this->begin(); } typename Map::const_iterator cend() const { return this->end(); } typename Map::const_reverse_iterator crbegin() const { return this->rbegin(); } typename Map::const_reverse_iterator crend() const { return this->rend(); } template std::pair equal_range(const K& value) { return std::make_pair(lower_bound(value), upper_bound(value)); } template std::pair equal_range(const K& value) const { return std::make_pair(lower_bound(value), upper_bound(value)); } Value& at(const Key& key) { auto it = find(key); assert(it != this->end()); return it->second; } const Value& at(const Key& key) const { auto it = find(key); assert(it != this->end()); return it->second; } template size_t count(const K& x) const { return this->find(x) != this->end(); } decltype(Compare()) key_comp() const { return Compare(); } template std::pair emplace(Args&&... args) { return this->insert(std::make_pair(std::forward(args)...)); } template typename Map::iterator emplace_hint(typename Map::const_iterator hint, Args&&... args) { return this->insert(hint, std::make_pair(std::forward(args)...)); } friend std::ostream& operator<<(std::ostream& out, const Map& map) { for (auto it = map.begin(); it != map.end(); ++it) { out << it->first << ' ' << it->second << '\n'; } return out; } }; namespace _map_util { template struct CompareEqual { bool operator()(const T& lhs, const T& rhs) const { return !Compare()(rhs, lhs); } }; } template > using pbds_multimap = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; template > struct MultiMap : pbds_multimap { MultiMap() : pbds_multimap() {} explicit MultiMap(const Compare& comp) : pbds_multimap(comp) {} template MultiMap(It first, It last, const Compare& comp = Compare()) : pbds_multimap(first, last, comp) {} MultiMap(const pbds_multimap& other) : pbds_multimap(other) {} MultiMap(const std::multimap& other) : pbds_multimap(other.begin(), other.end()) {} MultiMap(pbds_multimap&& other) : pbds_multimap(std::move(other)) {} MultiMap(std::initializer_list> init, const Compare& comp = Compare()) : pbds_multimap(init, comp) {} typename MultiMap::const_iterator cbegin() const { return this->begin(); } typename MultiMap::const_iterator cend() const { return this->end(); } typename MultiMap::const_reverse_iterator crbegin() const { return this->rbegin(); } typename MultiMap::const_reverse_iterator crend() const { return this->rend(); } template std::pair equal_range(const K& value) { return std::make_pair(lower_bound(value), upper_bound(value)); } template std::pair equal_range(const K& value) const { return std::make_pair(lower_bound(value), upper_bound(value)); } Value& at(const Key& key) { auto it = find(key); assert(it != this->end()); return it->second; } const Value& at(const Key& key) const { auto it = find(key); assert(it != this->end()); return it->second; } template size_t count(const K& x) const { auto range = equal_range(x); return std::distance(range.first, range.second); } decltype(Compare())key_comp() const { return Compare(); } template typename MultiMap::iterator emplace(Args&&... args) { return this->insert(std::make_pair(std::forward(args)...)); } template typename MultiMap::iterator emplace_hint(typename MultiMap::const_iterator hint, Args&&... args) { return this->insert(hint, std::make_pair(std::forward(args)...)); } friend std::ostream& operator<<(std::ostream& out, const MultiMap& map) { for (auto it = map.begin(); it != map.end(); ++it) { out << it->first << ' ' << it->second << '\n'; } return out; } }; namespace std { template struct hash> { size_t operator()(const Map& map) const { Vector> v(map.begin(), map.end()); return hash>>()(v); } }; template struct hash> { size_t operator()(const Map& map) const { Vector> v(map.begin(), map.end()); return hash>>()(v); } }; } #line 9 "library/KowerKoint/stl-wrapper/unordered_set.hpp" template , typename KeyEqual = std::equal_to> using pbds_unordered_set = __gnu_pbds::gp_hash_table; template , typename KeyEqual = std::equal_to> struct UnorderedSet : public pbds_unordered_set { UnorderedSet() : pbds_unordered_set() {} explicit UnorderedSet(std::size_t bucket_count, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : pbds_unordered_set(bucket_count, hash, equal) {} template UnorderedSet(InputIt first, InputIt last, std::size_t bucket_count = 1, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : pbds_unordered_set(first, last, bucket_count, hash, equal) {} UnorderedSet(const pbds_unordered_set& other) : pbds_unordered_set(other) {} UnorderedSet(const std::unordered_set& other) : pbds_unordered_set(other.begin(), other.end()) {} UnorderedSet(pbds_unordered_set&& other) : pbds_unordered_set(std::move(other)) {} UnorderedSet(std::initializer_list init, std::size_t bucket_count = 1, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : pbds_unordered_set(init, bucket_count, hash, equal) {} typename UnorderedSet::const_iterator cbegin() const { return this->begin(); } typename UnorderedSet::const_iterator cend() const { return this->end(); } KeyEqual key_eq() const { return KeyEqual(); } std::size_t count(const T& value) const { return this->find(value) != this->end(); } template std::pair emplace(Args&&... args) { return this->insert(T(std::forward(args)...)); } template typename UnorderedSet::iterator emplace_hint(typename UnorderedSet::const_iterator hint, Args&&... args) { return this->insert(hint, T(std::forward(args)...)); } friend std::ostream& operator<<(std::ostream& os, const UnorderedSet& set) { for (auto it = set.begin(); it != set.end(); ++it) { if(it != set.begin()) os << " "; os << *it; } return os; } }; template , typename KeyEqual = std::equal_to> struct UnorderedMultiSet : std::unordered_multiset { UnorderedMultiSet() : std::unordered_multiset() {} explicit UnorderedMultiSet(std::size_t bucket_count, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : std::unordered_multiset(bucket_count, hash, equal) {} template UnorderedMultiSet(InputIt first, InputIt last, std::size_t bucket_count = 1, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : std::unordered_multiset(first, last, bucket_count, hash, equal) {} UnorderedMultiSet(const std::unordered_set& other) : std::unordered_multiset(other) {} UnorderedMultiSet(std::unordered_set&& other) : std::unordered_multiset(std::move(other)) {} UnorderedMultiSet(std::initializer_list init, std::size_t bucket_count = 1, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : std::unordered_multiset(init, bucket_count, hash, equal) {} friend std::ostream& operator<<(std::ostream& os, const UnorderedMultiSet& set) { for (auto it = set.begin(); it != set.end(); ++it) { if(it != set.begin()) os << " "; os << *it; } return os; } }; namespace std { template struct hash> { size_t operator()(const UnorderedSet& set) const { Vector vec(set.begin(), set.end()); sort(vec.begin(), vec.end()); return hash>()(vec); } }; template struct hash> { size_t operator()(const UnorderedMultiSet& set) const { Vector vec(set.begin(), set.end()); sort(vec.begin(), vec.end()); return hash>()(vec); } }; } #line 9 "library/KowerKoint/stl-wrapper/unordered_map.hpp" template , typename KeyEqual = std::equal_to> using pbds_unordered_map = __gnu_pbds::gp_hash_table; template , typename KeyEqual = std::equal_to> struct UnorderedMap : public pbds_unordered_map { UnorderedMap() : pbds_unordered_map() {} explicit UnorderedMap(std::size_t bucket_count, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : pbds_unordered_map(bucket_count, hash, equal) {} template UnorderedMap(InputIt first, InputIt last, std::size_t bucket_count = 1, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : pbds_unordered_map(first, last, bucket_count, hash, equal) {} UnorderedMap(const pbds_unordered_map& other) : pbds_unordered_map(other) {} UnorderedMap(const std::unordered_map& other) : pbds_unordered_map(other.begin(), other.end()) {} UnorderedMap(pbds_unordered_map&& other) noexcept : pbds_unordered_map(std::move(other)) {} UnorderedMap(std::initializer_list> init, std::size_t bucket_count = 1, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : pbds_unordered_map(init, bucket_count, hash, equal) {} friend std::ostream& operator<<(std::ostream& os, const UnorderedMap& set) { for (auto it = set.begin(); it != set.end(); ++it) { os << it->first << ' ' << it->second << '\n'; } return os; } }; template , typename KeyEqual = std::equal_to> struct UnorderedMultiMap : std::unordered_multimap { UnorderedMultiMap() : std::unordered_multimap() {} explicit UnorderedMultiMap(std::size_t bucket_count, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : std::unordered_multimap(bucket_count, hash, equal) {} template UnorderedMultiMap(InputIt first, InputIt last, std::size_t bucket_count = 1, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : std::unordered_multimap(first, last, bucket_count, hash, equal) {} UnorderedMultiMap(const std::unordered_map& other) : std::unordered_multimap(other) {} UnorderedMultiMap(std::unordered_map&& other) noexcept : std::unordered_multimap(std::move(other)) {} UnorderedMultiMap(std::initializer_list> init, std::size_t bucket_count = 1, const Hash& hash = Hash(), const KeyEqual& equal = KeyEqual()) : std::unordered_multimap(init, bucket_count, hash, equal) {} friend std::ostream& operator<<(std::ostream& os, const UnorderedMultiMap& map) { for (auto it = map.begin(); it != map.end(); ++it) { os << it->first << ' ' << it->second << '\n'; } return os; } }; namespace std { template struct hash> { size_t operator()(const UnorderedMap& map) const { Vector vec(map.begin(), map.end()); sort(vec.begin(), vec.end()); return hash>()(vec); } }; template struct hash> { size_t operator()(const UnorderedMultiMap& map) const { Vector vec(map.begin(), map.end()); sort(vec.begin(), vec.end()); return hash>()(vec); } }; } #line 7 "library/KowerKoint/base.hpp" #define REP(i, n) for(int i = 0; i < (int)(n); i++) #define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++) #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define END(...) { print(__VA_ARGS__); return; } using VI = Vector; using VVI = Vector; using VVVI = Vector; using ll = long long; using VL = Vector; using VVL = Vector; using VVVL = Vector; using ull = unsigned long long; using VUL = Vector; using VVUL = Vector; using VVVUL = Vector; using VD = Vector; using VVD = Vector; using VVVD = Vector; using VS = Vector; using VVS = Vector; using VVVS = Vector; using VC = Vector; using VVC = Vector; using VVVC = Vector; using P = Pair; using VP = Vector

; using VVP = Vector; using VVVP = Vector; using LP = Pair; using VLP = Vector; using VVLP = Vector; using VVVLP = Vector; template using PQ = priority_queue; template using GPQ = priority_queue, greater>; constexpr int INF = 1001001001; constexpr ll LINF = 1001001001001001001ll; constexpr int DX[] = {1, 0, -1, 0}; constexpr int DY[] = {0, 1, 0, -1}; void print() { cout << '\n'; } template void print(const T &t) { cout << t << '\n'; } template void print(const Head &head, const Tail &... tail) { cout << head << ' '; print(tail...); } #ifdef DEBUG void dbg() { cerr << '\n'; } template void dbg(const T &t) { cerr << t << '\n'; } template void dbg(const Head &head, const Tail &... tail) { cerr << head << ' '; dbg(tail...); } #else template void dbg(const Args &... args) {} #endif template Vector> split(typename vector::const_iterator begin, typename vector::const_iterator end, T val) { Vector> res; Vector cur; for(auto it = begin; it != end; it++) { if(*it == val) { res.push_back(cur); cur.clear(); } else cur.push_back(*it); } res.push_back(cur); return res; } Vector split(typename string::const_iterator begin, typename string::const_iterator end, char val) { Vector res; string cur = ""; for(auto it = begin; it != end; it++) { if(*it == val) { res.push_back(cur); cur.clear(); } else cur.push_back(*it); } res.push_back(cur); return res; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template pair> compress(const vector &a) { int n = a.size(); Vector x; REP(i, n) x.push_back(a[i]); sort(ALL(x)); x.erase(unique(ALL(x)), x.end()); VI res(n); REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin(); return make_pair(res, x); } template auto rle(It begin, It end) { Vector> res; if(begin == end) return res; auto pre = *begin; int num = 1; for(auto it = begin + 1; it != end; it++) { if(pre != *it) { res.emplace_back(pre, num); pre = *it; num = 1; } else num++; } res.emplace_back(pre, num); return res; } template Vector> rle_sort(It begin, It end) { Vector cloned(begin, end); sort(ALL(cloned)); auto e = rle(ALL(cloned)); sort(ALL(e), [](const auto& l, const auto& r) { return l.second < r.second; }); return e; } template Pair, Vector> factorial(int n) { Vector res(n+1), rev(n+1); res[0] = 1; REP(i, n) res[i+1] = res[i] * (i+1); rev[n] = 1 / res[n]; for(int i = n; i > 0; i--) { rev[i-1] = rev[i] * i; } return make_pair(res, rev); } #line 3 "library/KowerKoint/operator.hpp" template T add_op(T a, T b) { return a + b; } template T sub_op(T a, T b) { return a - b; } template T zero_e() { return T(0); } template T div_op(T a, T b) { return a / b; } template T mult_op(T a, T b) { return a * b; } template T one_e() { return T(1); } template T xor_op(T a, T b) { return a ^ b; } template T and_op(T a, T b) { return a & b; } template T or_op(T a, T b) { return a | b; } ll mod3() { return 998244353LL; } ll mod7() { return 1000000007LL; } ll mod9() { return 1000000009LL; } template T max_op(T a, T b) { return max(a, b); } template T min_op(T a, T b) { return min(a, b); } template T max_e() { return numeric_limits::max(); } template T min_e() { return numeric_limits::min(); } #line 3 "library/KowerKoint/integer/extgcd.hpp" ll extgcd(ll a, ll b, ll& x, ll& y) { x = 1, y = 0; ll nx = 0, ny = 1; while(b) { ll q = a / b; tie(a, b) = LP(b, a % b); tie(x, nx) = LP(nx, x - nx*q); tie(y, ny) = LP(ny, y - ny*q); } return a; } #line 2 "library/KowerKoint/integer/pow-mod.hpp" ll inv_mod(ll n, ll m) { n %= m; if (n < 0) n += m; ll x, y; assert(extgcd(n, m, x, y) == 1); x %= m; if(x < 0) x += m; return x; } ll pow_mod(ll a, ll n, ll m) { if(n == 0) return 1LL; if(n < 0) return inv_mod(pow_mod(a, -n, m), m); a %= m; if (a < 0) n += m; ll res = 1; while(n) { if(n & 1) { res *= a; res %= m; } n >>= 1; a *= a; a %= m; } return res; } #line 4 "library/KowerKoint/integer/modint.hpp" template struct Modint { ll val; Modint(): val(0) {} Modint(ll x): val(x) { val %= mod(); if(val < 0) val += mod(); } Modint& operator+=(const Modint& r) { val += r.val; if(val >= mod()) val -= mod(); return *this; } friend Modint operator+(const Modint& l, const Modint& r) { return Modint(l) += r; } Modint& operator-=(const Modint& r) { val -= r.val; if(val < 0) val += mod(); return *this; } friend Modint operator-(const Modint& l, const Modint& r) { return Modint(l) -= r; } Modint& operator*=(const Modint& r) { val *= r.val; val %= mod(); return *this; } Modint operator*(const Modint& r) { return (Modint(*this) *= r); } friend Modint operator*(const Modint& l, const Modint& r) { return Modint(l) *= r; } Modint pow(ll n) const { return Modint(pow_mod(val, n, mod())); } Modint inv() const { return Modint(inv_mod(val, mod())); } Modint& operator/=(const Modint& r) { return (*this *= r.inv()); } friend Modint operator/(const Modint& l, const Modint& r) { return Modint(l) /= r; } Modint& operator^=(const ll n) { val = pow_mod(val, n, mod()); return *this; } Modint operator^(const ll n) { return this->pow(n); } Modint operator+() const { return *this; } Modint operator-() const { return Modint() - *this; } Modint& operator++() { val++; if(val == mod()) val = 0LL; return *this; } Modint& operator++(int) { Modint res(*this); ++*this; return res; } Modint& operator--() { if(val == 0LL) val = mod(); val--; return *this; } Modint& operator--(int) { Modint res(*this); --*this; return res; } friend bool operator==(const Modint& l, const Modint& r) { return l.val == r.val; } friend bool operator!=(const Modint& l, const Modint& r) { return l.val != r.val; } static Pair, Vector> factorial(int n) { Vector fact(n+1), rfact(n+1); fact[0] = 1; REP(i, n) fact[i+1] = fact[i] * (i+1); rfact[n] = 1 / fact[n]; for(int i = n-1; i >= 0; i--) rfact[i] = rfact[i+1] * (i+1); return {fact, rfact}; } friend istream& operator>>(istream& is, Modint& mi) { is >> mi.val; return is; } friend ostream& operator<<(ostream& os, const Modint& mi) { os << mi.val; return os; } }; namespace std { template struct hash> { size_t operator()(const Modint &p) const { return hash()(p.val); } }; } using MI3 = Modint; using V3 = Vector; using VV3 = Vector; using VVV3 = Vector; using MI7 = Modint; using V7 = Vector; using VV7 = Vector; using VVV7 = Vector; using MI9 = Modint; using V9 = Vector; using VV9 = Vector; using VVV9 = Vector; #line 3 "library/KowerKoint/counting/counting.hpp" template struct Counting { Vector fact, ifact; Counting() {} Counting(ll n) { assert(n >= 0); expand(n); } void expand(ll n) { assert(n >= 0); ll sz = (ll)fact.size(); if(sz > n) return; fact.resize(n+1); ifact.resize(n+1); fact[0] = 1; FOR(i, max(1LL, sz), n+1) fact[i] = fact[i-1] * i; ifact[n] = 1 / fact[n]; for(ll i = n-1; i >= sz; i--) ifact[i] = ifact[i+1] * (i+1); } T p(ll n, ll r) { if(n < r) return 0; assert(r >= 0); expand(n); return fact[n] * ifact[n-r]; } T c(ll n, ll r) { if(n < r) return 0; assert(r >= 0); expand(n); return fact[n] * ifact[r] * ifact[n-r]; } T h(ll n, ll r) { assert(n >= 0); assert(r >= 0); return c(n+r-1, r); } T stirling(ll n, ll k) { if(n < k) return 0; assert(k >= 0); if(n == 0) return 1; T res = 0; int sign = k%2? -1 : 1; expand(k); REP(i, k+1) { res += sign * ifact[i] * ifact[k-i] * T(i).pow(n); sign *= -1; } return res; } Vector> stirling_table(ll n, ll k) { assert(n >= 0 && k >= 0); Vector> res(n+1, Vector(k+1)); res[0][0] = 1; FOR(i, 1, n+1) FOR(j, 1, k+1) { res[i][j] = res[i-1][j-1] + j * res[i-1][j]; } return res; } T bell(ll n, ll k) { assert(n >= 0 && k >= 0); expand(k); Vector tmp(k+1); int sign = 1; tmp[0] = 1; FOR(i, 1, k+1) { sign *= -1; tmp[i] = tmp[i-1] + sign * ifact[i]; } T res = 0; REP(i, k+1) { res += T(i).pow(n) * ifact[i] * tmp[k-i]; } return res; } Vector> partition_table(ll n, ll k) { assert(n >= 0 && k >= 0); Vector> res(n+1, Vector(k+1)); REP(i, k+1) res[0][i] = 1; FOR(i, 1, n+1) FOR(j, 1, k+1) { res[i][j] = res[i][j-1] + (i struct Matrix { int n, m; Vector> A; Matrix() : n(0), m(0), A(Vector>(0)) {} Matrix(size_t _n, size_t _m) : n(_n), m(_m), A(_n, Vector(_m, zero())) {} Matrix(const vector>& _A) : n(_A.size()), m(_A[0].size()), A(_A) {} Vector &operator[](int i) { assert(0 <= i && i < n); return A.at(i); } const Vector &operator[](int i) const { assert(0 <= i && i < n); return A.at(i); } static Matrix I(size_t n) { assert(n >= 0); Matrix ret(n, n); REP(i, n) ret[i][i] = one(); return ret; } Matrix &operator+=(const Matrix &B) { assert(n == B.n && m == B.m); REP(i, n) REP(j, m) A[i][j] = add(A[i][j], B[i][j]); return *this; } Matrix operator+(const Matrix &B) const { assert(n == B.n && m == B.m); return (Matrix(*this) += B); } Matrix &operator-=(const Matrix &B) { assert(n == B.n && m == B.m); REP(i, n) REP(j, m) A[i][j] = sub(A[i][j], B[i][j]); return *this; } Matrix operator-(const Matrix &B) const { assert(n == B.n && m == B.m); return (Matrix(*this) -= B); } Matrix &operator*=(const Matrix &B) { assert(m == B.n); Vector> res(n, Vector(B.m, zero())); REP(i, n) REP(j, m) REP(k, B.m) res[i][k] = add(res[i][k], mult(A[i][j], B[j][k])); A.swap(res); m = B.m; return (*this); } Matrix operator*(const Matrix &B) const { assert(m == B.n); return (Matrix(*this) *= B); } Matrix &operator|=(const Matrix &B) { assert(B.n == n); REP(i, n) { A[i].resize(m+B.m); REP(j, B.m) A[i][m+j] = B[i][j]; } m += B.m; return (*this); } Matrix operator|(const Matrix &B) const { assert(B.n == n); return (Matrix(*this) |= B); } Matrix &operator|=(const vector &B) { assert(B.size() == n); REP(i, n) { A[i].push_back(B[i]); } m++; return (*this); } Matrix operator|(const vector &B) const { assert(B.size() == n); return (Matrix(*this) |= B); } Matrix &operator&=(const Matrix &B) { assert(B.m == m); A.resize(n+B.n); REP(i, B.n) { A[n+i] = B[i]; } n += B.n; return (*this); } Matrix operator&(const Matrix &B) const { assert(B.m == m); return (Matrix(*this) &= B); } Matrix &operator&=(const vector &B) { assert(B.size() == m); A.push_back(B); n++; return (*this); } Matrix operator&(const vector &B) const { assert(B.size() == m); return (Matrix(*this) &= B); } friend istream &operator>>(istream &is, Matrix &mat) { REP(i, mat.n) REP(j, mat.m) is >> mat[i][j]; return is; } friend ostream &operator<<(ostream &os, const Matrix &mat) { REP(i, mat.n) { REP(j, mat.m) os << mat[i][j] << (j==mat.m-1? '\n' : ' '); } return os; } Pair gaussian_elimination() const { Matrix mat(*this); T det = one(); VI columns; int i = 0; int j = 0; while(i < n && j < m) { int idx = -1; FOR(k, i, n) if(mat[k][j] != zero()) idx = k; if(idx == -1) { det = zero(); j++; continue; } if(i != idx) { det *= sub(zero(), one()); swap(mat[i], mat[idx]); } det *= mat[i][j]; T scale = mat[i][j]; REP(l, m) mat[i][l] = div(mat[i][l], scale); FOR(k, i+1, n) { T scale = mat[k][j]; REP(l, m) mat[k][l] = sub(mat[k][l], mult(mat[i][l], scale)); } columns.push_back(j); i++; j++; } REP(i, columns.size()) { int j = columns[i]; REP(k, i) { T scale = mat[k][j]; FOR(l, j, m) { mat[k][l] = sub(mat[k][l], mult(mat[i][l], scale)); } } } return {mat, det}; } void make_basis() { *this = gaussian_elimination().first; while(n && get_bra(n-1) == Vector(m, zero())) pop_bra(); } Matrix inv() const { Matrix and_i = (*this) | I(n); auto [i_and, det] = and_i.gaussian_elimination(); assert(det != zero()); Matrix res(n, n); REP(i, n) REP(j, n) res[i][j] = i_and[i][n+j]; return res; } Vector get_bra(int i) const { assert(0 <= i && i < n); return A[i]; } Vector get_ket(int i) const { assert(0 <= i && i < m); Vector res(n); REP(i, n) res[i] = A[i][i]; return res; } void pop_bra() { assert(n > 0); A.pop_back(); n--; } void pop_ket() { assert(m > 0); REP(i, n) A[i].pop_back(); m--; } Matrix transpose() const { Matrix res(m, n); REP(i, n) REP(j, m) res[j][i] = A[i][j]; return res; } Matrix operator^=(ll k) { if(k < 0) { *this = this->inv(); k = -k; } Matrix res = Matrix::I(n); while(k) { if(k & 1) res *= *this; *this *= *this; k >>= 1LL; } A.swap(res.A); return (*this); } Matrix operator^(const ll k) const { return (Matrix(*this) ^= k); } }; using XorMatrix = Matrix< int, xor_op, zero_e, and_op, one_e, xor_op, and_op >; #line 3 "Contests/main.cpp" /* #include */ /* using namespace atcoder; */ /* #include "KowerKoint/expansion/ac-library/all.hpp" */ void solve(){ ll n; cin >> n; Matrix m(2, 2); m[0][0] = m[0][1] = m[1][0] = 1; m ^= n-1; print(m[0][0] + m[0][1] - 1); } // generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator) int main() { // Fasterize input/output script ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(100); // scanf/printf user should delete this fasterize input/output script int t = 1; //cin >> t; // comment out if solving multi testcase for(int testCase = 1;testCase <= t;++testCase){ solve(); } return 0; }