#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  out << "[";
  for (auto k : v) out << k << " ";
  out << "]"
      << "\n";
  return out;
}
template <class T>
ostream& operator<<(ostream& out, set<T> s) {
  out << "{";
  for (auto k : s) out << k << " ";
  out << "}"
      << "\n";
  return out;
}
template <class T, class U>
ostream& operator<<(ostream& out, map<T, U> m) {
  out << "{";
  for (auto k : m) out << k << " ";
  out << "}"
      << "\n";
  return out;
}
template <class T, class U>
ostream& operator<<(ostream& out, pair<T, U> p) {
  out << "[ " << p.first << " , " << p.second << " ] ";
  return out;
}
template <class T, class U>
istream& operator>>(istream& in, pair<T, U>& p) {
  in >> p.first >> p.second;
  return in;
}
long long D = 1000000000000000;
vector<pair<long long, int> > facts;
map<long long, int> divs;
map<long long, vector<int> > prims;
map<long long, map<long long, long long> > cams;
void Recursiva(int i, long long acum, int cant, vector<int> exps) {
  if (i == facts.size()) {
    divs[acum] = cant;
    prims[acum] = exps;
    return;
  }
  exps.push_back(0);
  Recursiva(i + 1, acum, cant, exps);
  for (int j = int(1); j < int(facts[i].second + 1); j++) {
    acum *= facts[i].first;
    exps[exps.size() - 1] = j;
    Recursiva(i + 1, acum, cant * (j + 1), exps);
  }
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
const long long mod = 998244353;
long long BinPow(long long b, int e) {
  if (e == 0) return 1;
  if (e & 1) return (b * BinPow(b, e - 1)) % mod;
  long long bb = BinPow(b, e / 2);
  return bb * bb % mod;
}
long long fact[2000], inv[2000];
long long Cams(long long a, long long b) {
  if (a > b) swap(a, b);
  if (cams[a][b] != 0) return cams[a][b];
  long long cantf = 1;
  int fac = 0;
  for (int i = 0; i < int(facts.size()); i++) {
    int d = prims[b][i] - prims[a][i];
    fac += d;
    cantf = (cantf * inv[d]) % mod;
  }
  cantf = (cantf * fact[fac]) % mod;
  return cams[a][b] = cantf;
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  cin >> D;
  fact[0] = 1;
  inv[0] = 1;
  for (int i = int(1); i < int(2000); i++)
    fact[i] = (fact[i - 1] * i) % mod, inv[i] = BinPow(fact[i], mod - 2);
  if (D == 1) {
    int q;
    cin >> q;
    for (int i = 0; i < int(q); i++) cout << 1 << "\n";
    return 0;
  }
  for (long long i = 2; i * i <= D; i++) {
    int e = 0;
    while (D % i == 0) D /= i, e++;
    if (e) facts.push_back({i, e});
  }
  if (D > 1) facts.push_back({D, 1});
  Recursiva(0, 1, 1, {});
  int q;
  cin >> q;
  for (int i = 0; i < int(q); i++) {
    long long a, b;
    cin >> a >> b;
    if (a == b) {
      cout << 1 << "\n";
      continue;
    }
    if (a > b) swap(a, b);
    if (b % a == 0) {
      cout << Cams(a, b) << "\n";
      continue;
    }
    long long g = gcd(a, b);
    long long d = divs[a] + divs[b] - 2 * divs[g];
    long long f = Cams(a, g) * Cams(b, g) % mod;
    long long m = (a / g) * b;
    long long d2 = 2 * divs[m] - divs[a] - divs[b];
    if (d2 < d) {
      f = Cams(a, m) * Cams(b, m) % mod;
    } else if (d2 == d) {
      f = (f + Cams(a, m) * Cams(b, m)) % mod;
    }
    cout << f << "\n";
  }
  return 0;
}
