#include #include using namespace std; using namespace atcoder; using ll=long long; using ld=long double; ld pie=3.14159265359; ll inf=1001; ll mod=1000000007; ll modpow(ll x, ll n) { x = x%mod; if(n==0) return 1; //再帰の終了条件 else if(n%2==1) { return (x*modpow(x, n-1))%mod; //nが奇数ならnを1ずらす } else return modpow((x*x)%mod, n/2)%mod; //nが偶数ならnが半分になる } int main(){ ll t; cin >> t; for (ll i = 0; i < t; i++) { ll n,m; cin >> n >> m; ll x=modpow(n,m); ll ans=(x*(x+1))%mod; ans*=inv_mod(2,mod); ans%=mod; ll z=modpow(n,m/2); ll y=(z*(z+1))%mod; y*=(2*z+1)%mod; y%=mod; y*=inv_mod(6,mod); y%=mod; ans+=mod-y; ans%=mod; cout << ans << endl; } }