#include <bits/stdc++.h>
using namespace std;
long long p = 998244353;
long long power(long long a, long long b, long long p) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % p;
    a = (a * a) % p;
    b /= 2;
  }
  return res;
}
vector<long long> fac(300, 1), inv(300, 1);
void fill() {
  for (long long i = 1; i < 300; i++) {
    fac[i] = (fac[i - 1] * i) % p;
    inv[i] = power(fac[i], p - 2, p);
  }
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  fill();
  vector<long long> pp;
  long long d;
  cin >> d;
  for (long long i = 2; i <= sqrt(d); i++) {
    if (d % i == 0)
      pp.push_back(i);
    else
      continue;
    while (d % i == 0) d /= i;
  }
  if (d > 1) pp.push_back(d);
  int t = 1;
  cin >> t;
  while (t--) {
    long long u, v;
    cin >> u >> v;
    long long g = gcd(u, v);
    long long ans = 1;
    long long cnt = 0;
    u = u / g;
    for (int i = 0; i < pp.size(); i++) {
      int c2 = 0;
      long long num = pp[i];
      while (u % num == 0) {
        cnt++;
        c2++;
        u /= num;
      }
      ans = (ans * inv[c2]) % p;
    }
    ans = (ans * fac[cnt]) % p;
    cnt = 0;
    u = v / g;
    for (int i = 0; i < pp.size(); i++) {
      int c2 = 0;
      long long num = pp[i];
      while (u % num == 0) {
        cnt++;
        c2++;
        u /= num;
      }
      ans = (ans * inv[c2]) % p;
    }
    ans = (ans * fac[cnt]) % p;
    cout << ans << endl;
  }
  return 0;
}
