Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

#define ll long long
#define sf(a) scanf("%lld", &a)
#define pf(b) printf("%lld", b)
#define sp printf(" ")
#define nw printf("\\n")
#define mp make_pair
#define pi pair<ll, ll>
#define f first
#define s second
#define pb push_back
#define sor(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define fast ios_base::sync_with_stdio(false)
using namespace std;

vector<int> prime;
void sieve() {
  int sroot = 10000;
  bool a[sroot];
  memset(a, 0, sizeof(a));

  for (int i = 2; i <= sroot; i++) {
    if (a[i] == 0) {
      for (int j = i + i; j <= sroot; j = j + i) {
        a[j] = 1;
      }
    }
  }

  for (int i = 2; i <= sroot; i++) {
    if (a[i] == 0) {
      prime.push_back(i);
    }
  }
}
int mark[1000000];
int main() {

  sieve();
  int n, sprime, pprime, gc;
  sprime = pprime = 1;
  cin >> n;
  int ar[n + 1];
  for (ll i = 1; i <= n; i++) {
    cin >> ar[i];
    if (i > 1)
      gc = __gcd(gc, ar[i]);
    else
      gc = ar[i];
    int val = ar[i];
    for (ll j = 0; prime[j] * prime[j] <= val and j < prime.size(); j++) {
      while (val % prime[j] == 0) {
        mark[prime[j]]++;
        val /= prime[j];
      }
    }
    if (val > 1)
      mark[val]++;
  }
  if (gc != 1)
    sprime = 0;
  for (ll i = 1; i <= n; i++) {
    int val = ar[i];
    for (ll j = 0; prime[j] * prime[j] <= val and j < prime.size(); j++) {
      while (val % prime[j] == 0) {
        mark[prime[j]]--;
        val /= prime[j];
      }
    }
    if (val > 1)
      mark[val]--;
    val = ar[i];
    for (ll j = 0; prime[j] * prime[j] <= val and j < prime.size(); j++) {
      while (val % prime[j] == 0) {
        if (mark[prime[j]]) {
          pprime = 0;
          break;
        }
        val /= prime[j];
      }
      if (!pprime)
        break;
    }
    if (val > 1)
      if (mark[val])
        pprime = 0;
    if (!pprime)
      break;
  }
  if (pprime)
    printf("pairwise coprime");
  else if (sprime)
    printf("setwise coprime");
  else
    printf("not coprime");
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

#define ll long long
#define sf(a) scanf("%lld", &a)
#define pf(b) printf("%lld", b)
#define sp printf(" ")
#define nw printf("\\n")
#define mp make_pair
#define pi pair<ll, ll>
#define f first
#define s second
#define pb push_back
#define sor(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define fast ios_base::sync_with_stdio(false)
using namespace std;

vector<ll> prime;
void sieve() {
  int sroot = 1009;
  bool a[sroot];
  memset(a, 0, sizeof(a));

  for (int i = 2; i <= sroot; i++) {
    if (a[i] == 0) {
      for (int j = i + i; j <= sroot; j = j + i) {
        a[j] = 1;
      }
    }
  }

  for (int i = 2; i <= sroot; i++) {
    if (a[i] == 0) {
      prime.push_back(i);
    }
  }
}
ll mark[10000];
int main() {

  sieve();
  ll n, sprime, pprime, gc;
  sprime = pprime = 1;
  sf(n);
  ll ar[n + 1];
  for (ll i = 1; i <= n; i++) {
    sf(ar[i]);
    if (i > 1)
      gc = __gcd(gc, ar[i]);
    else
      gc = ar[i];
    ll val = ar[i];
    for (ll j = 0; prime[j] * prime[j] <= val and j < prime.size(); j++) {
      while (val % prime[j] == 0) {
        mark[prime[j]]++;
        val /= prime[j];
      }
    }
    if (val > 1)
      mark[val]++;
  }
  if (gc != 1)
    sprime = 0;
  for (ll i = 1; i <= 1; i++) {
    ll val = ar[i];
    for (ll j = 0; prime[j] * prime[j] <= val and j < prime.size(); j++) {
      while (val % prime[j] == 0) {
        mark[prime[j]]--;
        val /= prime[j];
      }
    }
    if (val > 1)
      mark[val]--;
    val = ar[i];
    for (ll j = 0; prime[j] * prime[j] <= val and j < prime.size(); j++) {
      while (val % prime[j] == 0) {
        if (mark[prime[j]]) {
          pprime = 0;
          break;
        }
        val /= prime[j];
      }
      if (!pprime)
        break;
    }
    if (val > 1)
      if (mark[val])
        pprime = 0;
    if (!pprime)
      break;
  }
  if (pprime)
    printf("pairwise coprime");
  else if (sprime)
    printf("setwise coprime");
  else
    printf("not coprime");
}