#include const int Mod = 998244353; const int inf = 0; int leaf[5001]; typedef struct { int left, right, max; } seg_node; void init_node(seg_node v[], int k, int l, int r) { v[k].left = l; v[k].right = r; v[k].max = inf; if (l < r) { init_node(v, k << 1, l, (l + r) / 2); init_node(v, (k << 1) ^ 1, (l + r) / 2 + 1, r); } else leaf[l] = k; } void update_node(seg_node v[], int k, int x) { int i, j = leaf[k]; v[j].max = x; for (i = j >> 1; i > 0; j = i, i >>= 1) v[i].max = (v[j].max > v[j^1].max)? v[j].max: v[j^1].max; } int BS_left(seg_node v[], int k, int l, int r, int x) { int tmp; if (v[k].max < x || v[k].right < l || v[k].left > r) return r + 1; else if (v[k].left == v[k].right) return v[k].left; else { tmp = BS_left(v, k << 1, l, r, x); if (tmp <= r) return tmp; else return BS_left(v, (k << 1) ^ 1, l, r, x); } } int BS_right(seg_node v[], int k, int l, int r, int x) { int tmp; if (v[k].max < x || v[k].right < l || v[k].left > r) return l - 1; else if (v[k].left == v[k].right) return v[k].left; else { tmp = BS_right(v, (k << 1) ^ 1, l, r, x); if (tmp >= l) return tmp; else return BS_right(v, k << 1, l, r, x); } } int main() { int i, N, P[5001]; scanf("%d", &N); for (i = 1, P[0] = 0; i <= N; i++) { scanf("%d", &(P[i])); P[i] = N + 1 - P[i]; } seg_node v[100000]; init_node(v, 1, 1, N); for (i = 1; i <= N; i++) update_node(v, i, P[i]); int j, k; static unsigned int dp[3][5001][5001] = {}; for (i = 1, dp[0][0][0] = 1; i <= N; i++) { dp[0][i][P[i]] = (dp[0][i-1][P[i-1]] + dp[1][i-1][0] + dp[2][i-1][P[i]]) % Mod; for (j = P[i] - 1; j >= 0; j--) dp[0][i][j] = dp[0][i][j+1]; j = BS_right(v, 1, 1, i - 1, P[i]); while (j >= 1) { dp[1][i][P[j]] = (dp[0][i-1][P[j]] + dp[1][i-1][P[j]]) % Mod; j = BS_right(v, 1, 1, j - 1, P[j]); } for (j = N - 1; j >= 0; j--) { dp[1][i][j] += dp[1][i][j+1]; if (dp[1][i][j] >= Mod) dp[1][i][j] -= Mod; } j = BS_left(v, 1, i + 1, N, P[i]); while (j <= N) { dp[2][i][P[j]] = (dp[0][i-1][P[i-1]] + dp[1][i-1][0] + dp[2][i-1][P[j]]) % Mod; j = BS_left(v, 1, j + 1, N, P[j]); } for (j = 1; j <= N; j++) { dp[2][i][j] += dp[2][i][j-1]; if (dp[2][i][j] >= Mod) dp[2][i][j] -= Mod; } } printf("%u\n", (dp[0][N][0] + dp[1][N][0] + dp[2][N][N]) % Mod); fflush(stdout); return 0; }