#include <bits/stdc++.h>
using namespace std;
const int N = (int)5e5 + 5;
long long int power(long long int a, long long int b) {
  if (b == 0) return 1;
  long long int c = power(a, b >> 1);
  c = (c * c) % 998244353;
  if (b & 1) return (c * a) % 998244353;
  return c;
}
long long int f[50];
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
vector<long long int> prime;
long long int calc(long long int x) {
  int cnt = 0;
  unordered_map<long long int, int> pf;
  for (auto it : prime) {
    while (x % it == 0) {
      pf[it]++, cnt++;
      x /= it;
    }
  }
  long long int ret = 1;
  for (auto it : pf) {
    ret = (ret * f[it.second]) % 998244353;
  }
  return (f[cnt] * power(ret, 998244353 - 2)) % 998244353;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  f[0] = 1;
  for (int i = 1; i < 50; i++) {
    f[i] = (f[i - 1] * i) % 998244353;
  }
  long long int d;
  cin >> d;
  for (int i = 2; (long long int)i * i <= d; i++) {
    if (d % i == 0) {
      prime.push_back(i);
      while (d % i == 0) {
        d /= i;
      }
    }
  }
  if (d > 1) prime.push_back(d);
  int q;
  cin >> q;
  while (q--) {
    long long int u, v;
    cin >> u >> v;
    long long int g = gcd(u, v);
    long long int ans = (calc(u / g) * calc(v / g)) % 998244353;
    cout << ans << "\n";
  }
}
