const MOD: usize = 1e9 as usize + 7; fn power(base: usize, times: usize) -> usize { if times == 0 { return 1usize; } if times == 1 { return base; } let temp = power(base, times/2); temp * temp % MOD * power(base, times%2) % MOD } fn solve(div6: usize) -> usize { let mut nm = String::new(); std::io::stdin().read_line(&mut nm).ok(); let nm: Vec = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nm[0]; let m = nm[1]; let total = power(n, m); let total = total * (total + 1) / 2 % MOD; // n * (n + 1) * (2 * n + 1) / 6 let upper = power(n, m/2) * if m % 2 == 1 { (n as f64).sqrt().floor() as usize } else { 1 } % MOD; let sqsum = upper * div6 % MOD * (upper + 1) % MOD * (2 * upper + 1) % MOD; (MOD + total - sqsum) % MOD } fn main() { let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); let t: usize = t.trim().parse().unwrap(); let mut result = Vec::with_capacity(t); let div6 = power(6, MOD-2); for _ in 0..t { result.push(solve(div6)); } for &v in result.iter() { println!("{}", v); } }