#include <bits/stdc++.h>
using namespace std;
const long long md = 998244353;
long long gcd(long long a, long long b) {
  while (a > 0 && b > 0) {
    if (a > b)
      a %= b;
    else
      b %= a;
  }
  return a + b;
}
long long bin(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2 == 0) {
    long long u = bin(a, b / 2);
    return (u * u) % md;
  } else {
    long long u = bin(a, b - 1);
    return (u * a) % md;
  }
}
vector<long long> d;
vector<long long> e;
long long f[1000000 + 5];
long long g[1000000 + 5];
long long solve(long long x, long long y) {
  e.clear();
  long long x0 = x;
  long long y0 = y;
  long long sum = 0;
  for (auto i : d) {
    long long cnt1 = 0;
    long long cnt2 = 0;
    while (x0 % i == 0) {
      cnt1++;
      x0 /= i;
    }
    while (y0 % i == 0) {
      cnt2++;
      y0 /= i;
    }
    if (cnt2 - cnt1 != 0) e.push_back(cnt2 - cnt1);
    sum += cnt2 - cnt1;
  }
  long long ans = f[sum];
  for (auto i : e) {
    ans *= g[i];
    ans %= md;
  }
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      d.push_back(i);
      while (n % i == 0) {
        n /= i;
      }
    }
  }
  if (n > 1) d.push_back(n);
  f[0] = 1;
  for (long long i = 1; i <= 1000; i++) {
    f[i] = f[i - 1] * i;
    f[i] %= md;
  }
  for (long long i = 0; i <= 1000; i++) {
    g[i] = bin(f[i], md - 2);
  }
  long long t;
  cin >> t;
  while (t--) {
    long long x, y;
    cin >> x >> y;
    long long d = gcd(x, y);
    long long ans1 = solve(d, x);
    long long ans2 = solve(d, y);
    long long ans = (ans1 * ans2) % md;
    cout << ans << '\n';
  }
}
