#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll MOD = 1000000007; ll mod_pow(ll x, ll n, ll mod = MOD) { ll res = 1; while (n > 0) { if (n & 1) { res = res * x % mod; } x = x * x % mod; n >>= 1; } return res; } ll mod_inverse(ll x, ll mod = MOD) { return mod_pow(x, mod - 2, mod); } int main() { int Q; cin >> Q; vector A; vector B; for (int i = 0; i < Q; ++i) { ll a, b; cin >> a >> b; A.push_back(a); B.push_back(b); } vector C = A; sort(C.begin(), C.end()); ll i = 1; ll val = 1; map memo; for (ll c : C) { while (i <= c && val > 0) { val *= i; val %= MOD; ++i; } memo[c] = val; } for (int i = 0; i < A.size(); ++i) { ll a = A[i]; ll b = B[i]; ll v1 = memo[a]; ll v2 = b * (1LL + b) * mod_inverse(2); cout << v1 * v2 % MOD << endl; } return 0; }