You are given two integers $N$ and $M$. Find the number of sequences $A_1, A_2, \ldots, A_N$, where each element is an integer between $1$ and $M$ (inclusive) and no three consecutive elements are equal. Since this number could be very large, compute it modulo $10^9+7$.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $N$ and $M$.

-----Output-----
For each test case, print a single line containing one integer ― the number of valid sequences modulo $10^9+7$.

-----Constraints-----
- $1 \le T \le 10^5$
- $1 \le N, M \le 10^{18}$

-----Subtasks-----
Subtask #1 (50 points):
- $T \le 20$
- $N \le 10^5$
Subtask #2 (50 points): original constraints

-----Example Input-----
2 
2 2
3 4

-----Example Output-----
4
60

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = '# cook your dish her\ntry:from cp import input,setInputFile\nexcept:pass\nimport math \nimport copy\n# import random\nimport numpy as np\nimport sys\n# import time\ndef input(): return sys.stdin.readline()\ndef itarr(arr): return list(range(len(arr)))\ndef mp():return list(map(int,input().split()))\ndef lmp():return list(map(int,input().split()))\ndef inp():return int(input())\nMOD=1000000007\n# from functools import lru_cach\ndef mexp(m,p):\n if p==1:\n  return m%MOD\n x=(m%MOD * m%MOD )%MOD\n r=mexp(x%MOD,p//2)\n if p%2==0:\n  return r%MOD\n else:\n  return (m%MOD*r%MOD)%MOD\n\ndef main():\n n,b=mp()\n t=b%MOD\n g=(b*b)%MOD\n n%=MOD\n if n==2:\n  print(g)\n  return \n if n==1:\n  print(t)\n  return\n\n MI=np.matrix(np.array([[(b-1)%MOD,(b-1)%MOD],[1,0]]))\n\n arr= mexp(MI,n-2) \n arr=np.array(arr)\n ans = (arr[0][0]*g)%MOD + (arr[0][1]*t)%MOD\n ans%=MOD\n print(ans)\n\n\n \n \n\n# main()\n\nfor _ in range(int(input())):\n main()\n # break\n\n\n\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 6604712