My logic is the exact same as the editorial, however it is giving WA, I have tried as many test cases as possible (all outputs being correct), even checked for long long, % 1e9 + 7 errors, etc. Can anyone figure out what is wrong with this code?
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <iterator>
#include <set>
#include <map>
#include <numeric>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define PB push_back
#define F first
#define S second
#define M 1000000007ll
ll fact(ll n)
{
ll fact = 1, i;
for (i = 2; i <= n; i++)
fact *= i; fact %= M;
return fact;
}
void solve() {
ll n; int k; ll a;
cin >> n >> k; vector<ll> as(n);
bool odd = false; bool even = false;
ll co = 0; ll ce = 0;
for (ll i = 0; i < n; i++) {
cin >> as[i];
if (as[i] % 2) {odd = true; co += 1;} else {even = true; ce += 1;}
}
if (k == 0) {
if (odd && even) {cout << 0 << "\n";}
else {
cout << fact(n) << "\n";
}
}
else {
if (n % 2) {
if (abs(co - ce) == 1) {
cout << (fact(n / 2 + 1) * fact(n / 2)) % M << "\n";
}
else {
cout << 0 << "\n";
}
}
else {
if (co == ce) {
cout << ((fact(n / 2) * fact(n / 2) % M) * 2) % M << "\n";
}
else {
cout << 0 << "\n";
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll t;
cin >> t;
while(t--) {solve();}
}