title: How can I handle enormously large numbers in Java?
body: <p>I'm writing a program for my competitive programming class's homework and I can't get over one problem. This problem requires you to calculate all permutations of a certain string based on certain condition and my program does that. However, the problem is when the input is a really large string, it can be up to 10^6.</p>
  
  <p>Solution to the problem is 2^result, where result depends on length of input string, so for 10^6 it can be up to 5*10^5. Solution must be given in form of: result % (10^9 + 7).</p>
  
  <p>I've tried putting solution into a BigInteger, it runs out of heap space. I've tried working with doubles, it overflows. Is there something I'm missing or is there a way to deal with this? Thank you.</p>
  
  <p>Here are some attempts:</p>
  
  <pre><code>System.out.println((int) (Math.pow(2, counter) % (1e9 + 7)));
  //it prints out 0, probably due to overflow?
  
  DecimalFormat df = new DecimalFormat("#");
  System.out.println(df.format(Math.pow(2, counter) % (1e9 + 7)));
  //prints out �
  </code></pre>