PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000108. Dãy con có cách chọn duy nhất (Unique Subsequence)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Dãy con có cách chọn duy nhất (Unique Subsequence)

Unique Subsequence

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an integer array A=(A1,A2,…,An)A=(A_1,A_2,\ldots,A_n). A non-empty subsequence s=(s1,s2,…,sk)s=(s_1,s_2,\ldots,s_k) is extracted from AA by selecting indices 1≤i1<i2<⋯<ik≤n1\le i_1<i_2<\cdots<i_k\le n with Ait=stA_{i_t}=s_t for every 1≤t≤k1\le t\le k.

Because AA may contain equal elements, the same sequence of values ss may be obtained from several distinct sets of indices. We accept ss only if there is exactly one increasing index sequence that extracts it.

For example, when A=(1,2,1)A=(1,2,1), the subsequence (1)(1) is excluded because it can use position 11 or position 33. In contrast, (1,1)(1,1) has exactly one extraction, using positions (1,3)(1,3).

Count the distinct non-empty value sequences ss with a unique extraction. Do not count the same value sequence more than once. Print the answer modulo 998244353998244353.

Input

  • The first line contains the integer nn, the array length.
  • The second line contains exactly nn integers A1,A2,…,AnA_1,A_2,\ldots,A_n. Values may repeat.

Output

Print one integer: the number of non-empty value sequences with exactly one extraction from AA, modulo 998244353998244353.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤Ai≤n1\le A_i\le n for every 1≤i≤n1\le i\le n; repeated values are allowed.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤Ai≤n1\le A_i\le n for every 1≤i≤n1\le i\le n; repeated values are allowed.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤Ai≤n1\le A_i\le n for every 1≤i≤n1\le i\le n; repeated values are allowed.

Examples

Example 1

Input

3
1 2 1

Output

5

Explanation

The five uniquely extractable value sequences are (1,1)(1,1) from indices (1,3)(1,3), (1,2)(1,2) from (1,2)(1,2), (1,2,1)(1,2,1) from (1,2,3)(1,2,3), (2)(2) from index 22, and (2,1)(2,1) from (2,3)(2,3). The value sequence (1)(1) has two extractions (index 11 or 33), so it is excluded. Hence the answer is 5.

Example 2

Input

4
4 2 1 3

Output

15

Explanation

All four values 4,2,1,34,2,1,3 are distinct, so two different index selections cannot yield the same value sequence. Every non-empty subsequence is uniquely extractable. There are 24−1=152^4-1=15 non-empty index subsets, and the answer is 15.

Example 3

Input

12
1 2 3 6 9 2 3 3 9 6 1 6

Output

1178

Explanation

Process all 12 positions using the DP of uniquely extractable index sequences. The per-position values f1,…,f12f_1,\ldots,f_{12} are (1,2,4,8,16,30,58,58,104,200,393,593)(1,2,4,8,16,30,58,58,104,200,393,593). After removing the outdated state at each repeated value, the sum of active states, including the empty selection, is 11791179. Removing the empty selection gives 1179−1=11781179-1=1178. Repeated values 2,3,9,6,12,3,9,6,1 explain why the naive 212−12^{12}-1 count is incorrect.