Codeforces Round 604 (Div. 1)
D2
Beautiful Bracket Sequence (hard version)
Codeforces
2900
原题
← D1E →
翻译中…

This is the hard version of this problem. The only difference is the limit of $n$ - the length of the input string. In this version, $1 \leq n \leq 10^6$.

翻译中…

Let's define a correct bracket sequence and its depth as follow:

翻译中…
  • An empty string is a correct bracket sequence with depth $0$.
  • If "s" is a correct bracket sequence with depth $d$ then "(s)" is a correct bracket sequence with depth $d + 1$.
  • If "s" and "t" are both correct bracket sequences then their concatenation "st" is a correct bracket sequence with depth equal to the maximum depth of $s$ and $t$.
翻译中…

For a (not necessarily correct) bracket sequence $s$, we define its depth as the maximum depth of any correct bracket sequence induced by removing some characters from $s$ (possibly zero). For example: the bracket sequence $s = $"())(())" has depth $2$, because by removing the third character we obtain a correct bracket sequence "()(())" with depth $2$.

翻译中…

Given a string $a$ consists of only characters '(', ')' and '?'. Consider all (not necessarily correct) bracket sequences obtained by replacing all characters '?' in $a$ by either '(' or ')'. Calculate the sum of all the depths of all these bracket sequences. As this number can be large, find it modulo $998244353$.

翻译中…

Hacks in this problem can be done only if easy and hard versions of this problem was solved.

翻译中…

Input

翻译中…

The only line contains a non-empty string consist of only '(', ')' and '?'. The length of the string is at most $10^6$.

翻译中…

Output

翻译中…

Print the answer modulo $998244353$ in a single line.

翻译中…

Note

翻译中…

In the first test case, we can obtain $4$ bracket sequences by replacing all characters '?' with either '(' or ')':

翻译中…
  • "((". Its depth is $0$;
  • "))". Its depth is $0$;
  • ")(". Its depth is $0$;
  • "()". Its depth is $1$.
翻译中…

So, the answer is $1 = 0 + 0 + 0 + 1$.

翻译中…

In the second test case, we can obtain $4$ bracket sequences by replacing all characters '?' with either '(' or ')':

翻译中…
  • "(((())". Its depth is $2$;
  • "()()))". Its depth is $2$;
  • "((()))". Its depth is $3$;
  • "()(())". Its depth is $2$.
翻译中…

So, the answer is $9 = 2 + 2 + 3 + 2$.

翻译中…

样例

Input #1
??
Output #1
1
InputCopy OutputCopy
Input #2
(?(?))
Output #2
9
InputCopy OutputCopy
题目信息
题号
1264D2
时间限制
2 s
内存限制
256 MB
标签
组合数学
概率论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。