#include "num.h"
#include <cstdio>

Num::Num(int value, int modulo) {
    this->value = value % modulo;
    if (this->value < 0) {
        this->value += this->modulo;
    }
    this->modulo = modulo;
}

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

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

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

Num &Num::operator*=(const Num &other) {
    __int64 val = (__int64) this->value * (__int64) other.value;
    val %= this->modulo;
    this->value = static_cast<int>(val);
    return *this;
}

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

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

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

Num Num::operator+(const Num &other) {
    __int64 val = (__int64) this->value + (__int64) other.value;
    val %= this->modulo;
    Num res(static_cast<int>(val), this->modulo);
    return res;
}

Num Num::operator-(const Num &other) {
    __int64 val = (__int64) this->value - (__int64) other.value;
    val %= this->modulo;
    Num res(static_cast<int>(val), this->modulo);
    if (res.value < 0) {
        res.value += res.modulo;
    }
    return res;
}

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

Num Num::operator+(int num) {
    __int64 val = (__int64) this->value + (__int64) num;
    val %= this->modulo;
    Num res(static_cast<int>(val), this->modulo);
    if (res.value < 0) {
        res.value += res.modulo;
    }
    return res;
}

Num Num::operator-(int num) {
    __int64 val = (__int64) this->value - (__int64) num;
    val %= this->modulo;
    Num res(static_cast<int>(val), this->modulo);
    if (res.value < 0) {
        res.value += res.modulo;
    }
    return res;
}

Num Num::operator*(int num) {
    __int64 val = (__int64) this->value * (__int64) num;
    val %= this->modulo;
    Num res(static_cast<int>(val), this->modulo);
    if (res.value < 0) {
        res.value += res.modulo;
    }
    return res;
}