PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000282. Số học modulo (Modular Arithmetic)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số học modulo (Modular Arithmetic)

Modular Arithmetic

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Given a modulus nn and qq operations in order, evaluate each operation a  op  ba\;op\;b modulo nn, where opop is one of +, -, *, /. Division means multiplying aa by the multiplicative inverse of bb modulo nn; if such an inverse does not exist, print -1. All valid residues are between 00 and n−1n-1.

Input

  • The first line contains integers nn and qq.
  • Each of the next qq lines contains integers a,ba,b separated by an operator opop.

Output

Print qq lines, each containing the result of the corresponding operation, or -1 if the modular division is undefined.

Subtasks

  • Subtask 1 (20 points): 2≤n≤1002\le n\le100; 1≤q≤201\le q\le20; 0≤a,b<n0\le a,b<n; opop is one of +, -, *, /.
  • Subtask 2 (30 points): 2≤n≤1000002\le n\le100000; 1≤q≤20001\le q\le2000; 0≤a,b<n0\le a,b<n; opop is one of +, -, *, /.
  • Subtask 3 (50 points): 2≤n≤1092\le n\le10^9; 1≤q≤1000001\le q\le100000; 0≤a,b<n0\le a,b<n; opop is one of +, -, *, /.

Examples

Input

7 4
3 + 6
3 - 6
3 * 6
3 / 2

Output

2
4
4
5

Explanation

Addition yields 9 mod 7=29\bmod7=2. Subtraction yields −3 mod 7=4-3\bmod7=4. Multiplication yields 18 mod 7=418\bmod7=4. Since the inverse of 22 modulo 77 is 44, division yields (3⋅4) mod 7=5(3\cdot4)\bmod7=5.