#P8229. [AGM 2022 资格赛] 抛硬币

[AGM 2022 资格赛] 抛硬币

Problem Description

Yili found a magic number KK, a magic basket full of strawberries, and a magic coin.

Yili noticed that when flipping the coin, it has a probability of PP to land heads up. Then she flips the coin nn times. If the coin lands heads up, the strawberries in the basket become KK times the original amount. Otherwise, she will eat all the strawberries in the basket. After she finishes eating, the basket will magically produce one new strawberry.

Yili is now curious about the expected number of strawberries she will eat after flipping the coin nn times. Please tell her the answer modulo 998244353998244353.

Input Format

The first line contains an integer TT, indicating the number of test cases.

The next TT lines each contain three integers n,K,Pn, K, P.

Output Format

Output TT lines. Each line contains a positive integer representing the answer.

4
1 2 499122177
3 2 499122177
10 2 499122177
1000000000000000000 100 16235235
499122177
748683267
748683281
565383148

Hint

Constraints

For 100%100\% of the testdata, it holds that 1T1051 \leq T \leq 10^5, 1n10181 \leq n \leq 10^{18}, and 0K,P<9982443530 \leq K, P < 998244353.

Notes

Translated from AGM 2022 Qualification Round A CoinFlip

Translated by ChatGPT 5