add modular arithmetic early in matrix mult to save comp time