#include "num.h"

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) {
    long long val = (long long) this->value + (long long) other.value;
    val %= this->modulo;
    this->value = (int) val;
    return *this;
}

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

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

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

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

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

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

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

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

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

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

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