#P7725. 珍珠帝王蟹(Crab King)
珍珠帝王蟹(Crab King)
Background
During a voyage, you accidentally discover a king crab surrounded by reefs. After being eroded by Moon Island energy, what connection does it have with moonlight? It seems that only by defeating it can you find out.
Problem Description
The king crab can be challenged by embedding gems. Different gems have different effects, but strangely, the order in which the gems are embedded sometimes also affects its strength.
The king crab has a strength value initially equal to . Each gem has attributes and , meaning:
- If is
+, then after embedding, the king crab’s strength value will increase by . - If is
*, then after embedding, the king crab’s strength value will be multiplied by .
Because the gems’ effects are very strange, may be negative.
As an adventurer who loves challenges, you want to choose an embedding order such that each gem is embedded exactly once, and the king crab’s strength value is maximized.
You only need to output the maximum strength value modulo . Note that this is a number in .
That is, if the answer is ans, in C++ syntax you need to output (ans % P + P) % P, where P = 998244353.
Input Format
The first line contains an integer , representing the number of gems.
The next lines each contain a character and an integer separated by a space, describing a gem.
Output Format
Output one integer on one line, representing the maximum strength value modulo .
3
+ -3
+ 4
* -4
16
3
+ -3
+ -4
* 4
998244346
Hint
[Sample 1 Explanation]
Following the input order, label the three gems as . All possible embedding orders are:
: $x = ((0 + {\color{red}{-3}}) + {\color{red}{4}}) \times {\color{red}{-4}} = -4$;
: $x = ((0 + {\color{red}{-3}}) \times {\color{red}{-4}}) + {\color{red}{4}} = 16$;
: $x = ((0 + {\color{red}{4}}) + {\color{red}{-3}}) \times {\color{red}{-4}} = -4$;
: $x = ((0 + {\color{red}{4}}) \times {\color{red}{-4}}) + {\color{red}{-3}} = -19$;
: $x = ((0 \times {\color{red}{-4}}) + {\color{red}{-3}}) + {\color{red}{4}} = 1$;
: $x = ((0 \times {\color{red}{-4}}) + {\color{red}{4}}) + {\color{red}{-3}} = 1$。
Therefore, the maximum strength value is , and after taking modulo it is .
[Sample 2 Explanation]
Following the input order, label the three gems as . All possible embedding orders are:
: $x = ((0 + {\color{red}{-3}}) + {\color{red}{-4}}) \times {\color{red}{4}} = -28$;
: $x = ((0 + {\color{red}{-3}}) \times {\color{red}{4}}) + {\color{red}{-4}} = -16$;
: $x = ((0 + {\color{red}{-4}}) + {\color{red}{-3}}) \times {\color{red}{4}} = -28$;
: $x = ((0 + {\color{red}{-4}}) \times {\color{red}{4}}) + {\color{red}{-3}} = -19$;
: $x = ((0 \times {\color{red}{4}}) + {\color{red}{-3}}) + {\color{red}{-4}} = -7$;
: $x = ((0 \times {\color{red}{4}}) + {\color{red}{-4}}) + {\color{red}{-3}} = -7$。
Therefore, the maximum strength value is , and after taking modulo it is .
[Constraints]
This problem uses bundled testdata.
For all testdata: , .
- Subtask 1 (26 points): , .
- Subtask 2 (22 points): .
- Subtask 3 (12 points): Guarantee that when is
*, . - Subtask 4 (15 points): Guarantee that when is
+, . - Subtask 5 (25 points): No special constraints.

Translated by ChatGPT 5