#include #include #include #include using namespace std; const int mod = 1000000007; const int inf = 1012345678; int sz, seg[524295]; void update(int pos, int val) { pos += sz; seg[pos] = val; while (pos > 1) { pos >>= 1; seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]); } } int query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return -inf; if (a <= l && r <= b) return seg[k]; int lc = query(a, b, k * 2, l, (l + r) >> 1); int rc = query(a, b, k * 2 + 1, (l + r) >> 1, r); return max(lc, rc); } unordered_map bit[200009]; void add(int f, int x, int val) { for (int i = x + 1; i <= sz; i += i & (-i)) { bit[f][i] += val; if (bit[f][i] >= mod) bit[f][i] -= mod; } } int sum(int f, int r) { int ans = 0; for (int i = r; i >= 1; i -= i & (-i)) { ans += bit[f][i]; if (ans >= mod) ans -= mod; } return ans; } int main() { int N; cin >> N; vector A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } vector perm(N); for (int i = 0; i < N; ++i) { perm[i] = i; } sort(perm.begin(), perm.end(), [&](int i, int j) { return A[i] != A[j] ? A[i] < A[j] : i > j; }); sz = 1; while (sz < N) sz *= 2; vector dp(N); for (int i = 0; i < N; ++i) { dp[perm[i]] = query(0, perm[i], 1, 0, sz) + 1; if (dp[perm[i]] <= 0) dp[perm[i]] = 1; update(perm[i], dp[perm[i]]); } vector ways(N); for (int i = 0; i < N; ++i) { if (dp[perm[i]] == 1) { ways[perm[i]] = 1; } else { ways[perm[i]] = sum(dp[perm[i]] - 1, perm[i]); } add(dp[perm[i]], perm[i], ways[perm[i]]); } int len = *max_element(dp.begin(), dp.end()); int ans = 0; for (int i = 0; i < N; ++i) { if (dp[i] == len) { ans += ways[i]; if (ans >= mod) ans -= mod; } } cout << ans << endl; return 0; }