fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4. const ll mod = 1e9+7;
  5.  
  6. ll pow(ll a,ll b, ll m){
  7. ll ans=1;
  8. while(b){
  9. if(b&1)
  10. ans=(ans*a)%m;
  11. b /= 2;
  12. a = (a*a)%m;
  13. }
  14. return ans;
  15. }
  16. int main()
  17. {
  18. int t;
  19. ll n,m,ans1,ans2;
  20. cin >> t;
  21. while(t--){
  22. cin >> n >> m;
  23. ans1 = pow(m,n,mod);
  24. n = n-2;
  25. ans2 = ((n%mod)*(m%mod))%mod;
  26. cout << ans1 - ans2 << "\n";
  27. }
  28. }
Success #stdin #stdout 0s 4332KB
stdin
2 
2 2
3 4
stdout
4
60