Source Code
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;

const int N = 1000001;

// array to store inverse of 1 to N
ll factorialNumInverse[N + 1];

// array to precompute inverse of 1! to N!
ll naturalNumInverse[N + 1];

// array to store factorial of first N numbers
ll fact[N + 1];

// Function to precompute inverse of numbers

void InverseofNumber(ll p)
{
	naturalNumInverse[0] = naturalNumInverse[1] = 1;

	for (int i = 2; i <= N; i++)

		naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}
// Function to precompute inverse of factorials

void InverseofFactorial(ll p)
{
	factorialNumInverse[0] = factorialNumInverse[1] = 1;

	// precompute inverse of natural numbers

	for (int i = 2; i <= N; i++)

		factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}

// Function to calculate factorial of 1 to N

void factorial(ll p)
{
	fact[0] = 1;

	// precompute factorials

	for (int i = 1; i <= N; i++)
	{
		fact[i] = (fact[i - 1] * i) % p;
	}
}

// Function to return nCr % p in O(1) time
ll nCr(ll N, ll R, ll p)
{
	// n C r = n!*inverse(r!)*inverse((n-r)!)

	ll ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p;

	return ans;
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	ll p = 1000000007;
	InverseofNumber(p);
	InverseofFactorial(p);
	factorial(p);

	int t;
	cin >> t;
	while (t--)
	{
		ll n;
		cin >> n;

		ll k = 0, ans = 0;
		while (n >= k)
		{
			ans += nCr(n, k, p);
			ans %= p;

			n--, k++;
		}
		cout << ans << "\n";
	}

	return 0;
}
Copy
Study Schedule Yamanabdullah1
GNU G++17
2097 ms
24.0 MB
Time Limit Exceeded