public Polynomial modPow(long e, Polynomial p) throws Exception {
        Polynomial x = this;
        Polynomial r = one();
        while (e > 0) {
            if ((e & 1L) != 0) r = r.mul(x).div(p)[1]; // r *= x mod p
            e >>= 1;
            x = x.mul(x).div(p)[1];  // x = x^2 mod p
        }
        return r;
    }