#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const unsigned MODVAL = 1000000007;
struct mint
{
	unsigned val;
	mint() :val(0) {}
	mint(int      x) :val(x%MODVAL) {}
	mint(unsigned x) :val(x%MODVAL) {}
	mint(LL       x) :val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val + y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val - y.val + MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x += y; }
mint operator-(mint x, mint y) { return x -= y; }
mint operator*(mint x, mint y) { return x *= y; }

mint POW(mint x, LL e) { mint v = 1; for (; e; x *= x, e >>= 1) if (e & 1) v *= x; return v; }
mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL - 2); }
mint operator/(mint x, mint y) { return x /= y; }

LL eulerPhi(LL n) {
	LL ans = n;
	for (LL x = 2; x*x <= n; ++x) {
		if (n % x == 0) {
			ans -= ans / x;
			while (n % x == 0) n /= x;
		}
	}
	if (n > 1) ans -= ans / n;
	return ans;
}

class ModEquation { public:
	vector <int> count(int n, int K, vector <int> query)
	{
		int epk = (int)eulerPhi(K);

		vector<pair<int, int>> dK;
		int v = K;
		for (int p = 2; p*p <= v; ++p)
			if (v%p == 0) {
				int cnt = 0;
				while (v%p == 0) cnt++, v /= p;
				dK.emplace_back(p, cnt);
			}
		if (v != 1)
			dK.emplace_back(v, 1);

		vector<int> ans;
		for (int q : query)
			ans.push_back(solve(n, K, epk, dK, q).val);
		return ans;
	}

	// # of solutions in mod K world s.t. x[0] x[1] ... x[n-1] = q
	//
	// * n <= 50
	// * K = BIG
	// * must be solved 1000 times in time limits.
	mint solve(int n, int K, int epk, const vector<pair<int,int>>& dK, int q)
	{
		if (n == 1)
			return 1;

		mint ans = POW(epk, n-1);

		for (auto pe : dK) {
			int p = pe.first;
			int e = pe.second;
			int qq = q;
			int cnt = 0;
			while (qq%p == 0 && cnt<e)
				cnt++, qq /= p;
			// p^cnt is in gcd(K,q)
			if (cnt < e) {
				ans *= POW(n, cnt); // div dups...
			}
			else {
				ans *= POW(n, cnt); // or more...
			}
		}

		return ans;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const vector <int>& Expected, const vector <int>& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, ModEquation().count(n, K, query));}
int main(){

CASE(0)
	int n = 2; 
	int K = 2; 
	int query_[] = {0,1};
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	int __[] = {3, 1 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(1)
	int n = 2; 
	int K = 4; 
	int query_[] = {0,1,2,3};
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	int __[] = {8, 2, 4, 2 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	int n = 6; 
	int K = 6; 
	int query_[] = {4};
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	int __[] = {2016 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(3)
	int n = 1; 
	int K = 2; 
	int query_[] = {0,0,0,1,1,1};
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	int __[] = {1, 1, 1, 1, 1, 1 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
/*
CASE(4)
	int n = ; 
	int K = ; 
	int query_[] = ;
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	int __[] = ;
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(5)
	int n = ; 
	int K = ; 
	int query_[] = ;
	  vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); 
	int __[] = ;
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
*/
}
// END CUT HERE
