D - Unique Subsequence 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 700

問題文

長さ N の整数列 A_1,A_2,\cdots,A_N が与えられます.

A の非空な部分列 s であって,以下の条件を満たすものの個数を 998244353 で割った余りを求めてください.

  • A から s を取り出す方法が一意である. つまり,s=(s_1,s_2,\cdots,s_k) とした時,A_{idx(i)}=s_i (1 \leq i \leq k)を満たす添字の列 1 \leq idx(1)<idx(2)<\cdots<idx(k) \leq N がちょうど一つ存在する.

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • 入力される値はすべて整数である

入力

入力は以下の形式で標準入力から与えられる.

N
A_1 A_2 \cdots A_N

出力

答えを出力せよ.


入力例 1

3
1 2 1

出力例 1

5

以下の 5 つの部分列が条件を満たします.

  • (1,1)
  • (1,2)
  • (1,2,1)
  • (2)
  • (2,1)

部分列 (1) は取り出す方法が 2 通りあるので条件を満たしません.


入力例 2

4
4 2 1 3

出力例 2

15

入力例 3

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

出力例 3

1178

Score : 700 points

Problem Statement

Given is a sequence of N integers A_1,A_2,\cdots,A_N.

Find the number of non-empty subsequences s of A satisfying the following condition, modulo 998244353.

  • There is only one way to extract s from A. Formally, there uniquely exists a sequence of indices 1 \leq idx(1)<idx(2)<\cdots<idx(k) \leq N such that A_{idx(i)}=s_i (1 \leq i \leq k), where s=(s_1,s_2,\cdots,s_k).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

3
1 2 1

Sample Output 1

5

The following five subsequences satisfy the condition.

  • (1,1)
  • (1,2)
  • (1,2,1)
  • (2)
  • (2,1)

The subsequence (1) does not satisfy the condition since there are two ways to extract it.


Sample Input 2

4
4 2 1 3

Sample Output 2

15

Sample Input 3

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

Sample Output 3

1178