#P11284. 「GFOI Round 2」Strings

「GFOI Round 2」Strings

Description

Given two positive integers nn and mm.

We define a sequence of positive integers a1,a2,,aka_1, a_2, \ldots, a_k of length kk as "good" if and only if:

  • i[1,k],1aim\forall i \in [1, k], 1 \leq a_i \leq m;
  • There exists a positive integer l[1,k3]l \in [1, \frac{k}{3}] such that: i[1,l],ai=a2l+1i\forall i \in [1, l], a_i = a_{2l + 1 - i}.

You need to determine how many good sequences of length n\leq n exist, modulo 109+710^9 + 7.

Input Format

This problem has multiple test cases in a single test.

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

For each test case:

  • The first line contains two positive integers nn and mm.

Output Format

For each test case, output a line containing a non-negative integer representing the answer modulo 109+710^9 + 7.

4
3 2
5 3
10 4
100000 998244353123456
4
117
430352
967771719

Hint

Sample Explanation

For the first test case, the good sequences of length 3\le 3 are [1,1,1],[1,1,2],[2,2,1],[2,2,2][1, 1, 1], [1, 1, 2], [2, 2, 1], [2, 2, 2].

Subtasks and Constraints

Subtask ID nn \le mm \le Subtask Dependencies Score
11 101810^{18} 11 No 11
22 1010 44 77
33 10510^5 101810^{18} 22 2828
44 101810^{18} 1,2,31, 2, 3 6464

For all tests, it is guaranteed that:

  • 1T101 \le T \le 10;
  • 3n10183 \le n \le 10^{18};
  • 1m10181 \le m \le 10^{18}.