#include using namespace std; #include using dbs = tr2::dynamic_bitset<>; //* #define INCLUDE_MODINT //*/ #ifdef INCLUDE_MODINT #include using namespace atcoder; // using mint = modint998244353; using mint = modint1000000007; // using mint = modint; #endif namespace mytemplate { using ll = long long; using dbl = double; using ld = long double; using uint = unsigned int; using ull = unsigned long long; using pll = pair; using tlll = tuple; using tllll = tuple; using vl = vector; using vpll = vector; using vtlll = vector; using vtllll = vector; using vstr = vector; using vvl = vector>; #ifdef __SIZEOF_INT128__ using i128 = __int128_t; i128 stoi128(string s) { i128 res = 0; if (s.front() == '-') { for (int i = 1; i < (int)s.size(); i++) res = 10 * res + s[i] - '0'; res = -res; } else { for (auto c : s) res = 10 * res + c - '0'; } return res; } string i128tos(i128 x) { string sign = "", res = ""; if (x < 0) x = -x, sign = "-"; while (x > 0) { res += '0' + x % 10; x /= 10; } reverse(res.begin(), res.end()); if (res == "") return "0"; return sign + res; } istream &operator>>(istream &is, i128 &a) { string s; is >> s; a = stoi128(s); return is; } ostream &operator<<(ostream &os, const i128 &a) { os << i128tos(a); return os; } #endif #define overload4(_1, _2, _3, _4, name, ...) name #define rep1(i, n) for (ll i = 0; i < ll(n); i++) #define rep2(i, l, r) for (ll i = ll(l); i < ll(r); i++) #define rep3(i, l, r, d) for (ll i = ll(l); (d) > 0 ? i < ll(r) : i > ll(r); i += d) #define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define ALL(a) (a).begin(), (a).end() const ll INF = 4'000'000'000'000'000'037; bool chmin(auto &a, const auto &b) { return a > b ? a = b, true : false; } bool chmax(auto &a, const auto &b) { return a < b ? a = b, true : false; } template T1 safemod(auto a, auto m) { T1 res = a % m; if (res < 0) res += m; return res; } template T1 divfloor(auto a, auto b) { if (b < 0) a = -a, b = -b; return (a - safemod(a, b)) / b; } template T1 divceil(auto a, auto b) { if (b < 0) a = -a, b = -b; return divfloor(a + b - 1, b); } template T1 pow_ll(auto a, auto b) { if (a == 0) return b == 0 ? 1 : 0; if (a == 1) return a; if (a == -1) return b & 1 ? -1 : 1; ll res = 1; rep(_, b) res *= a; return res; } template T1 mul_limited(auto a, auto b, T1 m = INF) { return b == 0 ? 0 : a > m / b ? m : a * b; } template T1 pow_limited(auto a, auto b, T1 m = INF) { if (a == 0) return b == 0 ? 1 : 0; if (a == 1) return a; ll res = 1; rep(_, b) { if (res > m / a) return m; res *= a; } return res; } template vector b_ary(T1 x, int b) { vector a; while (x > 0) { a.emplace_back(x % b); x /= b; } reverse(a.begin(), a.end()); return a; } template vector b_ary(T1 x, int b, int n) { vector a(n); rep(i, n) { a[i] = x % b; x /= b; } reverse(a.begin(), a.end()); return a; } template string b_ary_str(T1 x, int b) { auto a = b_ary(x, b); string s = ""; for (auto &&ai : a) s += (ai < 10 ? '0' + ai : 'A' + (ai - 10)); return s; } template string b_ary_str(T1 x, int b, int n) { auto a = b_ary(x, b, n); string s = ""; for (auto &&ai : a) s += (ai < 10 ? '0' + ai : 'A' + (ai - 10)); return s; } template vector> iprod(const vector &a) { vector> res; vector tmp(a.size()); auto dfs = [&](auto self, int i) { if (i == (int)a.size()) { res.emplace_back(tmp); return; } rep(j, a[i]) { tmp[i] = j; self(self, i + 1); } }; dfs(dfs, 0); return res; } template struct max_op { T operator()(const T &a, const T &b) const { return max(a, b); } }; template struct min_op { T operator()(const T &a, const T &b) const { return min(a, b); } }; template struct const_fn { T operator()() const { return val; } }; using max_e = const_fn; using min_e = const_fn; using zero_fn = const_fn; template vector digitvec(const string &s) { int n = s.size(); vector a(n); rep(i, n) a[i] = s[i] - '0'; return a; } template auto make_vec(const auto (&sz)[d], const T &init) { if constexpr (i < d) return vector(sz[i], make_vec(sz, init)); else return init; } template vector permid(int n, int base_index = 0) { vector p(n); rep(i, n) p[i] = i + base_index; return p; } template vector perminv(const vector &p) { vector q(p.size()); rep(i, p.size()) q[p[i]] = i; return q; } template vector combid(int n, int k) { vector p(n, 0); fill(p.rbegin(), p.rbegin() + k, 1); return p; } template auto gen_vec(int n, const F &f) { using T = decltype(f(0)); vector res(n); rep(i, n) res[i] = f(i); return res; } // res[i] = op[0, i) for 0 <= i < n+1 template ())> vector cuml(vector v, const F &op = plus<>(), const T &e = 0) { v.emplace_back(e); exclusive_scan(v.begin(), v.end(), v.begin(), e, op); return v; } // res[i] = op[i, n) for 0 <= i < n+1 template ())> vector cumr(vector v, const F &op = plus<>(), const T &e = 0) { v.insert(v.begin(), e); exclusive_scan(v.rbegin(), v.rend(), v.rbegin(), e, op); return v; } // res[i] = v[i] - v[i-1] for 0 <= i < n+1 template vector adjd(vector v) { v.emplace_back(0); adjacent_difference(v.begin(), v.end(), v.begin()); return v; } template vector cumlmax(const vector &v) { return cuml(v, max_op(), max_e()()); } template vector cumrmax(const vector &v) { return cumr(v, max_op(), max_e()()); } template vector cumlmin(const vector &v) { return cuml(v, min_op(), min_e()()); } template vector cumrmin(const vector &v) { return cumr(v, min_op(), min_e()()); } template vector sorted(vector v) { sort(v.begin(), v.end()); return v; } template vector reversed(const vector &v) { return {v.rbegin(), v.rend()}; } template void unique(vector &v) { v.erase(unique(v.begin(), v.end()), v.end()); } template vector uniqued(vector v) { v.erase(unique(v.begin(), v.end()), v.end()); return v; } template void sortunique(vector &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } template vector sortuniqued(vector v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v; } template void rotate(vector &v, int k) { rotate(v.begin(), v.begin() + k, v.end()); } template vector rotated(vector v, int k) { rotate(v.begin(), v.begin() + k, v.end()); return v; } string sorted(string s) { sort(s.begin(), s.end()); return s; } string reversed(const string &s) { return {s.rbegin(), s.rend()}; } void unique(string &s) { s.erase(unique(s.begin(), s.end()), s.end()); } string uniqued(string s) { s.erase(unique(s.begin(), s.end()), s.end()); return s; } void sortunique(string &s) { sort(s.begin(), s.end()); s.erase(unique(s.begin(), s.end()), s.end()); } string sortuniqued(string s) { sort(s.begin(), s.end()); s.erase(unique(s.begin(), s.end()), s.end()); return s; } void rotate(string &s, int k) { rotate(s.begin(), s.begin() + k, s.end()); } string rotated(string s, int k) { rotate(s.begin(), s.begin() + k, s.end()); return s; } template vector> top(const vector> &a) { if (a.empty()) return {}; const size_t n = a.size(), m = a[0].size(); vector> b(m, vector(n)); for (size_t i = 0; i < n; i++) for (size_t j = 0; j < m; j++) b[j][i] = a[i].at(j); return b; } vstr top(const vstr &a) { if (a.empty()) return {}; const size_t n = a.size(), m = a[0].size(); vstr b(m, string(n, 0)); for (size_t i = 0; i < n; i++) for (size_t j = 0; j < m; j++) b[j][i] = a[i].at(j); return b; } template vector> rot90(const vector> &a) { if (a.empty()) return {}; const size_t n = a.size(), m = a[0].size(); vector> b(m, vector(n)); for (size_t i = 0; i < n; i++) for (size_t j = 0; j < m; j++) b[j][n - 1 - i] = a[i][j]; return b; } vstr rot90(const vstr &a) { if (a.empty()) return {}; const size_t n = a.size(), m = a[0].size(); vstr b(m, string(n, 0)); for (size_t i = 0; i < n; i++) for (size_t j = 0; j < m; j++) b[j][n - 1 - i] = a[i][j]; return b; } #if __cplusplus < 202002L ull bit_ceil(ull x) { ull y = 1; while (y < x) y <<= 1; return y; } ull bit_floor(ull x) { ull y = 1; while (y <= x) y <<= 1; return y >> 1; } ull bit_width(ull x) { ull y = 1, z = 0; while (y <= x) y <<= 1, z++; return z; } ull countr_zero(ull x) { return __builtin_ctzll(x); } ull popcount(ull x) { return __builtin_popcountll(x); } ull has_single_bit(ull x) { return popcount(x) == 1; } #endif ull lsb_pos(ull x) { assert(x != 0); return countr_zero(x); } ull msb_pos(ull x) { assert(x != 0); return bit_width(x) - 1; } ull lsb_mask(ull x) { assert(x != 0); return x & -x; } ull msb_mask(ull x) { assert(x != 0); return bit_floor(x); } bool btest(ull x, uint k) { return (x >> k) & 1; } template void bset(T &x, uint k, bool b = 1) { b ? x |= (1ULL << k) : x &= ~(1ULL << k); } template void bflip(T &x, uint k) { x ^= (1ULL << k); } bool bsubset(ull x, ull y) { return (x & y) == x; } template vector> bsubsets(T x) { vector> res; for (T y = x; y > 0; y = (y - 1) & x) res.emplace_back(make_pair(y, x & ~y)); res.emplace_back(make_pair(0, x)); return res; } template Tuple tuple_add(Tuple &a, const Tuple &b, const index_sequence) { ((get(a) += get(b)), ...); return a; } template Tuple operator+=(Tuple &a, const Tuple &b) { return tuple_add(a, b, make_index_sequence>{}); } template Tuple operator+(Tuple a, const Tuple &b) { return a += b; } template void offset(vector &v, U add) { for (auto &vi : v) vi += add; } template void offset(vector> &v, U add) { for (auto &vi : v) for (auto &vij : vi) vij += add; } template array, m> top(const vector> &a) { const size_t n = a.size(); array, m> b; b.fill(vector(n)); for (size_t i = 0; i < n; i++) for (size_t j = 0; j < m; j++) b[j][i] = a[i][j]; return b; } template vector> top(const array, n> &a) { if (a.empty()) return {}; const size_t m = a[0].size(); vector> b(m); for (size_t i = 0; i < n; i++) for (size_t j = 0; j < m; j++) b[j][i] = a[i].at(j); return b; } template pair, vector> top(const vector> &a) { const size_t n = a.size(); vector b(n); vector c(n); for (size_t i = 0; i < n; i++) tie(b[i], c[i]) = a[i]; return make_pair(b, c); } template vector> top(const pair, vector> &a) { const size_t n = a.first.size(); vector> b(n); for (size_t i = 0; i < n; i++) b[i] = make_pair(a.first[i], a.second.at(i)); return b; } template tuple, vector, vector> top(const vector> &a) { const size_t n = a.size(); vector b(n); vector c(n); vector d(n); for (size_t i = 0; i < n; i++) tie(b[i], c[i], d[i]) = a[i]; return make_tuple(b, c, d); } template vector> top(const tuple, vector, vector> &a) { const size_t n = get<0>(a).size(); vector> b(n); for (size_t i = 0; i < n; i++) b[i] = make_tuple(get<0>(a)[i], get<1>(a).at(i), get<2>(a).at(i)); return b; } template tuple, vector, vector, vector> top(const vector> &a) { const size_t n = a.size(); vector b(n); vector c(n); vector d(n); vector e(n); for (size_t i = 0; i < n; i++) tie(b[i], c[i], d[i], e[i]) = a[i]; return make_tuple(b, c, d, e); } template vector> top(const tuple, vector, vector, vector> &a) { const size_t n = get<0>(a).size(); vector> b(n); for (size_t i = 0; i < n; i++) b[i] = make_tuple(get<0>(a)[i], get<1>(a).at(i), get<2>(a).at(i), get<3>(a).at(i)); return b; } #ifdef INCLUDE_MODINT using namespace atcoder; template * = nullptr> istream &operator>>(istream &is, T &a) { ll v; is >> v; a = v; return is; } template * = nullptr> ostream &operator<<(ostream &os, const T &a) { os << a.val(); return os; } #define MINT(...) mint __VA_ARGS__; INPUT(__VA_ARGS__) #endif template ::value == true> * = nullptr> istream &operator>>(istream &is, Tuple &t) { apply([&](auto&... a){ (is >> ... >> a); }, t); return is; } template void INPUT(T&... a) { (cin >> ... >> a); } template void INPUTVEC(int n, vector &v) { v.resize(n); rep(i, n) cin >> v[i]; } template void INPUTVEC(int n, vector& v, vector&... vs) { INPUTVEC(n, v); INPUTVEC(n, vs...); } template void INPUTVEC2(int n, int m, vector> &v) { v.assign(n, vector(m)); rep(i, n) rep(j, m) cin >> v[i][j]; } template void INPUTVEC2(int n, int m, vector& v, vector&... vs) { INPUTVEC2(n, m, v); INPUTVEC2(n, m, vs...); } #define INT(...) int __VA_ARGS__; INPUT(__VA_ARGS__) #define LL(...) ll __VA_ARGS__; INPUT(__VA_ARGS__) #define STR(...) string __VA_ARGS__; INPUT(__VA_ARGS__) #define ARR(T, n, ...) array __VA_ARGS__; INPUT(__VA_ARGS__) #define VEC(T, n, ...) vector __VA_ARGS__; INPUTVEC(n, __VA_ARGS__) #define VEC2(T, n, m, ...) vector> __VA_ARGS__; INPUTVEC2(n, m, __VA_ARGS__) template void PRINT(const T &a) { cout << a << '\n'; } template void PRINT(const T& a, const Ts&... b) { cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; } template void PRINTVEC(const vector &v) { int n = v.size(); rep(i, n) cout << v[i] << (i == n - 1 ? "" : " "); cout << '\n'; } template void PRINTVECT(const vector &v) { for (auto &vi : v) cout << vi << '\n';} template void PRINTVEC2(const vector> &v) { for (auto &vi : v) PRINTVEC(vi); } #define PRINTEXIT(...) do { PRINT(__VA_ARGS__); exit(0); } while (false) #define PRINTRETURN(...) do { PRINT(__VA_ARGS__); return; } while (false) #ifdef LOCAL // ttps://zenn.dev/sassan/articles/19db660e4da0a4 #include "/Users/Shared/cpp-dump-main/dump.hpp" #define dump(...) cpp_dump(__VA_ARGS__) CPP_DUMP_DEFINE_EXPORT_OBJECT(mint, val()); #ifdef INCLUDE_MODINT CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(val()); #endif #else #define dump(...) #endif } using namespace mytemplate; // idx[i] := A の i 番目が LIS に使われる可能性があるなら、どの添字で使われるか // 使われる可能性がないなら -1 // cand[j] := LIS の j 番目になりうる A の要素の添字の配列 (昇順) // 特に、cand[j].front() たちをとってきたものが辞書順最小の LIS, // cand[j].back() たちをとってきたものが辞書順最大の LIS struct LongestIncreasingSubsequence { vector idx; vector> cand; LongestIncreasingSubsequence() {} template LongestIncreasingSubsequence(const vector &a) : idx(a.size()) { const int n = a.size(); vector dp; for (int i = 0; i < n; i++) { auto it = lower_bound(dp.begin(), dp.end(), a[i]); idx[i] = it - dp.begin(); if (it == dp.end()) dp.emplace_back(a[i]); else *it = a[i]; } const int len = dp.size(); cand.resize(len); dump(idx); for (int i = n - 1, j = len - 1; i >= 0; i--) { if (idx[i] == j) cand[j--].emplace_back(i); else if (idx[i] + 1 == len || (!cand[idx[i] + 1].empty() && a[i] < a[cand[idx[i] + 1].back()])) cand[idx[i]].emplace_back(i); else idx[i] = -1; } for (auto &candj : cand) reverse(candj.begin(), candj.end()); } }; void main2() { /* https://judge.yosupo.jp/problem/longest_increasing_subsequence INT(N); VEC(int, N, A); LongestIncreasingSubsequence lis(A); dump(lis.idx, lis.cand); vector ans(lis.cand.size()); rep(i, lis.cand.size()) ans[i] = lis.cand[i].front(); PRINT(ans.size()); PRINTVEC(ans); //*/ /* https://atcoder.jp/contests/abc354/tasks/abc354_f INT(T); rep(_, T) { INT(N); VEC(int, N, A); LongestIncreasingSubsequence lis(A); vector ans; rep(i, N) if (lis.idx[i] != -1) ans.push_back(i); offset(ans, 1); PRINT(ans.size()); PRINTVEC(ans); } //*/ /* https://yukicoder.me/problems/no/1804 INT(N); VEC(int, N, P); LongestIncreasingSubsequence lis(P); dump(lis.cand); vector ans; rep(j, lis.cand.size()) { if ((int)lis.cand[j].size() == 1) { int i = lis.cand[j].front(); ans.push_back(P[i]); } } PRINT(ans.size()); PRINTVEC(ans); //*/ //* https://yukicoder.me/problems/no/992 INT(N); VEC(int, N, A); LongestIncreasingSubsequence lis(A); auto C = lis.cand; dump(C); vector dp(C.front().size(), 1), ndp; rep(i, 1, C.size()) { ndp.assign(C.at(i).size(), 0); // c[i-1][j] < c[i][k] かつ A[c[i-1][j]] > A[c[i][k]] なところに遷移 { int l = 0, r = 0; rep(j, dp.size()) { while (l < (int)C.at(i).size() && C.at(i - 1).at(j) > C.at(i).at(l)) l++; while (r < (int)C.at(i).size() && A.at(C.at(i - 1).at(j)) < A.at(C.at(i).at(r))) r++; dump(i, j, l, r); if (l < r) { ndp.at(l) += dp.at(j); if (r < (int)C.at(i).size()) ndp.at(r) -= dp.at(j); } //dump(ndp); } } rep(k, (int)ndp.size() - 1) ndp.at(k + 1) += ndp.at(k); swap(dp, ndp); dump(dp); } mint ans = reduce(ALL(dp)); PRINT(ans); //*/ } int main() { //* #ifndef LOCAL cin.tie(0); ios::sync_with_stdio(false); #endif //*/ cout << fixed << setprecision(20); int T = 1; /* cin >> T; //*/ while (T--) { main2(); } }