C - Election Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

今年の AtCoder 市長選挙には A 候補と B 候補の 2 人が立候補し、N 人の有権者が投票しました。 投票者にはそれぞれ 1 から N までの番号が付けられており、投票者 i (1 \leq i \leq N)c_i 候補に投票しました。

さて、これから開票作業が行われます。 開票作業では 1 票ずつ票が開けられていき、票が開けられるたびに、現時点での開票結果が以下の 3 つのうちどれであるかが発表されます。

  • 結果 A: 現時点で、A 候補の方が獲得票数が多い。
  • 結果 B: 現時点で、B 候補の方が獲得票数が多い。
  • 結果 C: 現時点で、A 候補と B 候補の獲得票数が同数である。

ここで開票の順番にはルールがあり、投票者 1 以外の票は、投票者の番号の小さい順に開票されなければなりません。 (投票者 1 の票は好きなタイミングで開票してかまいません)

発表される開票結果の列としてあり得るものが何通りあるかを答えてください。

開票結果の列とはi 票目 (1 \leq i \leq N) が開けられたタイミングで報告された結果を s_i (A, B, C のいずれか) とするとき,文字列 s_1 s_2 \dots s_N のことを指します。


制約

  • N2 \leq N \leq 1000000 を満たす整数
  • c_1, c_2, \dots, c_NA または B

入力

入力は以下の形式で標準入力から与えられます。

N
c_1 c_2 \cdots c_N

出力

答えを出力してください。


入力例 1

4
AABB

出力例 1

3

この入力例では、開票が行われる順番として以下の 4 通りが考えられます。

  • 投票者 1 \to 2 \to 3 \to 4 の順に開票が行われる。
  • 投票者 2 \to 1 \to 3 \to 4 の順に開票が行われる。
  • 投票者 2 \to 3 \to 1 \to 4 の順に開票が行われる。
  • 投票者 2 \to 3 \to 4 \to 1 の順に開票が行われる。

開票結果の列は上から順に AAACAAACACACACBC となるため、開票結果の列としてあり得るものは 3 通りです。


入力例 2

4
AAAA

出力例 2

1

どのような順序で開票を行っても、開票結果の列は AAAA となります。


入力例 3

10
BBBAAABBAA

出力例 3

5

入力例 4

172
AABAAAAAABBABAABBBBAABBAAABBABBABABABBAAABAAABAABAABBBBABBBABBABBBBBBBBAAABAAABAAABABBBAABAAAABABBABBABBBBBABAABAABBBABABBAAAABAABABBBABAAAABBBBABBBABBBABAABBBAAAABAAABAAAB

出力例 4

24

Score: 600 points

Problem Statement

This year's AtCoder mayoral election has two candidates, A and B, and N voters have cast their votes. The voters are assigned numbers from 1 to N, and voter i (1 \leq i \leq N) voted for candidate c_i.

Now, the votes will be counted. As each vote is counted, the provisional result will be announced as one of the following three outcomes:

  • Result A: Currently, candidate A has more votes.
  • Result B: Currently, candidate B has more votes.
  • Result C: Currently, candidates A and B have the same number of votes.

There is a rule regarding the order of vote counting: votes from all voters except voter 1 must be counted in ascending order of their voter numbers. (The vote from voter 1 may be counted at any time.)

How many different sequences of provisional results could be announced?

What is a sequence of provisional results?Let s_i be the result (A, B, or C) reported when the i-th vote (1 \leq i \leq N) is counted. The sequence of provisional results refers to the string s_1 s_2 \dots s_N.


Constraints

  • N is an integer such that 2 \leq N \leq 1000000.
  • Each of c_1, c_2, \dots, c_N is A or B.

Input

The input is given from Standard Input in the following format:

N
c_1 c_2 \cdots c_N

Output

Print the answer.


Sample Input 1

4
AABB

Sample Output 1

3

In this sample input, there are four possible orders in which the votes may be counted:

  • The votes are counted in the order of voter 1 \to 2 \to 3 \to 4.
  • The votes are counted in the order of voter 2 \to 1 \to 3 \to 4.
  • The votes are counted in the order of voter 2 \to 3 \to 1 \to 4.
  • The votes are counted in the order of voter 2 \to 3 \to 4 \to 1.

The sequences of preliminary results for these will be AAAC, AAAC, ACAC, ACBC from top to bottom, so there are three possible sequences of preliminary results.


Sample Input 2

4
AAAA

Sample Output 2

1

No matter the order of counting, the sequence of preliminary results will be AAAA.


Sample Input 3

10
BBBAAABBAA

Sample Output 3

5

Sample Input 4

172
AABAAAAAABBABAABBBBAABBAAABBABBABABABBAAABAAABAABAABBBBABBBABBABBBBBBBBAAABAAABAAABABBBAABAAAABABBABBABBBBBABAABAABBBABABBAAAABAABABBBABAAAABBBBABBBABBBABAABBBAAAABAAABAAAB

Sample Output 4

24