#P8356. 「WHOI-1」数列计数

「WHOI-1」数列计数

Background

No longer having it, the sequence stays with me.

Problem Description

This kind of sequence satisfies the following magical property:

  • a0=0a_0 = 0.
  • i[1,n]\forall i \in [1,n], we have ai=ai1+xa_i = a_{i-1} + x or ai=ai1+ya_i = a_{i-1} + y.
  • i[1,n],pai\forall i \in [1,n], p \nmid a_i.

Find the number of such {a}0n\{a\}_0^{n}. Output the answer modulo 109+710^9+7.

Two sequences are different if and only if there exists an index where the stored element is different.

Input Format

One input file contains multiple test cases.

The first line contains a positive integer TT, the number of test cases.

The next TT lines describe the test cases. For each test case, one line contains four positive integers, denoting n,p,x,yn, p, x, y.

Output Format

Output TT lines, each line containing one non-negative integer, representing the answer for that test case.

3
3 3 1 2
11 45 14 19
9876 10 114514 191981
2
1688
426554662

Hint

Sample #1:

Such aa are [0,1,2,4],[0,2,4,5][0,1,2,4], [0,2,4,5].

Sample #2 and #3:

The originally cute Otm has already written tens of thousands of pages of sample explanations, but the even cuter miku deleted them, so Otm does not want to write them again.


This problem uses the Subtask\texttt{Subtask} scoring method. You can get the score of a Subtask\texttt{Subtask} only if you pass all test cases in that Subtask\texttt{Subtask}.

Subtask\texttt{Subtask} ID Special Constraints Score
1 n20\sum n \leq 20 10
2 p103p \leq 10^3 30
3 xy,pxy, p are coprime 10
4 None 50

For all testdata, 1T1031 \leq T \leq 10^3, 1n1041 \leq \sum n \leq 10^4, 1x,y,p1091 \leq x,y,p \leq 10^9. All inputs are positive integers.

Translated by ChatGPT 5