#line 1 "test.cpp" //#pragma GCC target("avx2") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") #include using namespace std; #ifdef LOCAL #include #define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (static_cast(0)) #endif using ll = long long; using ld = long double; using pll = pair; using pii = pair; using vi = vector; using vvi = vector; using vvvi = vector; using vl = vector; using vvl = vector; using vvvl = vector; using vpii = vector; using vpll = vector; using vs = vector; template using pq = priority_queue,greater>; #define overload4(_1, _2, _3, _4, name, ...) name #define overload3(a,b,c,name,...) name #define rep1(n) for (ll UNUSED_NUMBER = 0; UNUSED_NUMBER < (n); ++UNUSED_NUMBER) #define rep2(i, n) for (ll i = 0; i < (n); ++i) #define rep3(i, a, b) for (ll i = (a); i < (b); ++i) #define rep4(i, a, b, c) for (ll i = (a); i < (b); i += (c)) #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep1(n) for(ll i = (n) - 1;i >= 0;i--) #define rrep2(i,n) for(ll i = (n) - 1;i >= 0;i--) #define rrep3(i,a,b) for(ll i = (b) - 1;i >= (a);i--) #define rrep4(i,a,b,c) for(ll i = (a) + ((b)-(a)-1) / (c) * (c);i >= (a);i -= c) #define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__) #define all1(i) begin(i),end(i) #define all2(i,a) begin(i),begin(i)+a #define all3(i,a,b) begin(i)+a,begin(i)+b #define all(...) overload3(__VA_ARGS__, all3, all2, all1)(__VA_ARGS__) #define sum(...) accumulate(all(__VA_ARGS__),0LL) template bool chmin(T &a, const T &b){ if(a > b){ a = b; return 1; } else return 0; } template bool chmax(T &a, const T &b){ if(a < b){ a = b; return 1; } else return 0; } template auto min(const T& a){ return *min_element(all(a)); } template auto max(const T& a){ return *max_element(all(a)); } template void in(Ts&... t); #define INT(...) int __VA_ARGS__; in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__; in(__VA_ARGS__) #define STR(...) string __VA_ARGS__; in(__VA_ARGS__) #define CHR(...) char __VA_ARGS__; in(__VA_ARGS__) #define DBL(...) double __VA_ARGS__; in(__VA_ARGS__) #define LD(...) ld __VA_ARGS__; in(__VA_ARGS__) #define VEC(type, name, size) vector name(size); in(name) #define VV(type, name, h, w) vector> name(h, vector(w)); in(name) ll intpow(ll a, ll b){ ll ans = 1; while(b){if(b & 1) ans *= a; a *= a; b /= 2;} return ans;} ll modpow(ll a, ll b, ll p){ ll ans = 1; a %= p;while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } ll GCD(ll a,ll b) { if(a == 0 || b == 0) return a + b; if(a % b == 0) return b; else return GCD(b,a%b);} ll LCM(ll a,ll b) { if(a == 0) return b; if(b == 0) return a;return a / GCD(a,b) * b;} namespace IO{ #define VOID(a) decltype(void(a)) struct setting{ setting(){cin.tie(nullptr); ios::sync_with_stdio(false);fixed(cout); cout.precision(12);}} setting; template struct P : P{}; template<> struct P<0>{}; template void i(T& t){ i(t, P<3>{}); } void i(vector::reference t, P<3>){ int a; i(a); t = a; } template auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; } template auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); } template void ituple(T& t, index_sequence){ in(get(t)...);} template auto i(T& t, P<0>) -> VOID(tuple_size{}){ ituple(t, make_index_sequence::value>{});} #undef VOID } #define unpack(a) (void)initializer_list{(a, 0)...} template void in(Ts&... t){ unpack(IO :: i(t)); } #undef unpack //constexpr int mod = 1000000007; constexpr int mod = 998244353; static const double PI = 3.1415926535897932; template struct REC { F f; REC(F &&f_) : f(forward(f_)) {} template auto operator()(Args &&...args) const { return f(*this, forward(args)...); }}; #line 2 "library/modint/Modint.hpp" template struct Modint{ int x; Modint():x(0) {} Modint(long long y): x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} Modint &operator += (const Modint &p) { if((x += p.x) >= mod) x -= mod; return *this;} Modint &operator -= (const Modint &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this;} Modint &operator *= (const Modint &p) { x = (int)(1LL * x * p.x % mod); return *this;} Modint &operator /= (const Modint &p) { *this *= p.inverse(); return *this;} Modint operator -() const{return Modint(-x);} Modint operator +(const Modint &p) const {return Modint(*this) += p;} Modint operator -(const Modint &p) const {return Modint(*this) -= p;} Modint operator *(const Modint &p) const {return Modint(*this) *= p;} Modint operator /(const Modint &p) const {return Modint(*this) /= p;} Modint &operator ++() {if(x == mod - 1) x = 0; else x++; return *this;} Modint &operator --() {if(x == 0) x = mod - 1; else x--; return *this;} bool operator == (const Modint &p) const {return x == p.x;} bool operator != (const Modint &p) const {return x != p.x;} Modint inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return Modint(u);} Modint pow(long long n) const { Modint ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret;} friend ostream &operator<<(ostream &os, const Modint &p) { return os << p.x; } friend istream &operator>>(istream &is, Modint &a) { long long t; is >> t; a = Modint(t); return (is); } static constexpr int get_mod() {return mod;} }; #line 87 "test.cpp" using mint = Modint; using vm = vector; using vvm = vector; using vvvm = vector; #line 2 "hashmap/hashmap.hpp" #line 2 "hashmap/hashmap-base.hpp" namespace HashMapImpl { using u32 = uint32_t; using u64 = uint64_t; template struct HashMapBase; template struct itrB : iterator { using base = iterator; using ptr = typename base::pointer; using ref = typename base::reference; u32 i; HashMapBase* p; explicit constexpr itrB() : i(0), p(nullptr) {} explicit constexpr itrB(u32 _i, HashMapBase* _p) : i(_i), p(_p) {} explicit constexpr itrB(u32 _i, const HashMapBase* _p) : i(_i), p(const_cast*>(_p)) {} friend void swap(itrB& l, itrB& r) { swap(l.i, r.i), swap(l.p, r.p); } friend bool operator==(const itrB& l, const itrB& r) { return l.i == r.i; } friend bool operator!=(const itrB& l, const itrB& r) { return l.i != r.i; } const ref operator*() const { return const_cast*>(p)->data[i]; } ref operator*() { return p->data[i]; } ptr operator->() const { return &(p->data[i]); } itrB& operator++() { assert(i != p->cap && "itr::operator++()"); do { i++; if (i == p->cap) break; if (p->flag[i] == true && p->dflag[i] == false) break; } while (true); return (*this); } itrB operator++(int) { itrB it(*this); ++(*this); return it; } itrB& operator--() { do { i--; if (p->flag[i] == true && p->dflag[i] == false) break; assert(i != 0 && "itr::operator--()"); } while (true); return (*this); } itrB operator--(int) { itrB it(*this); --(*this); return it; } }; template struct HashMapBase { using u32 = uint32_t; using u64 = uint64_t; using iterator = itrB; using itr = iterator; protected: template inline u64 randomized(const K& key) const { return u64(key) ^ r; } template ::value, nullptr_t> = nullptr, enable_if_t::value, nullptr_t> = nullptr> inline u32 inner_hash(const K& key) const { return (randomized(key) * 11995408973635179863ULL) >> shift; } template < typename K, enable_if_t::value, nullptr_t> = nullptr, enable_if_t::value, nullptr_t> = nullptr, enable_if_t::value, nullptr_t> = nullptr> inline u32 inner_hash(const K& key) const { u64 a = randomized(key.first), b = randomized(key.second); a *= 11995408973635179863ULL; b *= 10150724397891781847ULL; return (a + b) >> shift; } template ::value, nullptr_t> = nullptr, enable_if_t::value, nullptr_t> = nullptr> inline u32 inner_hash(const K& key) const { static constexpr u64 mod = (1LL << 61) - 1; static constexpr u64 base = 950699498548472943ULL; u64 res = 0; for (auto& elem : key) { __uint128_t x = __uint128_t(res) * base + (randomized(elem) & mod); res = (x & mod) + (x >> 61); } __uint128_t x = __uint128_t(res) * base; res = (x & mod) + (x >> 61); if (res >= mod) res -= mod; return res >> (shift - 3); } template ::value, nullptr_t> = nullptr> inline u32 hash(const D& dat) const { return inner_hash(dat); } template < typename D = Data, enable_if_t::value, nullptr_t> = nullptr> inline u32 hash(const D& dat) const { return inner_hash(dat.first); } template ::value, nullptr_t> = nullptr> inline Key dtok(const D& dat) const { return dat; } template < typename D = Data, enable_if_t::value, nullptr_t> = nullptr> inline Key dtok(const D& dat) const { return dat.first; } void reallocate(u32 ncap) { vector ndata(ncap); vector nf(ncap); shift = 64 - __lg(ncap); for (u32 i = 0; i < cap; i++) { if (flag[i] == true && dflag[i] == false) { u32 h = hash(data[i]); while (nf[h]) h = (h + 1) & (ncap - 1); ndata[h] = move(data[i]); nf[h] = true; } } data.swap(ndata); flag.swap(nf); cap = ncap; dflag.resize(cap); fill(std::begin(dflag), std::end(dflag), false); } inline bool extend_rate(u32 x) const { return x * 2 >= cap; } inline bool shrink_rate(u32 x) const { return HASHMAP_DEFAULT_SIZE < cap && x * 10 <= cap; } inline void extend() { reallocate(cap << 1); } inline void shrink() { reallocate(cap >> 1); } public: u32 cap, s; vector data; vector flag, dflag; u32 shift; static u64 r; static constexpr uint32_t HASHMAP_DEFAULT_SIZE = 4; explicit HashMapBase() : cap(HASHMAP_DEFAULT_SIZE), s(0), data(cap), flag(cap), dflag(cap), shift(64 - __lg(cap)) {} itr begin() const { u32 h = 0; while (h != cap) { if (flag[h] == true && dflag[h] == false) break; h++; } return itr(h, this); } itr end() const { return itr(this->cap, this); } friend itr begin(const HashMapBase& h) { return h.begin(); } friend itr end(const HashMapBase& h) { return h.end(); } itr find(const Key& key) const { u32 h = inner_hash(key); while (true) { if (flag[h] == false) return this->end(); if (dtok(data[h]) == key) { if (dflag[h] == true) return this->end(); return itr(h, this); } h = (h + 1) & (cap - 1); } } bool contain(const Key& key) const { return find(key) != this->end(); } itr insert(const Data& d) { u32 h = hash(d); while (true) { if (flag[h] == false) { if (extend_rate(s + 1)) { extend(); h = hash(d); continue; } data[h] = d; flag[h] = true; ++s; return itr(h, this); } if (dtok(data[h]) == dtok(d)) { if (dflag[h] == true) { data[h] = d; dflag[h] = false; ++s; } return itr(h, this); } h = (h + 1) & (cap - 1); } } // tips for speed up : // if return value is unnecessary, make argument_2 false. itr erase(itr it, bool get_next = true) { if (it == this->end()) return this->end(); s--; if (shrink_rate(s)) { Data d = data[it.i]; shrink(); it = find(dtok(d)); } int ni = (it.i + 1) & (cap - 1); if (this->flag[ni]) { this->dflag[it.i] = true; } else { this->flag[it.i] = false; } if (get_next) ++it; return it; } itr erase(const Key& key) { return erase(find(key)); } bool empty() const { return s == 0; } int size() const { return s; } void clear() { fill(std::begin(flag), std::end(flag), false); fill(std::begin(dflag), std::end(dflag), false); s = 0; } void reserve(int n) { if (n <= 0) return; n = 1 << min(23, __lg(n) + 2); if (cap < u32(n)) reallocate(n); } }; template uint64_t HashMapBase::r = chrono::duration_cast( chrono::high_resolution_clock::now().time_since_epoch()) .count(); } // namespace HashMapImpl /** * @brief Hash Map(base) (ハッシュマップ・基底クラス) */ #line 4 "hashmap/hashmap.hpp" template struct HashMap : HashMapImpl::HashMapBase> { using base = typename HashMapImpl::HashMapBase>; using HashMapImpl::HashMapBase>::HashMapBase; using Data = pair; Val& operator[](const Key& k) { typename base::u32 h = base::inner_hash(k); while (true) { if (base::flag[h] == false) { if (base::extend_rate(base::s + 1)) { base::extend(); h = base::hash(k); continue; } base::data[h].first = k; base::data[h].second = Val(); base::flag[h] = true; ++base::s; return base::data[h].second; } if (base::data[h].first == k) { if (base::dflag[h] == true) base::data[h].second = Val(); return base::data[h].second; } h = (h + 1) & (base::cap - 1); } } typename base::itr emplace(const Key& key, const Val& val) { return base::insert(Data(key, val)); } }; /* * @brief ハッシュマップ(連想配列) * @docs docs/hashmap/hashmap.md **/ int main() { INT(n); VEC(int,a,n); vvi last(n+1,vi(30,-1)); rep(i,n) { rep(j,30) { if(a[i] >> j & 1) last[i+1][j] = i; else last[i+1][j] = last[i][j]; } } HashMap dp; dp[n] = 1; rrep(i,n) { set s; HashMap ndp; rep(j,30) { if(~a[i] >> j & 1) { if(last[i+1][j] != -1) s.insert(last[i+1][j]); } } for(auto &x:s) { mint tmp; for(auto &[pre,c]:dp) { if(pre > x) tmp += c; } ndp[x] = tmp; } for(auto &[pre,c]:dp) { if(pre > i) ndp[i] += c; else ndp[pre] += c; } swap(dp,ndp); } mint ans; for(auto &[_,c]:dp) ans += c; cout << ans << '\n'; }