#include "bits/stdc++.h"
using namespace std;

int mod = 1000000007;
struct Mod {
public:
	int num;
	Mod() : Mod(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) {
	}
	Mod(int n) : Mod(static_cast<long long int>(n)) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int  b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod mod_pow(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = mod_pow((a * a), (n / 2));
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	if (!b.num) {
		throw(false);
	}
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	return Mod(a) / b;
}
Mod operator/=(Mod &a, const Mod b) {
	return a = a /b;
}

#define MAX_MOD_N 1024000

Mod fact[MAX_MOD_N], factinv[MAX_MOD_N];
void init() {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < MAX_MOD_N - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}

//  ????????????????????????
std::string Replace(std::string String1, std::string String2, std::string String3)
{
	std::string::size_type  Pos(String1.find(String2));

	while (Pos != std::string::npos)
	{
		String1.replace(Pos, String2.length(), String3);
		Pos = String1.find(String2, Pos + String3.length());
	}

	return String1;
}
string st;
int a;
Mod getnum() {
	Mod num = 0;
	while (isdigit(st[a])) {
		num = num * 10 + Mod(st[a] - '0');
		a++;
	}
	return num;
}
Mod getexpr();
Mod getkakko() {
	Mod num;
	if (st[a] == '(') {
		a++;
		num = getexpr();
		assert(st[a] == ')');
		a++;
		return num;
	}
	else {
		return getnum();
	}
}
Mod getkake() {
	Mod num;
	num=getkakko();
	while (a != st.size()) {
		if (st[a] == ')') {
			return num;
		}
		else if (st[a] == '+') {
			return num;
		}
		else if (st[a] == '-') {
			return num;
		}
		else if (st[a] == '*') {
			a++;
			Mod nnum(getkakko());
			num *= nnum;
		}
		else {
			assert(st[a] == '/');
			a++;
			Mod nnum(getkakko());
			num /= nnum;
		}
	}
	return num;
}
Mod getexpr() {
	Mod num;
	num = getkake();
	while (a != st.size()) {
		if (st[a] == ')') {
			return num;
		}
		else if (st[a] == '+') {
			a++;
			Mod nnum(getkake());
			num += nnum;
		}
		else if (st[a] == '-') {
			a++;
			Mod nnum(getkake());
			num -= nnum;
		}
		else {
			assert(false);
		}
	}
	return num;
}
void solve(const string ast) {
	st = ast;
	a = 0;
	try {
		Mod ans(getexpr());
		cout << ast << " = " << ans << " (mod " << mod << ")" << endl;
	}
	catch (...) {
		cout << "NG" << endl;
	}
}
int main() {
	string st;
	while (1) {
		getline(cin, st);
		st = Replace(st, " ", "");
		const int start = st.find(":")+1;
		mod = stoi(st.substr(0,start-1));
		if (mod) {
			solve(st.substr(start));
		}
		else {
			break;
		}
	}
	return 0;
}

Measured memory_bytes: 11360