JavaScript solution with Explanation
This is split into 2 parts. First is a code that will work theoretically, but will fail the space allocation because BigInt takes up too much space. Then 2nd pa
This is split into 2 parts. First is a code that will work theoretically, but will fail the space allocation because `BigInt` takes up too much space. Then 2nd part is full solution that is accepted, but it has alot of extra code to deal with `BigInt`.\n\nThis code shows the algorithm (theoretical)\n```javascript\nconst firstDayBeenInAllRooms = function (arr) {\n  let sum = 0n;\n  const prefixes = new Array(arr.length);\n\n  let i = 0;\n  let count = 0;\n  while (i !== arr.length - 1) {\n    const e = arr[i];\n    if (e === i) {\n      sum += 2n;\n    } else {\n      const days = sum - (prefixes[e - 1] || 0n);\n      sum += days + 2n;\n    }\n    prefixes[i] = sum;\n    i += 1;\n  }\n  return Number(sum % 1000000007n);\n};\n```\n\nNote, I use numbers like `0n`, `2n` etc. The `n` turns the number into a `BigInt`.\n\nThe above solution is essentially a prefix sum. In the `prefixes` array, each `prefixes[i]` contains the number of days to get to room `i`.\n\nExample: `[0,0,2,3,3,3,6,7]`\n\nPrefixes array will look like:\n\n```\n[ 2, 6, 8, 10, 14, 22, 24, <empty>]\n\n/* Here\'s how the prefixes are derived. Sum represents total number of days passed.\n\n* prefixes[0]: i is 0, and the value is 0. We are staying in same room, so its just 2. Sum is 2\n* prefixes[1]: i is 1, and we stay in room 1, then stay in room 0 twice, then room 1 again (4). Sum is 6\n* prefixes[2]: i is 2, and the value is 2. We are staying in same room, so its just 2. Sum is 8\n* prefixes[3]: i is 3, and the value is 3. We are staying in same room, so its just 2. Sum is 10\n* prefixes[4]: i is 4, and we stay in room 4, then start journey from room 3 again and then stay in room 4. Sum is 14\n* prefixes[5]: i is 5, and we stay in room 5, then start journey from room 3 again and then stay in room 5. (14 - prefixes[3-1] + 2) gives us 8. Sum is 22\n* prefixes[6]: i is 6, and the value is 6. We are staying in same room, so its just 2. Sum is 24 \n* i is 7, we return the sum (24 days)\n*/\n```\n\n\nThis code handles the mod operations to pass all test cases.\n```javascript\nconst firstDayBeenInAllRooms = function (arr) {\n  let sum = 0;\n\n  let i = 0;\n  let count = 0;\n  while (i !== arr.length - 1) {\n    const e = arr[i];\n    if (e === i) {\n      sum += 2;\n    } else {\n      const days = sum - (arr[e - 1] || 0);\n      sum += days + 2;\n    }\n    arr[i] = sum % 1000000007;\n    sum = sum % 1000000007;\n    i += 1;\n  }\n  sum = sum % 1000000007;\n  while (sum < 0) {\n    sum += 1000000007;\n  }\n  return sum;\n};\n```
['JavaScript']