Modular Exponentiation (Power in Modular Arithmetic) : <blockquote>
  <p>Hello!
  <br> I am getting stuck in understanding the concept of modular Exponentiation. When i need this and how this works.
  <br> Suppose i am calling the power function as : <strong>power(2,n-1)</strong>.
  <br>How the loops will be executed for say <strong>n=10</strong></p>
</blockquote>

<pre><code>#define m 1000000007

unsigned long long int power(unsigned long long int x, unsigned long long int n){

    unsigned long long int res = 1;
    while(n &gt; 0){
        if(n &amp; 1){
            res = res * x;
            res = res % m;
        }
        x = x * x;
        x= x % m;
        n &gt;&gt;= 1;
    }
    return res;

}
</code></pre>
