#include <bits/stdc++.h>
#define rep(i,n) for(int i=int(0);i<int(n);++i)
using namespace std;

using ll=long long;

#define error(args...) { vector<string> _debug = split(#args, ',');err(begin(_debug), args);}

vector<string> split(const string& s, char c){
	vector<string> v;stringstream ss(s);string x;
	while (getline(ss, x, c)) v.emplace_back(x);
	return move(v);
}

void err(vector<string>::iterator it) {cerr << endl;}
template<typename T, typename... Args> void err(vector<string>::iterator it, T a,Args... args){
	cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << " ",err(++it, args...);
}

vector<int> primes;
int p, phi;

inline int power(int a, int x){
	int b = 1;
	while(x){
		if(x & 1) b = 1LL * b * a % p;
		a = 1LL * a * a % p;
		x >>= 1; 
	}
	return b;
}

void init(){
	phi = p - 1;
	ll cur = phi;
	for(ll i = 2LL; i * i <= cur; ++i){
		if(cur % i) continue;
		ll tmp = 1LL;
		while(cur % i == 0){
			tmp *= i;
			primes.push_back(tmp);
			cur /= i;
		}
	}
	if(cur != 1) primes.push_back(cur);
}

inline ll gcd(ll a,ll b){
	if(b == 0) return a;
	return gcd(b, a % b);
}

inline ll lcm(ll a, ll b){
	return a / gcd(a,b) * b;
}

ll calc(int val){
	int ret = 1;
	for(auto &elem:primes){
		const int tmp = power(val, phi / elem);
		if(tmp == 1){
			ret = lcm(ret, elem);
		}
	}
	return ret;
}

int ary[100010];

int main(void){

	int t;
	scanf("%ld %d", &p, &t);
	init();

	rep(loop, t){
		int k,a;
		scanf("%d", &k);
		rep(i, k) scanf("%d", ary +i);
		scanf("%d", &a);
		const ll goal = calc(a);
		const int all = p - 1;
		int period = p - 1;

		bool ok = false;
		rep(i, k){
			const ll idx = calc(ary[i]);
			period = gcd(period, idx);
		}

		if(goal % period == 0) ok = true;
		printf("%d\n", ok);
	}

	return 0;
}
