#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2")
using namespace std;
using lld = long long int;
using pii = pair<int, int>;
using pll = pair<lld, lld>;
lld n;
lld gcd(lld p, lld q) {
  while (p % q) {
    lld t = p % q;
    p = q;
    q = t;
  }
  return q;
}
lld p[100009];
int pl;
lld fact[100009];
lld inv[100009];
lld invf[100009];
lld cal(lld x) {
  lld res = 1;
  int cnt = 0;
  for (int i = 0; i < pl && p[i] <= x; i++) {
    int j = 0;
    while (x % p[i] == 0) {
      x /= p[i];
      j++;
      cnt++;
    }
    res = (res * invf[j]) % 998244353;
  }
  res = (res * fact[cnt]) % 998244353;
  return res;
}
void inil() {
  lld z = n;
  lld i;
  pl = 0;
  for (i = 2; i <= z / i; i++) {
    if (z % i == 0) {
      p[pl++] = i;
      while (z % i == 0) {
        z /= i;
      }
    }
  }
  if (z > 1) {
    p[pl++] = z;
  }
  fact[0] = fact[1] = 1;
  inv[0] = inv[1] = 1;
  invf[0] = invf[1] = 1;
  for (i = 2; i <= 100000; i++) {
    fact[i] = (fact[i - 1] * i) % 998244353;
    inv[i] = (998244353 - (998244353 / i * inv[998244353 % i]) % 998244353) %
             998244353;
    invf[i] = (invf[i - 1] * inv[i]) % 998244353;
  }
}
int main() {
  int i, j, k;
  int t = 1, tv = 0;
  scanf("%lld", &n);
  inil();
  scanf("%d", &t);
  while (t--) {
    lld p, q;
    lld g;
    scanf("%lld %lld", &p, &q);
    g = gcd(p, q);
    if (p == q) {
      printf("1\n");
      continue;
    }
    printf("%lld\n", (cal(p / g) * cal(q / g)) % 998244353);
  }
}
