Num::Num(int value, int modulo) {
(modulo != 0) ? this->value = value % modulo : this->value = value;
this->modulo = modulo;
}

Num& Num::operator=(const Num& other) = default;

Num Num::operator+(const Num & other) {
Num c((this->value + other.value) % this->modulo, this->modulo);
return c;
}

Num Num::operator-(const Num & other) {
Num c((this->value - other.value) % this->modulo, this->modulo);
return c;
}

Num Num::operator*(const Num & other) {
Num c((this->value * other.value) % this->modulo, this->modulo);
return c;
}

Num Num::operator+(int num) {
this->value += num;
this->value %= this->modulo;
return *this;
}

Num Num::operator-(int num) {
this->value -= num;
this->value %= this->modulo;
return *this;
}

Num Num::operator*(int num) {
this->value *= num;
this->value %= this->modulo;
return *this;
}

Num& Num::operator+=(const Num & other) {
this->value += other.value;
this->modulo = modulo;
this->value %= this->modulo;
return *this;
}

Num& Num::operator-=(const Num & other) {
this->value -= other.value;
this->modulo = modulo;
this->value %= this->modulo;
if (this->value < 0) {
this->value = this->modulo + this->value;
}
return *this;
}

Num& Num::operator*=(const Num & other) {
this->value = ((int64_t)this->value * other.value) % modulo;
this->modulo = modulo;
this->value %= this->modulo;
return *this;
}
Num& Num::operator+=(int num) {
this->value = ((int64_t)this->value + num) % this->modulo;
return *this;
}

Num& Num::operator-=(int num) {
this->value -= num;
this->value %= this->modulo;
if (this->value < 0) {
this->value = this->modulo + this->value;
}
return *this;
}

Num& Num::operator*=(int num) {
this->value = ((int64_t)this->value * num) % this->modulo;
return *this;
}