You have a sequence 2,5,16,65,........Given an integer n as input. You have to find the value at the nth position in the sequence.
 
Example 1:
Input: n = 4
Output: 65
Example 2:
Input: n = 10
Output: 9864101
 
Your Task:
You don't need to read or print anything, Your task is to complete the function NthTerm() which takes n as input parameter and returns value at nth position of the given sequence modulo 10^{9} + 7.
 
Expected Time Complexity:  O(n)
Expected Space Complexity: O(1)
 
Constraints:
1 <= n <= 10^{4}

Source: geeksforgeeks | Difficulty: EASY

Starter code: #User function Template for python3



class Solution:

	def NthTerm(self, n):

		# Code here

Solutions:
class Solution:

	def NthTerm(self, n):
		i = 1
		j = 2
		ans = 2
		while i < n:
			ans = (ans * j + 1) % (10 ** 9 + 7)
			j += 1
			i += 1
		return ans % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		l = 1
		for i in range(1, n + 1):
			l = l * i + 1
		return l % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		if n == 1:
			return 2
		else:
			first = 2
			last = first
			for i in range(2, n + 1):
				last = first * i + 1
				first = last
			return first % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		s = 2
		if n == 1:
			return 2
		for i in range(2, n + 1):
			k = s
			s = k * i + 1
		return s % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		sum = 1
		for i in range(1, n + 1):
			sum = sum * i + 1
		return sum % 1000000007


import sys
sys.setrecursionlimit(10 ** 4)

class Solution:

	def NthTerm(self, n):
		if n == 1:
			return n * 1 + 1
		else:
			return (self.NthTerm(n - 1) * n + 1) % 1000000007


class Solution:

	def NthTerm(self, n):
		l = [2]
		i = 0
		k = 2
		while i <= n:
			l.append((l[i] * k + 1) % (10 ** 9 + 7))
			k = k + 1
			i = i + 1
		return l[n - 1] % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		d = {}
		d[1] = 2
		for i in range(1, n):
			d[i + 1] = (d[i] * (i + 1) + 1) % (10 ** 9 + 7)
		return d[n] % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		l = [2]
		if n == 1:
			return 2
		for i in range(1, n):
			l.append((l[i - 1] * (i + 1) + 1) % (10 ** 9 + 7))
		return l[-1] % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		i = 2
		p = 2
		k = 1
		while k < n:
			k = k + 1
			p = p * i + 1
			i = i + 1
		return p % 1000000007


class Solution:

	def NthTerm(self, n):
		ans = 2
		for o in range(2, n + 1):
			ans = ans * o + 1
		return ans % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		ans = 1
		mod = 1000000000 + 7
		for i in range(1, n + 1):
			ans = (ans * i + 1) % mod
		return ans


class Solution:

	def NthTerm(self, n):
		(m, ans) = (int(1000000000.0) + 7, 1)
		for i in range(1, n + 1):
			ans = (ans * i + 1) % m
		return ans


class Solution:

	def NthTerm(self, n):
		ans = 1
		mod = 10 ** 9 + 7
		for i in range(1, n + 1):
			ans = ans * i + 1
			ans %= mod
		return ans


class Solution:

	def NthTerm(self, n):
		self.n = n
		if n == 0:
			return self.n
		s = 1
		for i in range(1, self.n + 1):
			s = (s * i + 1) % 1000000007
		return s


class Solution:

	def NthTerm(self, n):
		if n == 1:
			return 2
		else:
			k = n
			a = 2
			n = 2
			while n:
				a = a * n + 1
				if n >= k:
					break
				n += 1
			return a % 1000000007


class Solution:

	def NthTerm(self, n):
		a = 1
		b = 1
		for i in range(1, n + 1):
			c = a * b + 1
			a = c
			b += 1
		return c % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		if n == 1:
			return 2
		else:
			i = 2
			p = 2
			c = 2
			while i <= n:
				c = c * p + 1
				p += 1
				i += 1
			return c % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		if n == 1:
			return 2
		a = 0
		b = 2
		for i in range(1, n):
			a = (b * (i + 1) + 1) % 1000000007
			b = a % 1000000007
		return a


class Solution:

	def NthTerm(self, n):
		op = 0
		for i in range(n + 1):
			op = (op * i + 1) % 1000000007
		return op


class Solution:

	def NthTerm(self, n):
		a = 2
		b = 1
		while b < n:
			b += 1
			a = a * b + 1
		return a % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		d = 2
		c = [2]
		for i in range(2, n + 1):
			d = d * i + 1
		return d % 1000000007


class Solution:

	def NthTerm(self, n):
		i = 2
		s = 1
		while s < n:
			s += 1
			i = i * s + 1
		return i % (10 ** 9 + 7)


class Solution:
	import sys
	sys.setrecursionlimit(10 ** 4)

	def NthTerm(self, n):
		if n == 1:
			return 2
		elif n == 2:
			return 5
		elif n == 3:
			return 16
		elif n == 2:
			return 65
		return (n * self.NthTerm(n - 1) + 1) % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		x = 1
		p = 10 ** 9 + 7
		for i in range(1, n + 1):
			x = (x * i + 1) % p
		return x


class Solution:

	def NthTerm(self, n):
		b = 2
		ans = 2
		l = [2]
		for i in range(1, n):
			ans = ans * b + 1
			b += 1
			ans = ans % (10 ** 9 + 7)
		return ans


class Solution:

	def NthTerm(self, n):
		num = 2
		if n == 1:
			return 2
		for i in range(2, n + 1):
			num = (num * i + 1) % 1000000007
		return num


class Solution:

	def NthTerm(self, n):
		if n == 1:
			return 2
		elif n == 2:
			return 5
		elif n == 3:
			return 16
		elif n == 4:
			return 65
		else:
			mul = 65
			for i in range(5, n + 1):
				mul = mul * i + 1
			return mul % 1000000007


class Solution:

	def NthTerm(self, n):
		a = 2
		j = 2
		m = 10 ** 9 + 7
		for i in range(1, n):
			a = (a * j + 1) % m
			j += 1
		return a


class Solution:

	def NthTerm(self, n):
		a = []
		mod = pow(10, 9) + 7
		for i in range(n):
			if i == 0:
				a.insert(i, 2)
			else:
				val = (a[i - 1] * (i + 1) % mod + 1) % mod
				a.insert(i, val)
		return a[n - 1]


class Solution:

	def NthTerm(self, n):
		ans = 0
		start = 2
		for i in range(2, n + 1):
			ans = start * i + 1
			start = ans
		return ans % (pow(10, 9) + 7) if n > 1 else 2


class Solution:

	def NthTerm(self, n):
		t = 1
		i = 1
		while n:
			t = t * i + 1
			i += 1
			n -= 1
		return t % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		m = 10 ** 9 + 7
		ans = 2
		if n == 1:
			return ans
		for i in range(2, n + 1):
			ans = (ans * i % m + 1) % m
		return ans


import math

class Solution:

	def NthTerm(self, n):
		ans = 1
		for i in range(1, n + 1):
			ans = ans * i + 1
		return ans % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		m = 1
		list = []
		for i in range(1, n + 1):
			m = m * i + 1
		return m % (10 ** 9 + 7)


class Solution:

	def NthTerm(self, n):
		a = 1
		mod = 1000000007
		for i in range(1, n + 1):
			b = a * i % mod + 1
			a = b
		return a


class Solution:

	def NthTerm(self, n):
		a = 2
		i = 2
		while n > 1:
			a = a * i + 1
			i += 1
			n -= 1
		return a % 1000000007


class Solution:

	def NthTerm(self, n):
		no = []
		mod = 1000000007
		for x in range(1, n + 1):
			if x == 1:
				term = x * 1 + 1
			else:
				term = (no[x - 2] * x + 1) % mod
			no.append(term)
		return no[-1]


class Solution:

	def NthTerm(self, n):
		mul = 1
		result = 1
		for i in range(1, n + 1):
			result = result * mul + 1
			mul = mul + 1
		return result % 1000000007


class Solution:

	def NthTerm(self, n):
		start = 2
		for i in range(2, n + 1):
			start = start * i + 1
		return start % 1000000007


class Solution:

	def NthTerm(self, n):
		dp = [0] * (n + 1)
		dp[0] = 2
		dp[1] = 5
		for i in range(2, n + 1):
			dp[i] = ((i + 1) * dp[i - 1] + 1) % 1000000007
		return dp[n - 1]