private static long modPowHelper(long a, long p, long N, Map<Long, Long> calculated) {
		// caching
		if (calculated.containsKey(p)) {
			return calculated.get(p);
		}
		if (p == 1) {
			return a % N;
		}

		long first = p / 2;
		long second = p - first;
		long result = modPowHelper(a, first, N, calculated) * modPowHelper(a, second, N, calculated) % N;
		calculated.put(p, result);
		return result;
	}