#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e15;
const long long MOD = 998244353;
struct th {
  vector<int> arr;
  list<pair<int, char>> dist;
};
map<long long, long long> fake;
map<long long, long long> mp;
vector<pair<long long, long long>> dv;
long long gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void rec(int ind, long long prev, long long mn) {
  if (ind >= dv.size()) return;
  for (int i = 0; i < (int)dv[ind].second; ++i) {
    if (i) mp[prev] = mn;
    rec(ind + 1, prev, mn);
    prev *= dv[ind].first;
  }
  mp[prev] = mn;
  rec(ind + 1, prev, mn);
}
void firstRec(int ind) {
  long long cur = dv[ind].first;
  mp[cur] = 1;
  for (int i = 0; i < dv[ind].second; ++i) {
    rec(ind + 1, cur, dv[ind].first);
    cur *= dv[ind].first;
    mp[cur] = dv[ind].first;
  }
}
long long binpow(long long a, long long p) {
  long long ans = 1;
  while (p != 0) {
    if (p % 2 == 1) {
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    p /= 2;
  }
  return ans;
}
long long get(long long u, long long v) {
  if (u < v) swap(u, v);
  if (u % v != 0) {
    auto gc = gcd(u, v);
    long long ans = get(u, gc);
    ans *= get(v, gc);
    ans %= MOD;
    return ans;
  }
  u /= v;
  map<long long, long long> div;
  long long cnt = 0;
  while (u != 1) {
    if (mp[u] == 1) {
      ++div[u];
      u = 1;
    } else {
      ++div[mp[u]];
      u /= mp[u];
    }
    ++cnt;
  }
  long long ans = 1;
  for (long long i = 2; i <= cnt; ++i) {
    ans *= i;
    ans %= MOD;
  }
  for (auto &i : div) {
    long long fact = 1;
    for (long long j = 2; j <= i.second; ++j) {
      fact *= j;
      fact %= MOD;
    }
    ans *= binpow(fact, MOD - 2);
    ans %= MOD;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);
  long long d, q;
  cin >> d >> q;
  long long cur = 2;
  long long temp = d;
  while (cur * cur <= temp) {
    while (temp % cur == 0) {
      temp /= cur;
      if (dv.empty() || dv.back().first != cur)
        dv.emplace_back(cur, 1);
      else
        ++dv.back().second;
    }
    ++cur;
  }
  if (temp != 1) dv.emplace_back(temp, 1);
  mp[1] = 1;
  cur = 1;
  for (int i = 0; i < dv.size(); ++i) {
    firstRec(i);
  }
  while (q--) {
    long long u, v;
    cin >> u >> v;
    cout << get(u, v) << '\n';
  }
  return 0;
}
