from sys import stdin, stdout
from sys import maxsize
input = stdin.readline
# def print(n):
#    stdout.write(str(n)+'\n')

def solve():
    pass

test = 1
test = int(input().strip())
for t in range(0, test):
    n = int(input().strip())
    # s = list(input().strip())                                                        # String Input, converted to mutable list.
    # n, k = list(map(int, input().strip().split()))
    a = [int(x) for x in input().strip().split()]
    # brr = [list(map(int,input().strip().split())) for i in range(rows)]              # 2D array row-wise input
    b = 0
    for i in range(n-2, 0, -1):
        if(a[i-1] > a[i] < a[i+1]):
            b = i
            break
    print(b)
    ans = solve()

'''
rows, cols = (5, 5)
arr = [[0]*cols for j in range(rows)]                                         # 2D array initialization
rev_str=s[::-1]                                                               # To reverse given string
b=input().strip().split()                                                     # list created by spliting about spaces
brr = [[int(b[cols*i+j]) for j in range(cols)] for i in range(rows)]          # 2D array Linear Input
rows,cols=len(brr),len(brr[0])                                                # no of rows/cols for 2D array
arr.sort(key = lambda x : x[1])                                               # sort list of tuples by 2nd element, Default priority - 1st Element then 2nd Element
s=set()                                                                       # empty set
a=maxsize                                                                     # initializing infinity
b=-maxsize                                                                    # initializing -infinity
mapped=list(map(function,input))                                              # to apply function to list element-wise
try:                                                                          # Error handling
    #code 1
except:                                                                       # ex. to stop at EOF
    #code 2 , if error occurs
'''


from sys import stdin, stdout
from sys import maxsize
input = stdin.readline
# def print(n):
#    stdout.write(str(n)+'\n')

def solve():
    pass

test = 1
test = int(input().strip())
for t in range(0, test):
    n = int(input().strip())
    # s = list(input().strip())                                                        # String Input, converted to mutable list.
    # n, k = list(map(int, input().strip().split()))
    a = [int(x) for x in input().strip().split()]
    # brr = [list(map(int,input().strip().split())) for i in range(rows)]              # 2D array row-wise input
    b = 0
    for i in range(n-2, 0, -1):
        if(a[i-1] > a[i] <= a[i+1]):
            b = i
            break
    print(b)
    ans = solve()

'''
rows, cols = (5, 5)
arr = [[0]*cols for j in range(rows)]                                         # 2D array initialization
rev_str=s[::-1]                                                               # To reverse given string
b=input().strip().split()                                                     # list created by spliting about spaces
brr = [[int(b[cols*i+j]) for j in range(cols)] for i in range(rows)]          # 2D array Linear Input
rows,cols=len(brr),len(brr[0])                                                # no of rows/cols for 2D array
arr.sort(key = lambda x : x[1])                                               # sort list of tuples by 2nd element, Default priority - 1st Element then 2nd Element
s=set()                                                                       # empty set
a=maxsize                                                                     # initializing infinity
b=-maxsize                                                                    # initializing -infinity
mapped=list(map(function,input))                                              # to apply function to list element-wise
try:                                                                          # Error handling
    #code 1
except:                                                                       # ex. to stop at EOF
    #code 2 , if error occurs
'''


from sys import stdin, stdout
from sys import maxsize
input = stdin.readline
# def print(n):
#    stdout.write(str(n)+'\n')

def solve():
    pass

test = 1
test = int(input().strip())
for t in range(0, test):
    n = int(input().strip())
    # s = list(input().strip())                                                        # String Input, converted to mutable list.
    # n, k = list(map(int, input().strip().split()))
    a = [int(x) for x in input().strip().split()]
    # brr = [list(map(int,input().strip().split())) for i in range(rows)]              # 2D array row-wise input
    i = n-1
    while(i > 0 and a[i-1] >= a[i]):
        i -= 1
    while(i > 0 and a[i-1] <= a[i]):
        i -= 1
    print(i)
    ans = solve()

'''
rows, cols = (5, 5)
arr = [[0]*cols for j in range(rows)]                                         # 2D array initialization
rev_str=s[::-1]                                                               # To reverse given string
b=input().strip().split()                                                     # list created by spliting about spaces
brr = [[int(b[cols*i+j]) for j in range(cols)] for i in range(rows)]          # 2D array Linear Input
rows,cols=len(brr),len(brr[0])                                                # no of rows/cols for 2D array
arr.sort(key = lambda x : x[1])                                               # sort list of tuples by 2nd element, Default priority - 1st Element then 2nd Element
s=set()                                                                       # empty set
a=maxsize                                                                     # initializing infinity
b=-maxsize                                                                    # initializing -infinity
mapped=list(map(function,input))                                              # to apply function to list element-wise
try:                                                                          # Error handling
    #code 1
except:                                                                       # ex. to stop at EOF
    #code 2 , if error occurs
'''


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            if a[i+1] > a[i]:
                k = i
            else:
                c = 0
                while a[i+1] == a[i] and i < len(a) - 2:
                    i += 1
                    c += 1
                if a[i+1] > a[i]:
                    k = i - c
    return k

for t in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            if a[i+1] > a[i]:
                k = i
            else:
                while a[i+1] == a[i] and i < len(a) - 2:
                    i += 1
                if a[i+1] > a[i]:
                    k = i-1
    return k

for t in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    if t == 1471:
        print(a)
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            if a[i+1] > a[i]:
                k = i
            else:
                while a[i+1] == a[i] and i < len(a) - 2:
                    i += 1
                if a[i+1] > a[i]:
                    k = i-1
    return k

for t in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    if t = 1471:
        print(a)
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i] and a[i] < a[i+1]:
            k = i
    return k

for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            if a[i+1] > a[i]:
                k = i
            else:
                while a[i+1] == a[i] and i < len(a) - 2:
                    i += 1
                if a[i+1] > a[i]:
                    k = i
    return k

for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    print(k)


def check(a, k):
    for i in range(k+1, len(a)-1):
        if a[i-1] > a[i] and a[i] < a[i+1]:
            return i
    return k

for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    while check(a, k) != k:
        k = check(a, k)
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            if a[i+1] > a[i]:
                k = i
            else:
                while a[i+1] == a[i]:
                    i += 1
                if a[i+1] > a[i]:
                    k = i
    return k

for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    print(k)


def check(a, k):
    for i in range(k+1, len(a)-1):
        if a[i-1] > a[i] and a[i] < a[i+1]:
            return i
    return k

for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    while check(a, k) != k:
        k = check(a)
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            if a[i+1] > a[i]:
                k = i
            else:
                while a[i+1] == a[i] and i < len(a) - 1:
                    i += 1
                if a[i+1] > a[i]:
                    k = i
    return k

for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    print(k)


def check(a):
    for i in range(1, len(a)-1):
        if a[i-1] > a[i] and a[i] < a[i+1]:
            return i
    return 0

for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = check(a)
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            if a[i+1] > a[i]:
                k = i
            else:
                while a[i+1] == a[i] and i < len(a) - 2:
                    i += 1
                if a[i+1] > a[i]:
                    k = i-1
    return k

for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            if a[i+1] > a[i]:
                k = i
            else:
                while a[i+1] == a[i] and i < len(a) - 2:
                    i += 1
                if a[i+1] > a[i]:
                    k = i-1
    return k

for t in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    if t == 874:
        for aa in a:
            print (aa, end = '.')
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            if a[i+1] > a[i]:
                k = i
            else:
                while a[i+1] == a[i] and i < len(a) - 2:
                    i += 1
                if a[i+1] > a[i]:
                    k = i-1
    return k

for t in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    if t == 1470:
        for aa in a:
            print (aa, end = '.')
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            if a[i+1] > a[i]:
                k = i
            else:
                while a[i+1] == a[i] and i < len(a) - 2:
                    i += 1
                if a[i+1] > a[i]:
                    k = i-1
    return k

for t in range(int(input())):
    input()
    a = list(map(int, input().split()))
    k = 0
    k = check(a)
    if t == 1470:
        print(a)
    print(k)


def check(a):
    k = 0
    for i in range(1, len(a)-1):
        if a[i-1] > a[i]:
            f = i
            while a[i+1] == a[i] and i < len(a) - 2:
                i += 1
            if a[i+1] > a[i]:
                k = f
    return k

for _ in range(int(input())):
    input()
    print(check(list(map(int, input().split()))))


for i in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    flag,flag2=0,0
    for i in range(n-1,0,-1):
        #print(i)
        if a[i]<a[i-1]:
            if flag2==1:
                flag=1
                break

        elif a[i]==a[i-1]:
            pass
        else:
            flag2=1
    if flag:
        print(i)
    else:
        print(0)


t=int(input())
def check(ar):
    a=ar[:]
    ar.sort(reverse=True)
    n=len(a)
    if a==sorted(a):
        return True
    elif ar==a:
        return True
    else:
        if n%2!=0:
            x=(n-1)//2
            b1=a[:x]
            b2=a[x:]
            if b1==sorted(b1) and b2[::-1]==sorted(b2[::-1]):
                return True
        else:
            x=(n-1)//2
            b1=a[:x]
            b2=a[x:]
            if b1==sorted(b1) and b2[::-1]==sorted(b2[::-1]):
                return True
        return False

for _ in range(t):
    n=int(input())
    a=[int(k) for k in input().split()]
    for i in range(n):
        a1=a[i:]
        x=n-i
        if check(a1)==True:
            print(i)
            break


t=int(input())
def check(ar):
    a=ar[:]
    ar.sort(reverse=True)
    n=len(a)
    if a==sorted(a):
        return True
    elif ar==a:
        return True
    else:
        if n%2!=0:
            x=(n-1)//2
            b1=a[:x]
            b2=a[x:]
            b3=a[:x+1]
            b4=a[x+1:]

            if ((b1==sorted(b1) and b2[::-1]==sorted(b2[::-1])) or (b3==sorted(b3) and b4[::-1]==sorted(b4[::-1]))):
                return True
        else:
            x=(n-1)//2
            b1=a[:x]
            b2=a[x:]
            if b1==sorted(b1) and b2[::-1]==sorted(b2[::-1]):
                return True
        return False

for _ in range(t):
    n=int(input())
    a=[int(k) for k in input().split()]
    for i in range(n):
        a1=a[i:]
        x=n-i
        if check(a1)==True:
            print(i)
            break


t=int(input())
def check(ar):
    a=ar[:]
    ar.sort(reverse=True)
    n=len(a)
    if a==sorted(a):
        return True
    elif ar==a:
        return True
    else:
        if n%2!=0:
            x=(n-1)//2
            b1=a[:x]
            b2=a[x:]
            b3=a[:x+1]
            b4=a[x+1:]
            #print(b1,b2,b3,b4)
            #print(b2,sorted(b2[::-1]))
            if ((b1==sorted(b1) and b2[::-1]==sorted(b2[::-1])) or (b3==sorted(b3) and b4[::-1]==sorted(b4[::-1]))):
                return True
        else:
            x=(n-1)//2
            b1=a[:x+1]
            b2=a[x+1:]
            #print(b1,b2)
            if b1==sorted(b1) and b2[::-1]==sorted(b2[::-1]):
                return True
        return False

for _ in range(t):
    n=int(input())
    a=[int(k) for k in input().split()]
    for i in range(n):
        #print(i)
        a1=a[i:]
        #print("a",a1)
        x=n-i
        if check(a1)==True:
            print(i)
            break


t=int(input())
def check(ar):
    a=ar[:]
    ar.sort(reverse=True)
    n=len(a)
    if a==sorted(a):
        return True
    elif ar==a:
        return True
    else:
        if n%2!=0:
            x=(n-1)//2
            b1=a[:x]
            b2=a[x:]
            b3=a[:x+1]
            b4=a[x+1:]

            if (b1==sorted(b1) and b2[::-1]==sorted(b2[::-1]) or (b3==sorted(b3) and b3[::-1]==sorted(b3[::-1]):
                return True
        else:
            x=(n-1)//2
            b1=a[:x]
            b2=a[x:]
            if b1==sorted(b1) and b2[::-1]==sorted(b2[::-1]):
                return True
        return False

for _ in range(t):
    n=int(input())
    a=[int(k) for k in input().split()]
    for i in range(n):
        a1=a[i:]
        x=n-i
        if check(a1)==True:
            print(i)
            break


t=int(input())
def check(ar):
    a=ar[:]
    ar.sort(reverse=True)
    n=len(a)
    if a==sorted(a):
        return True
    elif ar==a:
        return True
    else:
        if n%2!=0:
            x=(n-1)//2
            b1=a[:x]
            b2=a[x:]
            b3=a[:x+1]
            b4=a[x+1:]

            if ((b1==sorted(b1) and b2[::-1]==sorted(b2[::-1]) or (b3==sorted(b3) and b4[::-1]==sorted(b4[::-1]))):
                return True
        else:
            x=(n-1)//2
            b1=a[:x]
            b2=a[x:]
            if b1==sorted(b1) and b2[::-1]==sorted(b2[::-1]):
                return True
        return False

for _ in range(t):
    n=int(input())
    a=[int(k) for k in input().split()]
    for i in range(n):
        a1=a[i:]
        x=n-i
        if check(a1)==True:
            print(i)
            break


t=int(input())
def check(ar):
    a=ar[:]
    ar.sort(reverse=True)
    n=len(a)
    if a==sorted(a):
        return True
    elif ar==a:
        return True
    else:
        if n%2!=0:
            x=(n-1)//2
            b1=a[:x+1]
            b2=a[x:]
            if b1==sorted(b1) and b2[::-1]==sorted(b2[::-1]):
                return True
        else:
            x=(n-1)//2
            b1=a[:x]
            b2=a[x:]
            if b1==sorted(b1) and b2[::-1]==sorted(b2[::-1]):
                return True
        return False

for _ in range(t):
    n=int(input())
    a=[int(k) for k in input().split()]
    for i in range(n):
        a1=a[i:]
        x=n-i
        if check(a1)==True:
            print(i)
            break


def make(n,a):

    decreasing=False
    increasing=False
    equal=False
    last_index=0
    could_be=0
    for i in range(1,n):
        if a[i]>a[i-1]:
            if decreasing==False:
                increasing=True
                equal=False
            else:
                last_index=i-1
                decreasing=False
                increasing=True
                if equal:
                    last_index=could_be
                    equal=False
        elif a[i]<a[i-1]:
            if increasing==True:
                increasing=False
                decreasing=True
                equal=False
            else:
                decreasing=True

                if equal:
                    equal=False
        else:
            if increasing:
                continue
            else:
                could_be=i-1
                equal=True

    return last_index

t=int(input())
array=[]
for i in range(t):
    a=int(input())
    b=list(map(int,input().split(" ")))
    array.append([a,b])
for x in array:
    print(make(*x))


def make(n,a):

    decreasing=False
    increasing=False
    last_index=0
    for i in range(1,n):
        if a[i]>=a[i-1]:
            if decreasing==False:
                increasing=True
            else:
                last_index=i-1
                decreasing=False
                increasing=True
        elif a[i]<a[i-1]:
            if increasing==True:
                increasing=False
                decreasing=True
            else:
                decreasing=True

    return last_index

t=int(input())
array=[]
for i in range(t):
    a=int(input())
    b=list(map(int,input().split(" ")))
    array.append([a,b])
for x in array:
    print(make(*x))


def make(n,a):

    decreasing=False
    increasing=False
    equal=False
    last_index=0
    could_be=0
    for i in range(1,n):
        if a[i]>a[i-1]:
            if decreasing==False:
                increasing=True
            else:
                last_index=i-1
                decreasing=False
                increasing=True
                if equal:
                    last_index=could_be
                    equal=False
        elif a[i]<a[i-1]:
            if increasing==True:
                increasing=False
                decreasing=True
            else:
                decreasing=True
                if equal:
                    equal=False
        else:
            if increasing:
                continue
            else:
                could_be=i-1
                equal=True

    return last_index

t=int(input())
array=[]
for i in range(t):
    a=int(input())
    b=list(map(int,input().split(" ")))
    array.append([a,b])
for x in array:
    print(make(*x))


def make(n,a):

    decreasing=False
    increasing=False
    last_index=0
    for i in range(1,n):
        if a[i]>a[i-1]:
            if decreasing==False:
                increasing=True
            else:
                last_index=i-1
                decreasing=False
                increasing=True
        elif a[i]<a[i-1]:
            if increasing==True:
                increasing=False
                decreasing=True
            else:
                decreasing=True
        else:
            continue
    return last_index

t=int(input())
array=[]
for i in range(t):
    a=int(input())
    b=list(map(int,input().split(" ")))
    array.append([a,b])
for x in array:
    print(make(*x))


def make(n,a):

    decreasing=False
    increasing=False
    equal=False
    last_index=0
    could_be=0
    for i in range(1,n):
        if a[i]>a[i-1]:
            if decreasing==False:
                increasing=True
                equal=False
            else:
                last_index=i-1
                decreasing=False
                increasing=True
                if equal:
                    last_index=could_be
                    equal=False
        elif a[i]<a[i-1]:
            if increasing==True:
                increasing=False
                decreasing=True
                equal=False
            else:
                decreasing=True

                if equal:
                    equal=False
        else:
            if increasing:
                continue
            else:
                if equal:
                    continue
                else:
                    could_be=i-1
                    equal=True

    return last_index

t=int(input())
array=[]
for i in range(t):
    a=int(input())
    b=list(map(int,input().split(" ")))
    array.append([a,b])
for x in array:
    print(make(*x))


# Codeforces round 655

'''test=int(input())
for i in range(test):
    n=int(input())
    arr=[1 for i in range(n)]
    print(*arr)'''

'''test=int(input())
for i in range(test):
    n=int(input())
    i=2
    arr=[]
    while i**2<=n:
        if n%i==0:
            arr.append(i)
            arr.append(n/i)
        i+=1
    if arr:
        k=max(arr)
        print(int(k),int(n-k))
    else:
        print(1,n-1)'''

'''test=int(input())
for i in range(test):
    n=int(input())
    arr=[int(i) for i in input().split()]
    t=arr.copy()
    t.sort()
    j=n
    for i in range(n-1,-1,-1):
        if arr[i]==t[i]:
            j-=1
        else:
            break
    arr=arr[:j]
    if not arr:
        print(0)
    else:
        dp=[0 for j in range(len(arr))]
        for i in range(len(arr)):
            if arr[i]==t[i]:
                dp[i]=1
            else:
                dp[i]=0
        cnt=0
        recent=-1
        for i in range(1,len(arr)):
            if dp[i]==1 and dp[i-1]==0:
                cnt+=1
        print(dp)
        if not cnt:
            print(1)
        else:
            cnt+=1 if dp[-1]==0 else 0
            print(cnt)'''

#codeforces educational round
'''string=input()
patt=input()
from collections import defaultdict
ans=float("inf")
cnt=defaultdict(int)
for i in patt:
    cnt[i]+=1
l=0
cnt_2=defaultdict(int)
for i in range(len(string)):
    if string[i] in patt:
        cnt_2[string[i]]+=1
    print(cnt_2)
    flag=0
    while all(cnt[k]==cnt_2[k] for k in cnt.keys()):
        if string[l] in patt:
            cnt_2[string[l]]-=1
        l+=1
        flag=1
    if flag:
        l-=1
        cnt_2[string[l]]+=1
        flag=0
        print(l)
        ans=min(ans,i-l+1)
        print(i-l+1)

print(ans)'''

'''n,m=[int(i) for i in input().split()]
if m==n:
    s=""
    for i in range(m):
        s+="10"
    print(s)
elif m>n:
    if m>2*(n+1):
        print(-1)
    else:
        s=""
        for i in range(m-n-1):
            s+="110"
        for j in range(2*(n+1)-m):
            s+="10"
        print(s[:m+n])
else:
    if n>m+1:
        print(-1)
    else:
        s=""
        for j in range(m):
            s+="01"
        s+="0"
        print(s)'''

'''n=int(input())
dates=[]
for i in range(n):
    a,b=[int(j) for j in input().split()]
    dates.append([a,b])
dates.sort(key=lambda x: x[0])
ans=dates[0][1]
from collections import defaultdict
cnt=defaultdict(int)
for i in range(n):
    if cnt[dates[i][0]]<dates[i][1]:
        cnt[dates[i][0]]=dates[i][1]

#print(cnt)
for i in range(1,n):
    if cnt[dates[i][0]]>=ans:
        ans=cnt[dates[i][0]]
    else:
        ans=dates[i][0]
print(ans)
print (cnt)'''

'''n=int(input())
arr=[int(i) for i in input().split()]
from collections import defaultdict
cnt=defaultdict(int)
for i in arr:
    cnt[i]+=1
k=max(arr)
dp=[0 for i in range(k+1)]
dp[0]=0
dp[1]=cnt[1]
for i in range(2,k+1):
    dp[i]=max(dp[i-1],dp[i-2] + cnt[i]*i)
    #print(dp[i])
print(dp[-1])'''

#Codeforces round 656 DIV-3
'''test=int(input())
for i in range(test):
    x,y,z=[int(i) for i in input().split()]
    if x==y==z:
        print("YES")
        print(x,y,z)
    else:
        if z==x and y<x:
            print("YES")
            print(y,x,max(1,y-1))
        elif y==z and x<y:
            print("YES")
            print(x,max(1,x-1),y)
        elif x==y and z<y:
            print("YES")
            print(x,z,max(1,z-1))
        else:
            print("NO")
'''

'''def check(num,curr):
    if not num:
        return True
    if len(num)==1:
        if num[0]>=curr:
            return True
        return False
    if num[0]<=num[-1] and num[0]>=curr:
        return True and check(num[1:],min(num[0],num[-1]))
    elif num[0]>=num[-1] and num[-1]>=curr:
        return True and check(num[:-1],min(num[0],num[-1]))
    return False'''
test=int(input())
for i in range(test):
    n=int(input())
    arr=[int(i) for i in input().split()]
    j=n-1
    flag=1
    while j>0:
        if arr[j]>arr[j-1]:
            flag=0
            break
        j-=1
    while j>0:
        if arr[j]<arr[j-1]:
            flag=0
            break
        j-=1
    if not flag:
        ans=j
    else:
        ans=0
    print(ans)

'''def check(s,t):
    k=chr(ord("a")+t)
    if len(s)==1:
        return s!=k
    mid=len(s)//2
    cnt_1=check(s[:mid],t+1)
    cnt_1+=len(s)//2-s[mid:].count(k)
    cnt_2=check(s[mid:],t+1)
    cnt_2+=len(s)//2-s[:mid].count(k)
    return min(cnt_1,cnt_2)

test=int(input())
for i in range(test):
    n=int(input())
    s=str(input())
    print(check(s,1))
    if len(s)==1:
        if s=="a":
            print(0)
        else:
            print(1)
    else:
        l=0
        r=n-1
        t="abcbefghijklmnopqrstuvwxyz"
        k=0
        ans=0
        while l<=r:
            mid=l+(r-l)//2
            cnt_1=0
            for j in s[mid+1:]:
                if j==t[k]:
                    cnt_1+=1
            cnt_2=0
            for j in s[:mid+1]:
                if j==t[k]:
                    cnt_2+=1
            if cnt_1>cnt_2:
                ans+=cnt_1
                l=mid+1
                k+=1
            else:
                ans+=cnt_1
                r=mid-1
                k+=1'''


# Codeforces round 655

'''test=int(input())
for i in range(test):
    n=int(input())
    arr=[1 for i in range(n)]
    print(*arr)'''

'''test=int(input())
for i in range(test):
    n=int(input())
    i=2
    arr=[]
    while i**2<=n:
        if n%i==0:
            arr.append(i)
            arr.append(n/i)
        i+=1
    if arr:
        k=max(arr)
        print(int(k),int(n-k))
    else:
        print(1,n-1)'''

'''test=int(input())
for i in range(test):
    n=int(input())
    arr=[int(i) for i in input().split()]
    t=arr.copy()
    t.sort()
    j=n
    for i in range(n-1,-1,-1):
        if arr[i]==t[i]:
            j-=1
        else:
            break
    arr=arr[:j]
    if not arr:
        print(0)
    else:
        dp=[0 for j in range(len(arr))]
        for i in range(len(arr)):
            if arr[i]==t[i]:
                dp[i]=1
            else:
                dp[i]=0
        cnt=0
        recent=-1
        for i in range(1,len(arr)):
            if dp[i]==1 and dp[i-1]==0:
                cnt+=1
        print(dp)
        if not cnt:
            print(1)
        else:
            cnt+=1 if dp[-1]==0 else 0
            print(cnt)'''

#codeforces educational round
'''string=input()
patt=input()
from collections import defaultdict
ans=float("inf")
cnt=defaultdict(int)
for i in patt:
    cnt[i]+=1
l=0
cnt_2=defaultdict(int)
for i in range(len(string)):
    if string[i] in patt:
        cnt_2[string[i]]+=1
    print(cnt_2)
    flag=0
    while all(cnt[k]==cnt_2[k] for k in cnt.keys()):
        if string[l] in patt:
            cnt_2[string[l]]-=1
        l+=1
        flag=1
    if flag:
        l-=1
        cnt_2[string[l]]+=1
        flag=0
        print(l)
        ans=min(ans,i-l+1)
        print(i-l+1)

print(ans)'''

'''n,m=[int(i) for i in input().split()]
if m==n:
    s=""
    for i in range(m):
        s+="10"
    print(s)
elif m>n:
    if m>2*(n+1):
        print(-1)
    else:
        s=""
        for i in range(m-n-1):
            s+="110"
        for j in range(2*(n+1)-m):
            s+="10"
        print(s[:m+n])
else:
    if n>m+1:
        print(-1)
    else:
        s=""
        for j in range(m):
            s+="01"
        s+="0"
        print(s)'''

'''n=int(input())
dates=[]
for i in range(n):
    a,b=[int(j) for j in input().split()]
    dates.append([a,b])
dates.sort(key=lambda x: x[0])
ans=dates[0][1]
from collections import defaultdict
cnt=defaultdict(int)
for i in range(n):
    if cnt[dates[i][0]]<dates[i][1]:
        cnt[dates[i][0]]=dates[i][1]

#print(cnt)
for i in range(1,n):
    if cnt[dates[i][0]]>=ans:
        ans=cnt[dates[i][0]]
    else:
        ans=dates[i][0]
print(ans)
print (cnt)'''

'''n=int(input())
arr=[int(i) for i in input().split()]
from collections import defaultdict
cnt=defaultdict(int)
for i in arr:
    cnt[i]+=1
k=max(arr)
dp=[0 for i in range(k+1)]
dp[0]=0
dp[1]=cnt[1]
for i in range(2,k+1):
    dp[i]=max(dp[i-1],dp[i-2] + cnt[i]*i)
    #print(dp[i])
print(dp[-1])'''

#Codeforces round 656 DIV-3
'''test=int(input())
for i in range(test):
    x,y,z=[int(i) for i in input().split()]
    if x==y==z:
        print("YES")
        print(x,y,z)
    else:
        if z==x and y<x:
            print("YES")
            print(y,x,max(1,y-1))
        elif y==z and x<y:
            print("YES")
            print(x,max(1,x-1),y)
        elif x==y and z<y:
            print("YES")
            print(x,z,max(1,z-1))
        else:
            print("NO")
'''
'''test=int(input())
for i in range( test):
    n=int(input())
    arr=[int(i) for i in input().split()]
    visited=[0 for i in range(n+1)]
    ans=[]
    for i in range(len(arr)):
        if visited[arr[i]]==0:
            ans.append(arr[i])
            visited[arr[i]]=1
    print(*ans)'''

test=int(input())
def check(num,curr):
    if len(num)==1:
        if num[0]>=curr:
            return True
        return False
    if num[0]<=num[-1] and num[0]>=curr:
        return True and check(num[1:],num[0])
    elif num[0]>=num[-1] and num[-1]>=curr:
        return True and check(num[:-1],num[-1])
    return False

for i in range(test):
    n=int(input())
    arr=[int(i) for i in input().split()]

    l=0
    r=n-1
    ans=n-1
    while l<=r:
        mid=l+(r-l)//2
        if check(arr[mid:],0):
            ans=mid
            r=mid-1
        else:
            l=mid+1
    print(ans)


# Codeforces round 655

'''test=int(input())
for i in range(test):
    n=int(input())
    arr=[1 for i in range(n)]
    print(*arr)'''

'''test=int(input())
for i in range(test):
    n=int(input())
    i=2
    arr=[]
    while i**2<=n:
        if n%i==0:
            arr.append(i)
            arr.append(n/i)
        i+=1
    if arr:
        k=max(arr)
        print(int(k),int(n-k))
    else:
        print(1,n-1)'''

'''test=int(input())
for i in range(test):
    n=int(input())
    arr=[int(i) for i in input().split()]
    t=arr.copy()
    t.sort()
    j=n
    for i in range(n-1,-1,-1):
        if arr[i]==t[i]:
            j-=1
        else:
            break
    arr=arr[:j]
    if not arr:
        print(0)
    else:
        dp=[0 for j in range(len(arr))]
        for i in range(len(arr)):
            if arr[i]==t[i]:
                dp[i]=1
            else:
                dp[i]=0
        cnt=0
        recent=-1
        for i in range(1,len(arr)):
            if dp[i]==1 and dp[i-1]==0:
                cnt+=1
        print(dp)
        if not cnt:
            print(1)
        else:
            cnt+=1 if dp[-1]==0 else 0
            print(cnt)'''

#codeforces educational round
'''string=input()
patt=input()
from collections import defaultdict
ans=float("inf")
cnt=defaultdict(int)
for i in patt:
    cnt[i]+=1
l=0
cnt_2=defaultdict(int)
for i in range(len(string)):
    if string[i] in patt:
        cnt_2[string[i]]+=1
    print(cnt_2)
    flag=0
    while all(cnt[k]==cnt_2[k] for k in cnt.keys()):
        if string[l] in patt:
            cnt_2[string[l]]-=1
        l+=1
        flag=1
    if flag:
        l-=1
        cnt_2[string[l]]+=1
        flag=0
        print(l)
        ans=min(ans,i-l+1)
        print(i-l+1)

print(ans)'''

'''n,m=[int(i) for i in input().split()]
if m==n:
    s=""
    for i in range(m):
        s+="10"
    print(s)
elif m>n:
    if m>2*(n+1):
        print(-1)
    else:
        s=""
        for i in range(m-n-1):
            s+="110"
        for j in range(2*(n+1)-m):
            s+="10"
        print(s[:m+n])
else:
    if n>m+1:
        print(-1)
    else:
        s=""
        for j in range(m):
            s+="01"
        s+="0"
        print(s)'''

'''n=int(input())
dates=[]
for i in range(n):
    a,b=[int(j) for j in input().split()]
    dates.append([a,b])
dates.sort(key=lambda x: x[0])
ans=dates[0][1]
from collections import defaultdict
cnt=defaultdict(int)
for i in range(n):
    if cnt[dates[i][0]]<dates[i][1]:
        cnt[dates[i][0]]=dates[i][1]

#print(cnt)
for i in range(1,n):
    if cnt[dates[i][0]]>=ans:
        ans=cnt[dates[i][0]]
    else:
        ans=dates[i][0]
print(ans)
print (cnt)'''

'''n=int(input())
arr=[int(i) for i in input().split()]
from collections import defaultdict
cnt=defaultdict(int)
for i in arr:
    cnt[i]+=1
k=max(arr)
dp=[0 for i in range(k+1)]
dp[0]=0
dp[1]=cnt[1]
for i in range(2,k+1):
    dp[i]=max(dp[i-1],dp[i-2] + cnt[i]*i)
    #print(dp[i])
print(dp[-1])'''

#Codeforces round 656 DIV-3
'''test=int(input())
for i in range(test):
    x,y,z=[int(i) for i in input().split()]
    if x==y==z:
        print("YES")
        print(x,y,z)
    else:
        if z==x and y<x:
            print("YES")
            print(y,x,max(1,y-1))
        elif y==z and x<y:
            print("YES")
            print(x,max(1,x-1),y)
        elif x==y and z<y:
            print("YES")
            print(x,z,max(1,z-1))
        else:
            print("NO")
'''

'''def check(num,curr):
    if not num:
        return True
    if len(num)==1:
        if num[0]>=curr:
            return True
        return False
    if num[0]<=num[-1] and num[0]>=curr:
        return True and check(num[1:],min(num[0],num[-1]))
    elif num[0]>=num[-1] and num[-1]>=curr:
        return True and check(num[:-1],min(num[0],num[-1]))
    return False'''
'''for i in range(test):
    n=int(input())
    arr=[int(i) for i in input().split()]
    j=n-1
    flag=1
    while j>0:
        if arr[j]>arr[j-1]:
            flag=0
            break
        j-=1
    while j>0:
        if arr[j]<arr[j-1]:
            flag=0
            break
        j-=1
    if not flag:
        ans=j
    else:
        ans=0
    print(ans)'''

def check(s,t):
    k=chr(ord("a")+t)
    if len(s)==1:
        return s!=k
    mid=len(s)//2
    cnt_1=check(s[:mid],t+1)
    cnt_1+=len(s)//2-s[mid:].count(k)
    cnt_2=check(s[mid:],t+1)
    cnt_2+=len(s)//2-s[:mid].count(k)
    return min(cnt_1,cnt_2)

test=int(input())
for i in range(test):
    n=int(input())
    s=str(input())
    print(check(s,1))
    '''if len(s)==1:
        if s=="a":
            print(0)
        else:
            print(1)
    else:
        l=0
        r=n-1
        t="abcbefghijklmnopqrstuvwxyz"
        k=0
        ans=0
        while l<=r:
            mid=l+(r-l)//2
            cnt_1=0
            for j in s[mid+1:]:
                if j==t[k]:
                    cnt_1+=1
            cnt_2=0
            for j in s[:mid+1]:
                if j==t[k]:
                    cnt_2+=1
            if cnt_1>cnt_2:
                ans+=cnt_1
                l=mid+1
                k+=1
            else:
                ans+=cnt_1
                r=mid-1
                k+=1'''


# Codeforces round 655

'''test=int(input())
for i in range(test):
    n=int(input())
    arr=[1 for i in range(n)]
    print(*arr)'''

'''test=int(input())
for i in range(test):
    n=int(input())
    i=2
    arr=[]
    while i**2<=n:
        if n%i==0:
            arr.append(i)
            arr.append(n/i)
        i+=1
    if arr:
        k=max(arr)
        print(int(k),int(n-k))
    else:
        print(1,n-1)'''

'''test=int(input())
for i in range(test):
    n=int(input())
    arr=[int(i) for i in input().split()]
    t=arr.copy()
    t.sort()
    j=n
    for i in range(n-1,-1,-1):
        if arr[i]==t[i]:
            j-=1
        else:
            break
    arr=arr[:j]
    if not arr:
        print(0)
    else:
        dp=[0 for j in range(len(arr))]
        for i in range(len(arr)):
            if arr[i]==t[i]:
                dp[i]=1
            else:
                dp[i]=0
        cnt=0
        recent=-1
        for i in range(1,len(arr)):
            if dp[i]==1 and dp[i-1]==0:
                cnt+=1
        print(dp)
        if not cnt:
            print(1)
        else:
            cnt+=1 if dp[-1]==0 else 0
            print(cnt)'''

#codeforces educational round
'''string=input()
patt=input()
from collections import defaultdict
ans=float("inf")
cnt=defaultdict(int)
for i in patt:
    cnt[i]+=1
l=0
cnt_2=defaultdict(int)
for i in range(len(string)):
    if string[i] in patt:
        cnt_2[string[i]]+=1
    print(cnt_2)
    flag=0
    while all(cnt[k]==cnt_2[k] for k in cnt.keys()):
        if string[l] in patt:
            cnt_2[string[l]]-=1
        l+=1
        flag=1
    if flag:
        l-=1
        cnt_2[string[l]]+=1
        flag=0
        print(l)
        ans=min(ans,i-l+1)
        print(i-l+1)

print(ans)'''

'''n,m=[int(i) for i in input().split()]
if m==n:
    s=""
    for i in range(m):
        s+="10"
    print(s)
elif m>n:
    if m>2*(n+1):
        print(-1)
    else:
        s=""
        for i in range(m-n-1):
            s+="110"
        for j in range(2*(n+1)-m):
            s+="10"
        print(s[:m+n])
else:
    if n>m+1:
        print(-1)
    else:
        s=""
        for j in range(m):
            s+="01"
        s+="0"
        print(s)'''

'''n=int(input())
dates=[]
for i in range(n):
    a,b=[int(j) for j in input().split()]
    dates.append([a,b])
dates.sort(key=lambda x: x[0])
ans=dates[0][1]
from collections import defaultdict
cnt=defaultdict(int)
for i in range(n):
    if cnt[dates[i][0]]<dates[i][1]:
        cnt[dates[i][0]]=dates[i][1]

#print(cnt)
for i in range(1,n):
    if cnt[dates[i][0]]>=ans:
        ans=cnt[dates[i][0]]
    else:
        ans=dates[i][0]
print(ans)
print (cnt)'''

'''n=int(input())
arr=[int(i) for i in input().split()]
from collections import defaultdict
cnt=defaultdict(int)
for i in arr:
    cnt[i]+=1
k=max(arr)
dp=[0 for i in range(k+1)]
dp[0]=0
dp[1]=cnt[1]
for i in range(2,k+1):
    dp[i]=max(dp[i-1],dp[i-2] + cnt[i]*i)
    #print(dp[i])
print(dp[-1])'''

#Codeforces round 656 DIV-3
'''test=int(input())
for i in range(test):
    x,y,z=[int(i) for i in input().split()]
    if x==y==z:
        print("YES")
        print(x,y,z)
    else:
        if z==x and y<x:
            print("YES")
            print(y,x,max(1,y-1))
        elif y==z and x<y:
            print("YES")
            print(x,max(1,x-1),y)
        elif x==y and z<y:
            print("YES")
            print(x,z,max(1,z-1))
        else:
            print("NO")
'''
'''test=int(input())
for i in range( test):
    n=int(input())
    arr=[int(i) for i in input().split()]
    visited=[0 for i in range(n+1)]
    ans=[]
    for i in range(len(arr)):
        if visited[arr[i]]==0:
            ans.append(arr[i])
            visited[arr[i]]=1
    print(*ans)'''

test=int(input())
def check(num,curr):
    if not num:
        return True
    if len(num)==1:
        if num[0]>=curr:
            return True
        return False
    if num[0]<=num[-1] and num[0]>=curr:
        return True and check(num[1:],min(num[0],num[-1]))
    elif num[0]>=num[-1] and num[-1]>=curr:
        return True and check(num[:-1],min(num[0],num[-1]))
    return False

for i in range(test):
    n=int(input())
    arr=[int(i) for i in input().split()]
    l=0
    r=n-1
    ans=n-1
    while l<=r:
        mid=l+(r-l)//2
        if check(arr[mid:],0):
            ans=mid
            r=mid-1
        else:
            l=mid+1
    print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	pos = n-1
	while pos>1 and arr[pos-1]>=arr[pos]:
		pos-=1
	while pos>1 and arr[pos-1]<=arr[pos]:
		pos-=1
	print(pos)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	pos = n-1
	while pos>1 and arr[pos-1]>=arr[pos]:
		pos-=1
	while pos>1 and arr[pos-1]<=arr[pos]:
		pos-=1
	print(pos-1)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	pos = n-1
	while pos>0 and arr[pos-1]>=arr[pos]:
		pos-=1
	while pos>0 and arr[pos-1]<=arr[pos]:
		pos-=1
	print(pos)


def check(arr):
	index = -1
	for j in range(len(arr)-1):
		if arr[j]> arr[j+1]:
			index = j
			break

	if index != -1:
		for k in range(index, len(arr)-1):
			if arr[k] < arr[k+1]:
				return False
	return True

def solve(n,a):
	i = 0
	while i < n:
		if check(a[i:]):
			print(i)
			return
		i+=1
t = int(input())
for i in range(t):
	n = int(input())
	a= list(map(int, input().split()))
	solve(n,a)


def solve(n,a):
	pos = n-1
	index = n-1
	while pos>0:
		if a[pos] > a[pos-1]:
			index = pos
			break
		pos-=1

	while pos>0:
		if a[pos] < a[pos-1]:
			return pos
		pos -=1
	return 0
t = int(input())
for i in range(t):
	n = int(input())
	a= list(map(int, input().split()))
	print(solve(n,a))


def check(arr):
	for j in range(len(arr)):
		if arr[j]>= arr[j+1]:
			index = j
	for k in range(j, len(arr)):
		if arr[j] < arr[j+1]:
			return False
	return True
def solve(n,a):
	i = 0
	while i <n:
		if check(a[i:]):
			print(i)
t = int(input())
for i in range(t):
	n = int(input())
	a= list(map(int, input().split()))
	solve(n,a)


def solve(n,a):
	pos = n-1
	index = n-1
	while pos>0:
		if a[pos] > a[pos-1]:
			index = pos
			break
		pos-=1

	while pos>0:
		if a[pos] < a[pos-1]:
			return pos
		pos -=1
t = int(input())
for i in range(t):
	n = int(input())
	a= list(map(int, input().split()))
	print(solve(n,a))


t = int(input())
l = []
for i in range(t):
    n = int(input())
    s = list(map(int, input().split()))
    c = s.copy()
    item = min(s[0], s[n-1])
    count = 0
    t=1
    j = n
    while j != 1:
        if s[0] >= item and s[0] <=s [j-1]:
            item = s[0]
            s = s[1:j]
            count = count+1
            j = j-1
        elif s[j-1] >= item and s[j-1] <= s[0]:
            item = s[j-1]
            s = s[0:j-1]
            j = j-1
            count=count+1
        else:
            s = c[t:n]
            item = min(s[0], s[n-t-1])
            j = n-t
            count = 0
            t=t+1
    l.append(n-count-1)
for i in l:
    print(i)


l=[]
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    a=a[::-1]
    c=1
    count=1
    for i in range(1,n):
        if c==1:
            if a[i-1]<=a[i]:
                count=count+1
            else:
                c=2
        if c==2:
            if a[i-1]>a[i]:
                count=count+1
            else:
                break
    l.append(n-count)
for i in l:
    print(i)


t = int(input())
l = []
for i in range(t):
    n = int(input())
    s = list(map(int, input().split()))
    c = s.copy()
    item = min(s[0], s[n-1])
    count = 0
    t=1
    j = n
    while j != 1:
        if s[0] >= item and s[0] <=s [j-1]:
            item = s[0]
            s = s[1:j]
            count = count+1
            j = j-1
        elif s[j-1] >= item and s[j-1] <= s[0]:
            item = s[j-1]
            s = s[0:j-1]
            j = j-1
            count=count+1
        else:
            s = c[t:n]
            item = min(s[0], s[n-t-1])
            j = n-t
            count = 0
            t=t+1
        print(count)
    print("space")
    l.append(n-count-1)
for i in l:
    print(i)


l=[]
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    a=a[::-1]
    for i in range(n-1):
        if a[i]>a[i+1]:
            a=a[i:]
            break
    count=0
    for i in range(len(a)-1):
        if a[i]>=a[i+1]:
            count=count+1
        else:
            break
    l.append(n-(count+n-len(a)+1))

for i in l:
    print(i)


t = int(input())
arr = []
for i in range(t):
    n = int(input())
    s = list(map(int, input().split()))
    it=min(s[0],s[-1])
    j=n-1
    k=0
    count=0
    while (j-k)>=1:
        if s[k]<=s[j] and s[k]>=it:
            it=s[k]
            k=k+1
            count=count+1
        elif s[k]>=s[j] and s[j]>=it:
            it=s[j]
            j=j-1
            count=count+1
        else:
            count=0
            if s[k-1]==s[j]:
                k=k
            else:
                k=k+1
            j=n-1
            it=min(s[j],s[k])
    arr.append(n-count-1)
for i in arr:
    print(i)


l=[]
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    a=a[::-1]
    flag1=0
    flag2=0
    for i in range(1,n):
        if a[i]>a[i-1]:
            flag1=1
            break
    for i in range(1,n):
        if a[i]<a[i-1]:
            flag2=1
            break
    if flag1==0 or flag2==0:
        l.append(0)
    else:
        item=a[0]
        index=0
        for i in range(n):
            if item<=a[i]:
                item=a[i]
            else:
                index=i
                item=a[i]
                break
        a=a[index:]
        t=index
        index=len(a)
        for i in range(len(a)):
            if item>=a[i]:
                item=a[i]
            else:
                index=i
                break
        l.append(n-t-index)
for  i in l:
    print(i)


l=[]
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    a.append(0)
    n=n+1
    a=a[::-1]
    c=1
    count=1
    for i in range(1,n):
        if c==1:
            if a[i-1]<=a[i]:
                count=count+1
            else:
                c=2
        if c==2:
            if a[i-1]>a[i]:
                count=count+1
            else:
                break
    l.append(n-count-1)
for i in l:
    print(i)


l=[]
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    a=a[::-1]
    for i in range(n-1):
        if a[i]>a[i+1]:
            a=a[i:]
            break
    if len(a)==n:
        l.append(0)
    else:
        count=n-len(a)+1
        print(a)
        for i in range(len(a)-1):
            if a[i]>=a[i+1]:
                count=count+1
            else:
                break
        l.append(n-count)

for i in l:
    print(i)


l=[]
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    a.append(0)
    n=n+1
    a=a[::-1]
    c=1
    count=1
    for i in range(1,n):
        if c==1:
            if a[i-1]<=a[i]:
                count=count+1
            else:
                c=2
        if c==2:
            if a[i-1]>a[i]:
                count=count+1
            else:
                break
    l.append(n-count)
for i in l:
    print(i)


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))

    a=0
    b=n-1
    i=[]
    c=[]

    while a!=b:
        if l[a]==min(l[a],l[b]):
            i.append(a)
            c.append(l[a])
            a+=1
        else:
            i.append(b)
            c.append(l[b])
            b-=1

    i.append(a)
    c.append(l[a])

    ans=0
    for j in range(1,n):
        if c[j]<c[j-1]:
            ans=i[j]
    print(ans)


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))

    if n<=2:
        print(0)
    else:
        diffs=[]
        for i in range(1,n):
            if l[i]>l[i-1]:
                diffs.append(1)
            elif l[i]==l[i-1]:
                diffs.append(0)
            else:
                diffs.append(-1)

        if 1 not in set(diffs) or -1 not in set(diffs):
            print(0)
        else:
            ans=0
            if diffs[-1]==1:
                i=len(diffs)-1
                while diffs[i]!=-1:
                    i-=1
                ans=i+1
            else:
                i=len(diffs)-1
                while diffs[i]!=1:
                    i-=1
                while diffs[i]!=-1 and diffs[i]!=0 and i>=0:
                    i-=1

                ans=i+1
            print(ans)


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))

    if n<=2:
        print(0)
    else:
        diffs=[]
        for i in range(1,n):
            if l[i]>l[i-1]:
                diffs.append(1)
            elif l[i]==l[i-1]:
                diffs.append(0)
            else:
                diffs.append(-1)
        i=len(diffs)-1
        while diffs[i]!=1 and i>=0:
            i-=1
        while diffs[i]!=-1 and i>=0:
            i-=1
        print(i+1)


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))

    a=0
    b=n-1
    c=min(l[a],l[b])
    ans=0

    while a!=b:
        if c<l[a] and c<l[b]:
            c=min(l[a],l[b])
        if l[a]<l[b]:
            if l[a]<c:
                ans = a
            a+=1
        elif l[b]<=l[a]:
            if l[b]<c:
                ans = b
            b-=1
    print(ans)


import bisect

t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))

    s=sorted(l)
    a=0
    b=n-1
    ans=0
    for i in s:
        if i==l[a]:
            a+=1
        elif i==l[b]:
            b-=1
        else:
            while l[a]!=i:
                s.pop(bisect.bisect_left(s,l[a]))
                a+=1
                ans=a
            a+=1
    print(ans)


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))

    if n<=2:
        print(0)
    else:
        diffs=[]
        for i in range(1,n):
            if l[i]>l[i-1]:
                diffs.append(1)
            elif l[i]==l[i-1]:
                diffs.append(0)
            else:
                diffs.append(-1)

        if len(set(diffs))==1:
            print(0)
        else:
            ans=0
            if diffs[-1]==1:
                i=len(diffs)-1
                while diffs[i]!=-1:
                    i-=1
                ans=i+1
            else:
                i=len(diffs)-1
                while diffs[i]==-1:
                    i-=1
                while diffs[i]!=-1 and i>=0:
                    i-=1

                ans=i+1
            print(ans)


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))

    if n<=2:
        print(0)
    else:
        diffs=[]
        for i in range(1,n):
            if l[i]>l[i-1]:
                diffs.append(1)
            elif l[i]==l[i-1]:
                diffs.append(0)
            else:
                diffs.append(-1)

        if 1 not in set(diffs) or -1 not in set(diffs):
            print(0)
        else:
            ans=0
            if diffs[-1]==1:
                i=len(diffs)-1
                while diffs[i]!=-1:
                    i-=1
                ans=i+1
            else:
                i=len(diffs)-1
                while diffs[i]==-1:
                    i-=1
                while diffs[i]!=-1 and i>=0:
                    i-=1

                ans=i+1
            print(ans)


t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))

    if n<=2:
        print(0)
    else:
        diffs=[]
        for i in range(1,n):
            if l[i]>l[i-1]:
                diffs.append(1)
            elif l[i]==l[i-1]:
                diffs.append(0)
            else:
                diffs.append(-1)

        if len(set(diffs))==1:
            print(0)
        else:
            ans=0
            if diffs[-1]==1:
                i=len(diffs)-1
                while diffs[i]!=-1:
                    i-=1
                ans=i+1
            else:
                i=len(diffs)-1
                while diffs[i]==-1:
                    i-=1
                while diffs[i]!=-1:
                    i-=1

                ans=i+1
            print(ans)


test_number = int(input())
for test in range(test_number):
    array_length = int(input())
    if array_length == 1:
        print(0)
    else:
        str_array = input()
        array=[]
        for count in range(array_length):
            array.append(int(str_array[2 * count]))
        begin = array[-1]
        decreasing = 0
        mark = -1
        for count in range(array_length-2,-1,-1):
            if array[count] > begin:
                if decreasing == 1:
                    mark = count
                    break
                else:
                    begin = array[count]
            elif array[count] < begin:
                decreasing = 1
                begin = array[count]
            else:
                begin = array[count]
        print(mark+1)


#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <complex>
#include <algorithm>
#include <cstring>
#include <cassert>
using namespace std;;
#define end(c) (c).end()
#define bgn(c) (c).begin()
#define all(c) bgn(c),end(c)
#define sz(c) int((c).size())
#define bak(c) c.back()
#define rep(i,n) for(int i=0, _##i=(n); i<_##i; ++i)
#define dwn(i,n) for(int i=(n); i-->0; )
#define repr(i,l,r) for(int i=(l), _##i=(r); i<_##i; ++i)
#define dwnr(i,r,l) for(int i=(r), _##i=(l); i-->_##i; )
#define repi(i,a) for(auto i:a)
#define dwni(i,a) for(auto i=(a).rbegin(), _##i=(a).rend(); i!=_##i; ++i)
#define smin(a,b) a=min(a,b)
#define smax(a,b) a=max(a,b)
#define inf INT_MAX
#define eps 1e-9
#define pi 3.1415926535897932384626
#define vec vector
#define fila(a,b,v) fill(a,a+asz(a,b),v)
#define fil(a,v) fill(all(a),v)
#define pb push_back
#define popb pop_back
#define popf pop_front
#define mp make_pair
#define zro(c) memset(c,0,sizeof(c))
#define neg(c) memset(c,-1,sizeof(c))
#define ord(a,b) do{if(a>b)swap(a,b);}while(0)
#define at(c,i) ((c>>i)&1)
#define dbg(a...) do{cerr<<#a<<" = ";_d,a;cerr<<endl;}while(0)
#define dbga(a, n)do{rep(_i,n)cout<<a[_i]<<' ';cout<<endl;}while(0)
#define big long long
#define dub double
#define pp pair
#define pii pp<int, int>
#define pbb pp<big, big>
#define pdd pp<dub, dub>
#define vi vec<int>
#define vb vec<big>
#define vpi vec<pii>
#define str string
#define bigd long double
#define ff first
#define ss second
#define pt complex<int>
#define ptb complex<big>
#define xx real
#define yy imag
#define mine(c...) min_element(c)
#define maxe(c...) max_element(c)
#define bin binary_search
#define lb lower_bound
#define ub upper_bound
#define msi map<string, int>
#define mii map<int, int>
#define cnt __builtin_popcount
#define gcd __gcd
#define pq priority_queue
#define pl(x) cout<<(x)<<'\n'
#define fin(x) do{x;return;}while(0)
#define sqr(x) ((x)*(x))
#define in(l,x,r) ((l)<(x)&&(x)<=(r))
struct _dbg{
	template<class T> _dbg& operator,(const T& v){
		cerr<<v<<" ";
		return *this;};};
_dbg _d;
void go();
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);go();};

const big D = 1e9+7;
big r=1, x=2, n;

void go(){
	cin>>n, --n;
	if(n==-1) fin(pl(1));
	for(; n; n /= 2, x = x*x%D) {
		if(n&1) r = r*x%D;};
	pl(r*(2*r+1)%D);};


// Solution by Mukai Yersin
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <set>
#include <map>

#define sz 2000000
#define mp make_pair
#define pb push_back
#define eps 1e-5
#define inf 1<<30
#define INF (long long)(1ll<<62)
#define rep(i, l, r) for (int i = l; i < r; i++)
#define irep(i, r, l) for (int i = r - 1; i >= l; i--)
#define F first
#define S second
#define M 1000000007

typedef long long ll;

using namespace std;

ll k, n;

ll power(ll a, ll n) {
	ll ret = 1ll;
 	while (n) {
		if (n % 2 == 1) ret = ret * a % M;
		a = a * a % M;
		n /= 2;
 	}
 	return ret;
}

int main()
{
	scanf("%I64d", &n);
	k = power(2, n);
	k = (k % M * ((k + 1ll) % M) ) % M * power(2, M - 2) % M;
	printf("%I64d", k);
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>

using namespace std;

#define task "a"

#define fi first
#define se second
#define mp make_pair

typedef long long ll;

const ll MOD = 1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;

struct Matrix {
    ll a[2][2];
    Matrix () {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                a[i][j] = 0;
            }
        }
    }
    Matrix operator * (Matrix b) {
        Matrix c;
        for (int k = 0; k < 2; k++)
            for (int i = 0; i < 2; i++)
                for (int j = 0; j < 2; j++) {
                    c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % MOD;
                }
        return c;
    }
};

Matrix binPow(Matrix a, ll q) {
    Matrix res;
    res.a[0][0] = 1;
    res.a[1][1] = 1;
    while (q > 0) {
        if (q & 1) {
            res = res * a;
        }
        a = a * a;
        q /= 2;
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
    //freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
    Matrix mask;
    mask.a[0][0] = 1;
    ll n;
    cin >> n;
    Matrix a;
    a.a[0][0] = 3;
    a.a[1][0] = 1;
    a.a[0][1] = 1;
    a.a[1][1] = 3;
    cout << (mask * binPow(a, n)).a[0][0] << endl;
    return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

#define m_p make_pair
#define p_b push_back
#define ull unsigned long long
#define ll long long
#define mod 1000000007

using namespace std;

ll power(ll a,ll b){
    if(b==0)return 1;
    if((int)b%2==0){
        ll lol=power(a,b/2)%mod;
        return (lol*lol)%mod;
    }
    else return (a*power(a,b-1))%mod;

}

int main(){

ios_base::sync_with_stdio(0);

ll n;

cin>>n;

if(n==0)
{
    cout<<1;return 0;
}

cout<<((power(2,2*n-1)+power(2,n-1)))%mod<<endl;

return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

#define m_p make_pair
#define p_b push_back
#define ull unsigned long long
#define ll long long
#define mod 1000000007

using namespace std;

ll n,i,j;

int main(){

ios_base::sync_with_stdio(0);

cin>>n;

cout<<(pow(4,n)+pow(2,n))/2<<endl;

return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

#define m_p make_pair
#define p_b push_back
#define ull unsigned long long
#define ll long long
#define mod 1000000007

using namespace std;

ll power(ll a,ll b){
    if(b==0)return 1;
    if((int)b%2==0){
        ll lol=power(a,b/2)%mod;
        return (lol*lol)%mod;
    }
    else return (a*power(a,b-1))%mod;

}

int main(){

ios_base::sync_with_stdio(0);

ll n;

cin>>n;

if(n==0)
{
    cout<<1;return 0;
}

cout<<(power(2,2*n-1)+power(2,n-1))<<endl;

return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int N = 20;
const long long MD = 1000000007;

long long A[N][N], tmp[N][N];
int n;

void MatMul (long long A[][N], long long B[][N], long long ans[][N]) {
	memset (tmp, 0, sizeof (tmp));
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) tmp[i][j] = (tmp[i][j] + A[i][k] * B[k][j]) % MD;
		memcpy(ans, tmp, sizeof (tmp));
}

void MatPow (long long M[][N], long long x, long long ans[][N]) {
	int i, j, k, p[0] = {0}, cnt = 0;
	while (x != 0) { p[++cnt] = x % 2; x /= 2;}
	for (i = 1; i <= n; i++)
		for (j = 1;j <= n; j++) ans[i][j] = 0;
	for (i = 1; i <= n; i++) ans[i][i] = 1;

	for (int i = cnt; i >= 1; i--) {
		MatMul(ans, ans, ans);
		if (p[i]) MatMul(ans, M, ans);
	}
}
int main () {
	long long ans[N][N];
	long long j;
	n = 2;
	A[1][1] = A[2][2] = 3;
	A[1][2] = A[2][1] = 1;

	cin >> j;
	MatPow (A, j, ans);
	cout << ans[1][1] << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int N = 20;
const long long MD = 1000000007;

long long A[N][N], tmp[N][N];
int n;

void MatMul (long long A[][N], long long B[][N], long long ans[][N]) {
	memset (tmp, 0, sizeof (tmp));
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) tmp[i][j] = (tmp[i][j] + A[i][k] * B[k][j]) % MD;
		memcpy(ans, tmp, sizeof (tmp));
}

void MatPow (long long M[][N], long long x, long long ans[][N]) {
	int i, j, k, p[100] = {0}, cnt = 0;
	while (x != 0) { p[++cnt] = x % 2; x /= 2;}
	for (i = 1; i <= n; i++)
		for (j = 1;j <= n; j++) ans[i][j] = 0;
	for (i = 1; i <= n; i++) ans[i][i] = 1;

	for (int i = cnt; i >= 1; i--) {
		MatMul(ans, ans, ans);
		if (p[i]) MatMul(ans, M, ans);
	}
}
int main () {
	long long ans[N][N];
	long long j;
	n = 2;
	A[1][1] = A[2][2] = 3;
	A[1][2] = A[2][1] = 1;

	cin >> j;
	MatPow (A, j, ans);
	cout << ans[1][1] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MOD = 1000000007;
ll n;

ll fast_pow(ll a, ll b) {
	if (b == 0) return 1;
	if (b & 1) return (a * fast_pow(a, b - 1)) % MOD;
	ll temp = fast_pow(a, b / 2);
	return (temp * temp) % MOD;
}

int main() {
	cin >> n;
	if (n == 0) {
		cout << 1 << endl;
	} else {
		ll level = fast_pow(2, n - 1);
		cout << (level * (2 * level + 1)) % MOD << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MOD = 1000000007;
ll n;

ll fast_pow(ll a, ll b) {
	if (b == 0) return 1;
	if (b & 1) return (a * fast_pow(a, b - 1)) % MOD;
	ll temp = fast_pow(a, b / 2);
	return (temp * temp) % MOD;
}

int main() {
	cin >> n;
	ll level = fast_pow(2, n - 1);
	cout << (level * (2 * level + 1)) % MOD << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <cmath>
#include <memory.h>
#include <algorithm>
#include <cstdlib>
#include <cassert>
#include <sstream>
#include <unordered_map>
using namespace std;
void solve();

#define pb push_back
#define mp make_pair

typedef long long li;
typedef vector<li> vi;
typedef pair<int, int> pi;

int main() {
#ifdef DEBUG
    freopen("input", "r", stdin);
    //freopen("output","w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    int t = 1;
    //cin>>t;
    while (t--)
        solve();
    return 0;
}
const li mod = 1000000007;
struct matrix{
    li a[2][2];
    matrix(){
        a[0][0] = 1;
        a[0][1] = 0;
        a[1][0] = 0;
        a[1][1] = 1;
    }

    matrix operator * (const matrix& x){
        matrix t;
        for(int i=0;i<2;++i){
            for(int j=0;j<2;++j){
                t.a[i][j] = 0;
                for(int k =0;k<2;++k){
                    t.a[i][j] += (a[i][k] * x.a[k][j]) % mod;
                    t.a[i][j] %= mod;
                }
            }
        }
        return t;
    }
};

matrix pow(matrix a, li b){
    if(!b)
        return matrix();
    if(b%2)
        return a * pow(a, b-1);

    return pow(a*a, b/2);
}
void solve(){
    li n;
    cin>>n;

    matrix m;
    m.a[0][0] = m.a[1][1] = 3;
    m.a[0][1] = m.a[1][0] = 1;

    matrix p = pow(m ,n);
    cout<<p.a[0][0];
}


#include <iostream>
#include <cstdlib>
#include <math.h>
#include <cstdio>
#include <queue>
#include <map>
#include <string.h>
#include <vector>
#include <algorithm>

#define REP(i, n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i, n) for(int i=(n)-1; i>=0; i--)
#define FOR(i, a, b) for(int i=(a), _b=(b); i<=_b; i++)
#define FORD(i, a, b) for(int i=(a), _b=(b); i>=_b; i--)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define x1 x213123
#define y1 y123123
#define x2 x123
#define y2 y24124

using namespace std;

typedef pair<int,int> pii;
typedef long long LL;

const int MAXN = 100009;
const double eps = 1e-9;
const LL mod = 1000000007;

LL addmod(LL a, LL b)
{
  return ((a % mod) + (b % mod)) % mod;
}

LL mulmod(LL a, LL b)
{
  return (a % mod) * (b % mod) % mod;
}

LL fastpow(LL a, LL b)
{
  LL res = 1;
  while (b)
  {
    if (b & 1)
      res = mulmod(res, a);
    a = mulmod(a, a);
    b >>= 1;
  }
  return res;
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  LL n;
  cin >> n;
  LL res = mulmod(fastpow(2, n), addmod(fastpow(2, n), 1));
  res = mulmod(res, fastpow(2, mod - 2));
  cout << res << endl;
  return 0;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;

long long bigmod(long long a)
{
    long long tmp;
    if (a==0)
        return 1;
    else if (a%2==0)
    {
        tmp=bigmod(a/2);
        return (tmp*tmp)%1000000007;
    }
    else
    {
        tmp=bigmod(a/2);
        return (2*tmp*tmp)%1000000007;
    }
}
int main()
{
    long long n;
    scanf("%I64d",&n);
    if (n==0)
        printf("1\n");
    else
    {
        long long tmp;
        tmp=bigmod(n-1);
        printf("%I64d\n",(tmp*(((tmp*2)%1000000007)+1))%1000000007);
    }
}


#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <set>
#include <iomanip>
#include <cmath>
#include <sstream>
using namespace std;

ifstream in("input.txt");
ofstream out("output.txt");

#ifdef DEBUG
#define cin in
#define cout out
#endif

typedef long long ll;
const ll mod = 1e9 + 7;

long long bp(long long a,long long b){
    if(b<0) return 0;
    if(b==0) return 1;
    if(b==1) return a;
    long long res = bp(a,b/2);
    if(b%2==0) return (res*res)%mod;
    else return (((res*res)%mod)*a)%mod;

}

int main(){
    string a,b;
    ios::sync_with_stdio(0);
    long long n;
    cin >> n;
    cout << (((bp(2,n)*(bp(2,n)+1))%mod)*bp(2,mod-2))%mod;
    return 0;

}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<deque>
#include<string>
#include<string.h>
#include<vector>
#include<stack>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
#include<time.h>
#include<bitset>
#include<list>
using namespace std;
typedef long long ll;
ll kism(ll m)
{
    ll a=1;
    for (ll b=2;m;m/=2,b=b*b%1000000007) if (m&1) a=a*b%1000000007;
    return a;
}
int main(){
    ll n;
    cin>>n;
    if (n==0) cout<<"1"<<endl;
    else cout<<(kism(n-1)+kism(n+n-1))%1000000007;
    return 0;
}


#include<stdio.h>
#define MOD 1000000007
typedef unsigned long long int ULL;
int main()
{
    ULL n,d,b;
    scanf("%I64d",&n);
    d = 1;b=2;
    while(n>0)
    {
        if(n%2==1) d=(d*b)%MOD;
        n=n>>1;
        b=(b*b)%MOD;
    }
    d=((d*(d+1))%MOD)/2;
    printf("%I64d",d);
    return 0;
}


#include<stdio.h>
#define MOD 1000000007
typedef unsigned long long int ULL;
int main()
{
    ULL n,d,b;
    scanf("%I64u",&n);
    d = 1;b=2;
    while(n>0)
    {
        if(n%2==1) d=(d*b)%MOD;
        n=n>>1;
        b=(b*b)%MOD;
    }
    d=((d*(d+1))/2)%MOD;
    printf("%I64u",d);
    return 0;
}


#include<stdio.h>
#define Mod(a) (a%1000000007)
unsigned long long int pow(unsigned long long int n)
{
    if(n==0)
    return 1;
    else
    {
        unsigned long long int a;
        if(n%2==0)
        {a=Mod(pow(n/2));
         return Mod(a*a);
        }
        else
        {a=Mod(pow(n/2));
         return Mod(2*a*a);
        }
    }
}
int main()
{
    unsigned long long int n,a;
    scanf("%I64u",&n);
    if(n==0)
    printf("1");
    else{
    a=pow(n-1);
    n=Mod(a*2+1);
    a=Mod(a*n);
    printf("%I64u",a);}
    return 0;
}


#include<stdio.h>
#include<math.h>
#define max 1000000007
__int64 fun(__int64 n)
{
    int i,s=1;
    for(i=1;i<=n;i++)
    {
        s=((s%max)*2)%max;
    }
    return s;
}
int main()
{
    __int64 n;
    scanf("%I64d",&n);
    if(n==0)printf("1\n");
    else
    printf("%I64d\n",(((1<<(2*n-1))%max+(1<<(n-1))%max))%max);
    return 0;
}


#include<stdio.h>
#include<math.h>
#define max 1000000007
__int64 fun(__int64 n)
{
    int i,s=1;
    for(i=1;i<=n;i++)
    {
        s=((s%max)*2)%max;
    }
    return s;
}
int main()
{
    __int64 n;
    scanf("%I64d",&n);
    if(n==0)printf("1\n");
    else
    printf("%I64d\n",(fun(2*n-1)+fun(n-1))%max);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 1000000007
__int64 quickpow(__int64 m,__int64 n,__int64 k)
{
    __int64 b=1;
    while(n>0)
    {
        if(n&1)
            b=(b+m)%k;
        n=n>>1;
        m=(m*m)%k;
    }
    return b;
}
int main()
{
    __int64 n;
    scanf("%I64d",&n);
    if(n==0)printf("1\n");
    else if(n==1)printf("3\n");
    else
    printf("%I64d\n",(quickpow(2,2*n-1,MAX)+quickpow(2,n-1,MAX))%MAX);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 1000000007
__int64 quickpow(__int64 m,__int64 n,__int64 k)
{
    __int64 b=1;
    while(n>0)
    {
        if(n&1)
            b=(b+m)%k;
        n=n>>1;
        m=(m*m)%k;
    }
    return b;
}
int main()
{
    __int64 n;
    scanf("%I64d",&n);
    printf("%I64d\n",(quickpow(2,2*n-1,MAX)+quickpow(2,n-1,MAX))%MAX);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 1000000007
__int64 quickpow(__int64 m,__int64 n,__int64 k)
{
    __int64 b=1;
    while(n>0)
    {
        if(n&1)
            b=(b*m)%k;
        n=n>>1;
        m=(m*m)%k;
    }
    return b;
}
int main()
{
    __int64 n;
    scanf("%I64d",&n);
    if(n==0)printf("1\n");
    else if(n==1)printf("3\n");
    else
    printf("%I64d\n",(quickpow(2,2*n-1,MAX)+quickpow(2,n-1,MAX))%MAX);
    return 0;
}


//Created by Pratik Paras Mandlecha
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long long int p=1000000007;
long long int power(long long int x,long long int y)
{
	long long int res=1;
	x=x%p;
	while(y>0)
	{
		if(y&1)
			res=(res*x)%p;
		y=y>>1;
		x = (x*x) % p;
	}
	return res;
}
int main()
{
	long long int n;
	scanf("%lld",&n);
	if(n==0) printf("1");
	else printf("%lld",(((power(2,n-1)%1000000007)+(2*power(4,n-1)%1000000007))%1000000007));

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define MOD 1000000007

int main(void){
    long long int n, i, sum, j, value, ans;
    scanf("%I64d", &n);
    value=1;
    for(i=0;i<n;i++)
    {
        value=(value*2)%MOD;
    }
    sum=((value*(value+1))/2);
    ans=sum%MOD;
    printf("%I64d", ans);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define MOD 1000000007

typedef long long int ll;

ll power(ll n){
    if(n==0){
        return 1;
    }
    else if(n%2){
        return (2*power(n/2)*power(n/2))%MOD;
    }
    else{
        return (power(n/2)*power(n/2))%MOD;
    }
}

int main(void){
    ll n, i, sum, j, value, ans;
    scanf("%I64d", &n);
    value=power(n);
    sum=((value*(value+1))/2);
    ans=sum%MOD;
    printf("%I64d", ans);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(void){
    long long int n, i, sum, j, value;
    scanf("%I64d", &n);
    value=1;
    for(i=0;i<n;i++)
    {
        value*=2;
    }
    sum=0;
    sum=((value*(value+1))/2);
    printf("%I64d", sum%1000000007);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(void){
    long long int n, i, sum, j, value;
    scanf("%l64d", &n);
    value=1;
    for(i=0;i<n;i++)
    {
        value*=2;
    }
    sum=0;
    for(j=1;j<=value;j++)
    {
        sum+=j;
    }
    printf("%l64d", sum%1000000007);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(void){
    long long int n, i, sum, j, value, ans;
    scanf("%I64d", &n);
    value=1;
    for(i=0;i<n;i++)
    {
        value*=2;
    }
    sum=0;
    sum=((value*(value+1))/2);
    ans=sum%1000000007;
    printf("%I64d", ans);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(void)  {
    int n, i, sum, j, value;
    scanf("%d", &n);
    value=1;
    for(i=0;i<n;i++)
    {
        value*=2;
    }
    sum=0;
    for(j=1;j<=value;j++)
    {
        sum+=j;
    }
    printf("%d", sum%1000000007);
    return 0;
}


#include <stdio.h>

typedef long long ll;

const ll mod = 1000000007;

struct matrix {
  ll m[2][2];
};

struct matrix multiply(struct matrix x, struct matrix y) {
  int i, j, k;
  struct matrix g;
  for (i = 0; i < 2; i ++) {
    for (j = 0; j < 2; j ++) {
      g.m[i][j] = 0;
      for (k = 0; k < 2; k ++) {
        g.m[i][j] = (g.m[i][j] + (x.m[i][k] * y.m[k][j]) % mod) % mod;
      }
    }
  }
  return g;
}

void func(ll n) {
  int i, j;
  struct matrix a, t;
  if (n == 0) {
    printf("1\n");
    return;
  }
  t.m[0][0] = t.m[1][1] = a.m[0][0] = a.m[1][1] = 3;
  t.m[0][1] = t.m[1][0] = a.m[0][1] = a.m[1][0] = 1;
  n --;
  while (n) {
    if (n & 1) {
      t = multiply(a, t);
    }
    a = multiply(a, a);
    n >>= 1;
  }
  printf("%I64d\n", t.m[0][0]);
}

int main() {
  ll n;
  while (scanf("%I64d", &n) == 1) {
    func(n);
  }
  return 0;
}


# include<stdio.h>
# include<conio.h>
long long int power(long long int n)
{
long long int a,b=1000000007;
  if(n==0)
  return(1);
  else if(n==1)
  return(2);
  else if(n%2==0)
  {
    a=power(n/2);
    return((a*a)%b);
  }
  else
  {
    a=power(n/2);
    return((2*a*a)%b);
  }

}
main()
{
   long long int i,j,a=1000000007;
   long long int n;
   scanf("%I64d",&n);
   if(n==0)
   printf("1");
   else
   {
   i=power(2*n-1)%a;
   j=power(n-1)%a;
   i=(i+j)%a;
   printf("%I64d",i);
   }
   return(0);
}


# include<stdio.h>
# include<conio.h>
long long int power(long long int n)
{
long long int a,b=1000000007;
  if(n==0)
  return(1);
  else if(n==1)
  return(2);
  else if(n%2==0)
  {
    a=power(n/2);
    return((a*a)%b);
  }
  else
  {
    a=power(n/2);
    return((2*a*a)%b);
  }

}
main()
{
   long long int i,j,k,a=1000000007;
   long long int n;
   scanf("%I64d",&n);
   i=power(2*n)%a;
   j=power(2*n-1)%a;
   k=power(n-1)%a;
   i=(i-j+k)%a;
   printf("%I64d",i);
   return(0);
}


#include<stdio.h>
long long int  bo(int n)
{static long long int a=1,b=0;
long long int  c=a;
if(n==0)return a;
a=(a*3+b)%1000000007;b=(b*3+c)%1000000007;
	return (bo(n-1));
}
int main()
{ int n;
scanf("%d",&n);
printf("%I64d",bo(n));
return 0;
 }


#include<stdio.h>
#include <iostream>
using namespace std;
const long long maxn=1e9+7;
long long bo(long long n)
{static long long a=1,b=0;
long long c=a%maxn;
if(n==0)return a;
a=3*a%maxn+b;b=3*b%maxn+c;
	return (bo(n-1));
}
int main()
{long long n;
//scanf("%I64d",&n);
//printf("%I64d",bo(n)%maxn);
 cin>>n;
  cout<<bo(n)%maxn<<endl;
return 0;
 }


#include<stdio.h>
#include <iostream>
using namespace std;
long long int  bo(long long int n)
{static long long int a=1,b=0;
long long int  c=a;
if(n==0)return a;
a=(a*3+b)%1000000007;b=(b*3+c)%1000000007;

	return (bo(n-1));
}
int main()
{long long int n;
scanf("%d",&n);
printf("%I64d",bo(n));
return 0;
 }


#include <iostream>
#include <cstdio>
using namespace std;
const long long mod=1e9+7;
long long power(long long p){
    long long  ans=1,temp=2;
    while(p){
        if(p&1) ans=ans*temp%mod;
        temp=temp*temp%mod;
        p>>=1;
    }
    return ans;
}
int main()
{
    long long  n;
    while(cin>>n){
         if(n==0){
            puts("1");
            continue;
         }
         printf("%I64d\n",(power(2*n-1)+power(n-1))%mod);
    }
    return 0;
}


#include<stdio.h>
#include <iostream>
using namespace std;
long long int  bo(long long int n)
{static long long int a=1,b=0;
long long int  c=a;
if(n==0)return a;
a=(a*3+b)%1000000007;b=(b*3+c)%1000000007;
	return (bo(n-1));
}
int main()
{long long int n;
scanf("%d",&n);
cout<<bo(n);
return 0;
 }


#include<stdio.h>
#include <iostream>
using namespace std;
const long long maxn=1e9+7;
long long bo(int n)
{static long long a=1,b=0;
long long c=a%maxn;
if(n==0)return a;
a=3*a%maxn+b;b=3*b%maxn+c;
	return (bo(n-1));
}
int main()
{int n;
//scanf("%I64d",&n);
//printf("%I64d",bo(n)%maxn);
 cin>>n;
  cout<<bo(n)%maxn<<endl;
return 0;
 }


#include<stdio.h>
long long bo(int n)
{static __int64 a=1,b=0;
__int64 c=a;
if(n==0)return a;
a=a*3+b;b=b*3+c;
	return (bo(n-1));
}
int main()
{int n;
scanf("%d",&n);
printf("%I64d",bo(n));
return 0;
 }


#include<stdio.h>
#define ll long long
ll ans,n,ans1;
ll qpow(ll b)
{
  ll r=1,base=2;
  while(b!=0)
  {
    if(b&1)r=(r*base)%1000000007;
    base=(base*base)%1000000007;
    b>>=1;
  }
  return r;
}
int main()
{
  scanf("%I64d",&n);
  if(n==0)printf("1\n");
  else
  {
    ans=qpow(n-1);
    ans1=qpow(2*n-1);
    printf("%I64d\n",(ans+ans1)%1000000007);
  }
  return 0;
}


#include<stdio.h>
long long int  bo(long long int n)
{static long long int a=1,b=0;
long long int  c=a;
if(n==0)return a;
a=a*3+b;b=b*3+c;
	return (bo(n-1));
}
int main()
{long long int n;
scanf("%d",&n);
printf("%I64d",bo(n));
return 0;
 }


#include<stdio.h>
int bo(int n)
{static int a=1,b=0;
int c=a;
if(n==0)return a;
a=a*3+b;b=b*3+c;
	return (bo(n-1));
}
int main()
{int n;
scanf("%d",&n);
printf("%d",bo(n));
return 0;
 }


#include<stdio.h>
int bo(int n)
{static int a=1,b=0;
int c=a;
if(n==0)return a;
a=a*3+b;b=c;
	return (bo(n-1));
}
int main()
{int a=1,b=0,c=1,n;
scanf("%d",&n);
printf("%d",bo(n));
return 0;
 }


#include<stdio.h>
#include <iostream>
using namespace std;
const long long maxn=1e9+7;
long long bo(long long n)
{static long long a=1,b=0;
long long c=a%maxn;
if(n==0)return a;
a=3*a%maxn+b;b=3*b%maxn+c;
	return (bo(n-1));
}
int main()
{long long n,a=1,b=0,c;
//scanf("%I64d",&n);
//printf("%I64d",bo(n)%maxn);
 cin>>n;
while(n--)
 {c=a%maxn;
 a=3*a%maxn+b;b=3*b%maxn+c;
 }
  cout<<a%maxn<<endl;
return 0;
}


#include<stdio.h>
long long int  bo(long long int n)
{static long long int a=1,b=0;
long long int  c=a;
if(n==0)return a;
a=(a*3+b)%1000000007;b=(b*3+c)%1000000007;
	return (bo(n-1));
}
int main()
{long long int n;
scanf("%d",&n);
printf("%I64d",bo(n));
return 0;
 }


#include<stdio.h>
#include<stdlib.h>
#define modd 1000000007
long long fast_expo(long long a,long long b)
{
	long long result=1;
	while(b>0)
	{
		if(b&1)
			result=(result*a)%modd;
		a=(a*a);
		b=b>>1;
	}
	return result;
}
int main()
{
	long long n,ans;
	while(scanf("%I64d",&n)==1)
	{
		if(n==0) printf("1\n");
		else
			printf("%I64d\n",(fast_expo(2,n-1)*(fast_expo(2,n)+1))%modd);
	}
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#define modd 1000000007
long long fast_expo(long long a,long long b)
{
	long long result=1;
	while(b>0)
	{
		if(b&1)
			result=(result*a)%modd;
		a=(a*a)%modd;
		b=b>>1;
	}
	return result;
}
int main()
{
	long long n,ans;
	while(scanf("%I64d",&n)==1)
	{
		if(n==0) printf("1\n");
		else
			printf("%I64d\n",(fast_expo(2,n-1)*((fast_expo(2,n)+1)%modd))%modd);
	}
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#define modd 1000000007
long long fast_expo(long long a,long long b)
{
	long long result=1;
	while(b>0)
	{
		if(b&1)
			result=(result*a)%modd;
		a=(a*a);
		b=b>>1;
	}
	return result;
}
int main()
{
	long long n,ans;
	while(scanf("%I64d",&n)==1)
	{
		if(n==0) printf("1\n");
		else
			printf("%I64d\n",(fast_expo(2,n-1)*((fast_expo(2,n)+1)%modd))%modd);
	}
	return 0;
}


# include <stdio.h>
# include <string.h>

# define MOD ((long long)1000000007)

long long pot( long long base, long long pt ){
 long long r;
 if( pt <= 0 ) return 1;
 if( pt == 1 ) return base;
 r = pot( base, pt / 2);
 r = ( r * r) % MOD;
 if( pt % 2 == 1) r = ( r * base);
 return r % MOD;
}

main(){
  long long n, res, A, B;
  scanf("%I64d", &n);
  res = ( pot( 2, n - 1) * ( pot( 2 , n ) + 1 ) ) % MOD;
  if( n == 0 ) res = 1;
  printf("%I64d\n", res % MOD);
  return 0;
}


# include <stdio.h>
# include <string.h>

# define MOD ((long long)1000000007)

main(){
  long long n, res, A, B;
  scanf("%I64d", &n);
  n = 2 * n;
  A = n;
  B = ( n + 1);
  if( A % 2 == 0 ) A /= 2;
  else B /= 2;
  res = ( ( A % MOD ) *( B % MOD) ) % MOD;
  printf("%I64d\n", res % MOD);
  return 0;
}


# include <stdio.h>
# include <string.h>

# define MOD ((long long)1000000007)

long long pot( long long base, int pt ){
 long long r;
 if( pt <= 0 ) return 1;
 if( pt == 1 ) return base;
 r = pot( base, pt / 2);
 r = ( r * r) % MOD;
 if( pt % 2 == 1) r = ( r * base);
 return r % MOD;
}

main(){
  long long n, res, A, B;
  scanf("%I64d", &n);
  n = n;
  res = ( pot( 2, n - 1) * ( pot( 2 , n ) + 1 ) ) % MOD;
  if( n == 0 ) res = 1;
  printf("%I64d\n", res % MOD);
  return 0;
}


#include<stdio.h>
#define m 1000000007

 long long int power(long long int x,long long int n)
    {  long long int p;
       if(n==1)
       return(x%m);
       else
       {p=power(x,n/2)%m;
         if(n%2==0)
         return((p*p)%m);
         else
         return((x*p*p)%m);
         }
         }
int main()
{
  long long int n,p,c;
  scanf("%I64d",&n);
  if(n==0)
  printf("1");
  else
  {p=power(2,n)%m;
  c=(p*(p+1)/2)%m;

  printf("%I64d",c);}
  return 0;
  }


#include<stdio.h>
#define m 1000000007

 long long int power(long long int x,long long int n)
    {  long long int p;
       if(n==1)
       return(x%m);
       else
       {p=power(x,n/2)%m;
         if(n%2==0)
         return((p*p)%m);
         else
         return((x*p*p)%m);
         }
         }
int main()
{
  long long int n,p,c;
  scanf("%I64d",&n);
  p=power(2,n)%m;
  c=(p*(p+1)/2)%m;

  printf("%I64d",c);
  return 0;
  }


#include <stdio.h>
const long long MOD = 1e9 + 7;
long long  binpow(long long n)
{
	long long b;
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return (binpow(n - 1) * 2) % MOD;
	else {
		b = binpow(n / 2) % MOD;
		return (b * b) % MOD;
	}
}
int main(void)
{
	long long n, ans, t;
	scanf("%I64d", &n);
	t = binpow(n);
	ans = (t * (t + 1) / 2) % MOD;
 	printf("%I64d\n", ans);
}


#include <stdio.h>
const long long MOD = 1e9 + 7;
unsigned long long  binpow(unsigned long long n)
{
	unsigned long long b;
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return binpow(n - 1) * 2;
	else {
		b = binpow(n / 2) % MOD;
		return (b * b) % MOD;
	}
}
int main(void)
{
	unsigned long long n, ans, t;
	scanf("%I64d", &n);
	t = binpow(n);
	ans = (t * (t + 1) / 2) % MOD;
 	printf("%I64d\n", ans);
}


#include <stdio.h>
const long long MOD = 1e9 + 7;
unsigned long long  binpow(unsigned long long n)
{
	unsigned long long b;
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return binpow(n - 1) * 2;
	else {
		b = binpow(n / 2) % MOD;
		return (b * b) % MOD;
	}
}
int main(void)
{
	unsigned long long n, ans, t;
	scanf("%I64d", &n);
	t = binpow(n);
	ans = (t * (t + 1) / 2) % MOD;
 	printf("%llu\n", ans);
}


#include <stdio.h>
const long long MOD = 1e9 + 7;
long long binpow(long long n)
{
    long long b;
    if (n == 0)
        return 1;
    if (n % 2 == 1)
        return (binpow(n - 1) * 2) % MOD;
    else {
        b = binpow(n / 2) % MOD;
        return (b * b) % MOD;
    }
}
int main(void)
{
    long long n, ans, t;
    scanf("%I64d", &n);
    t = binpow(n);
    ans = (t * (t + 1) / 2) % MOD;
    printf("%I64d\n", ans);
    return 0;
}


#include<stdio.h>
long long int m=1000000007;
long long int power(long long int x,long long int y)
{
    long long int e;
    if(y==1)
    return(x%m);
    e=power(x,y/2);
    if(y%2==1)
    return((x*e*e)%m);
    else
    return((e*e)%m);
}
main()
{
    long long int n,e,f;
    scanf("%I64d",&n);
    if(n==1)
    printf("%d",3);
    else if(n==0)
    printf("%d",1);
    else
    {
    e=power(2,n-1);
    f=((2*e)+1)%m;
    printf("%I64d",(e*f)%m);
    }
    return(0);
}


#include<stdio.h>
long long int m=1000000007;
long long int power(long long int x,long long int y)
{
    long long int e;
    if(y==1 || y==0)
    return(x%m);
    e=power(x,y/2);
    if(y%2==1)
    return((x*e*e)%m);
    else
    return((e*e)%m);
}
main()
{
    long long int n,e,f;
    scanf("%I64d",&n);
    e=power(2,n-1);
    f=((2*e)+1)%m;
    printf("%I64d",(e*f)%m);
    return(0);
}


#include<stdio.h>
long long int m=1000000007;
long long int power(long long int x,long long int y)
{
    long long int e;
    if(y==1)
    return(x%m);
    e=power(x,y/2);
    if(y%2==1)
    return((x*e*e)%m);
    else
    return((e*e)%m);
}
main()
{
    long long int n,e;
    scanf("%I64d",&n);
    e=power(2,n);
    printf("%I64d",((e+1)*(e/2))%m);
    return(0);
}


#include<stdio.h>
long long int m=1000000007;
long long int power(long long int x,long long int y)
{
    long long int e;
    if(y==1)
    return(x%m);
    e=power(x,y/2);
    if(y%2==1)
    return((x*e*e)%m);
    else
    return((e*e)%m);
}
main()
{
    long long int n,e,f;
    scanf("%I64d",&n);
    if(n==1)
    printf("%d",3);
    else
    {
    e=power(2,n-1);
    f=((2*e)+1)%m;
    printf("%I64d",(e*f)%m);
    }
    return(0);
}


#include<stdio.h>
#define MOD 1000000007
long long int pow(long long int a,long long int b)
{
    long long int x=1,y=a;
    while(b > 0LL)
    {
        if(b%2 == 1)
        {
            x=(x*y);
            if(x>MOD) x%=MOD;
        }
        y = (y*y);
        if(y>MOD) y%=MOD;
        b /= 2;
    }
    return x;
}
int main()
{
    long long int n;
    scanf("%lld",&n);
    if(n==0)
        printf("1");
    else
    {
        long long ans=(pow(4,n)-(((pow(2,n)*(pow(2,n)-1))/2)%MOD));
        if(ans>=0)
            printf("%lld",ans);
        else
            printf("%lld",MOD-ans);
    }
    return 0;
}


#include<stdio.h>
#define MOD 1000000007
long long int pow(long long int a,long long int b)
{
    long long int x=1,y=a;
    while(b > 0LL)
    {
        if(b%2 == 1)
        {
            x=(x*y);
            if(x>MOD) x%=MOD;
        }
        y = (y*y);
        if(y>MOD) y%=MOD;
        b /= 2;
    }
    return x;
}
int main()
{
    long long int n;
    scanf("%lld",&n);
    if(n==0)
        printf("1");
    else if(n==1)
        printf("3");
    else
    {
        long long ans=(10*pow(4,n-2))%MOD;
        printf("%lld",ans);
    }
    return 0;
}


#include<stdio.h>
#define MOD 1000000007
long long int pow(long long int a,long long int b)
{
    long long int x=1,y=a;
    while(b > 0LL)
    {
        if(b%2 == 1)
        {
            x=(x*y);
            if(x>MOD) x%=MOD;
        }
        y = (y*y);
        if(y>MOD) y%=MOD;
        b /= 2;
    }
    return x;
}
int main()
{
    long long int n;
    scanf("%lld",&n);
    if(n==0)
        printf("1");
    else
    {
        long long ans=(pow(4,n)-(pow(2,n-1)*(pow(2,n)-1))%MOD);
        if(ans>=0)
            printf("%lld",ans);
        else
            printf("%lld",MOD+ans);
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#define getchar getchar//_unlocked
#define M 1000000007

int getint ()
{
        int  n=0;
        char ch=getchar();

        while(ch<'0' || ch>'9')
            ch=getchar();

        while(ch>='0' && ch<='9')
        {
                n=n*10+ch-'0';
                ch=getchar();
        }

        return n;

}

unsigned long long gcd(unsigned long long a, unsigned long long b)
{

        unsigned long long div=a,did=b,rem;

        while((rem=did%div)!=0)
        {
                did=div;
                div=rem;

        }

        return div;

}

long long getlonglong ()
{
        long long  n=0;
        char ch=getchar();

        while(ch<'0' || ch>'9')
            ch=getchar();

        while(ch>='0' && ch<='9')
        {
                n=n*10+ch-'0';
                ch=getchar();
        }

        return n;

}

void sort(int a[] ,int start1 ,int end1 ,int start2 ,int end2)
{

        int temp[end1-start1 +1 + end2-start2+1 ];
        int i=start1,j=start2,count=0;

        while(i<=end1 && j<=end2)
        {
                if(a[i]>=a[j])
                    temp[count++]=a[i++];
                else
                    temp[count++]=a[j++];

        }

        for(;i<=end1;++i)
            temp[count++]=a[i];

        for(;j<=end2;++j)
            temp[count++]=a[j];

        for(i=0;i<count;++i)
            a[i+start1]=temp[i];

}

void mergesort(int a[] ,int start,int end)
{
        if(start==end)
            return;

        int middle =(start+end)/2;

        mergesort(a,start,middle);
        mergesort(a,middle+1,end);
        sort(a,start,middle,middle+1,end);

}

long long Ceil(long long a , long long n)
{
        if(a<0)
            return 0;

        if(a%n==0)
            return a/n;
        else
            return a/n +1 ;

}

int main()
{

        long long  two =2,n,x=1,m;

        n=getlonglong();

        m=n-1;

        while(m)
        {
                if(m%2==0)
                {
                    two= (two *two)%M ;
                    m=m/2;
                }
                else
                {
                        x=(x*two)%M;
                        m--;
                }

        }

        x=((x)*(1+2*x))%M;
        printf("%I64d",x);
        return 0;
}


#include<stdio.h>
#include<math.h>
#define getchar getchar//_unlocked
#define M 1000000007

int getint ()
{
        int  n=0;
        char ch=getchar();

        while(ch<'0' || ch>'9')
            ch=getchar();

        while(ch>='0' && ch<='9')
        {
                n=n*10+ch-'0';
                ch=getchar();
        }

        return n;

}

unsigned long long gcd(unsigned long long a, unsigned long long b)
{

        unsigned long long div=a,did=b,rem;

        while((rem=did%div)!=0)
        {
                did=div;
                div=rem;

        }

        return div;

}

long long getlonglong ()
{
        long long  n=0;
        char ch=getchar();

        while(ch<'0' || ch>'9')
            ch=getchar();

        while(ch>='0' && ch<='9')
        {
                n=n*10+ch-'0';
                ch=getchar();
        }

        return n;

}

void sort(int a[] ,int start1 ,int end1 ,int start2 ,int end2)
{

        int temp[end1-start1 +1 + end2-start2+1 ];
        int i=start1,j=start2,count=0;

        while(i<=end1 && j<=end2)
        {
                if(a[i]>=a[j])
                    temp[count++]=a[i++];
                else
                    temp[count++]=a[j++];

        }

        for(;i<=end1;++i)
            temp[count++]=a[i];

        for(;j<=end2;++j)
            temp[count++]=a[j];

        for(i=0;i<count;++i)
            a[i+start1]=temp[i];

}

void mergesort(int a[] ,int start,int end)
{
        if(start==end)
            return;

        int middle =(start+end)/2;

        mergesort(a,start,middle);
        mergesort(a,middle+1,end);
        sort(a,start,middle,middle+1,end);

}

long long Ceil(long long a , long long n)
{
        if(a<0)
            return 0;

        if(a%n==0)
            return a/n;
        else
            return a/n +1 ;

}

int main()
{

        long long  two =2,n,x=1,m;

        m=getlonglong();

        n=m;

        if(m!=0)
            m=m-1;

        while(m)
        {
                if(m%2==0)
                {
                    two=(two*two)%M ;
                    m=m/2;
                }
                else
                {
                        x=(x*two)%M;
                        m--;
                }

        }

        if(n!=0)
            x=x*(1+2*x)%M;
        else
            x=1;

        printf("%I64d",x);
        return 0;
}


#include<stdio.h>
long long int n,x;
long long int f(long long int a)
{
    if(a<2)
        return a+1;
    x=f(a/2);
    x *= x;
    if(a & 1) x *= 2;
    x %= 1000000007;
    return x;
}
int main()
{
    scanf("%lld",&n);
    long long int a=f(n);
    printf("%lld",(a*(a+1)/2)%1000000007);
    return 0;
}


#include<stdio.h>
#include<math.h>

long int recu (long long int n,long long int m);
long int recu (long long int n,long long int m)
	{
	if(n==1)
		{
		long int t=2%m;
		//printf("t=%ld\n",t);
		return (t);
		}
	else
		{
		if(n%2==0)
			{
			return ((long long int)pow(recu(n/2,m),2)%m);
			}
		else
			{
			return (((2%m)*recu(n-1,m)%m)%m);
			}
		}
	}
int main()
	{
	long long int n;
	scanf("%I64d",&n);long long int m=1000000007;
	long int ans;
	if(n==0)
	{ans=1;goto end;}
	else if(n==1)
	{ans=3;goto end;}
	else
	ans = recu(n-1,m);
	ans = (ans*((2*ans)%m+1))%m;
	end:
	printf("%ld",ans);
	return 0;
	}


#include<stdio.h>

int main()
{
  int countun,countdn,i,j,k,n,countu=1,countd=0;
  scanf("%d", &n);
  for(i=0;i<n;i++)
  {
    countun=(countu*3+countd)%1000000007;
    countdn=(countd*3+countu)%1000000007;
    countu=countun;
    countd=countdn;
  }
  printf("%d\n", countu);
}


#include<stdio.h>

long long int power(long long int a, long long int b)
{
      long long int sum=1;
      while(b!=0)
                {
                          sum=sum%1000000007;
                                  if(b&1==1)
                                                sum*=a;
                                          a=(a*a)%1000000007;
                                                  b=b/2;
                                                      }
              return sum;
}

int main()
{
      long long int a,b,n,m,i,j,k;
          scanf("%lld", &n);
              a=power(2,n);
                  b=power(2,(2*n));
                      k=((a+b)%1000000007)/2;
                          printf("%lld\n", k);
                              return 0;
}


#include<stdio.h>

int main()
{
  long long int countun,countdn,i,j,k,n,countu=1,countd=0;
  scanf("%lld", &n);
  n=2*n;
  printf("%lld\n", n*(n+1)/2);
  return 0;
}


#include<stdio.h>

long long int power(long long int a, long long int b)
{
      long long int sum=1;
      while(b!=0)
      {
       if(b&1==1)
            sum=(sum*a)%1000000007;
       a=(a*a)%1000000007;
       b=b/2;
       }
       return sum;
}

int main()
{
      long long int a,b,n,m,i,j,k;
      scanf("%lld", &n);
      a=power(2,n);

      //printf("%lld %lld\n", a, b);
      k=((a+a*a)/2)%1000000007;
      printf("%lld\n", k);
      return 0;
}


#include<stdio.h>

int main()
{
  long long int countun,countdn,i,j,k,n,countu=1,countd=0;
  scanf("%lld", &n);
  for(i=0;i<n;i++)
  {
    countun=(countu*3+countd)%1000000007;
    countdn=(countd*3+countu)%1000000007;
    countu=countun;
    countd=countdn;
  }
  printf("%lld\n", countu);
  return 0;
}


#include<stdio.h>

int main()
{
  unsigned long long int countun,countdn,i,j,k,n,countu=1,countd=0;
  scanf("%llu", &n);
  for(i=0;i<n;i++)
  {
    countun=(countu*3+countd)%1000000007;
    countdn=(countd*3+countu)%1000000007;
    countu=countun;
    countd=countdn;
  }
  printf("%llu\n", countu);
}


#include<stdio.h>

int main()
{
  long long int countun,countdn,i,j,k,n,countu=1,countd=0;
  scanf("%lld", &n);
  for(i=0;i<n;i++)
  {
    countun=(countu*3+countd)%1000000007;
    countdn=(countd*3+countu)%1000000007;
    countu=countun;
    countd=countdn;
  }
  printf("%lld\n", countu);
}


#include<stdio.h>

int main()
{
  long long int countun,countdn,i,j,k,n,countu=1,countd=0;
  scanf("%lld", &n);
  n=2*n%1000000007;
  printf("%lld\n", (n*(n+1)/2)%100000007);
  return 0;
}


#include<stdio.h>
typedef __int64 lld;
lld mod(lld a, lld b, lld m)
{
    lld ret=1;
    a%=m;
    while(b)
    {
        if(b&1)ret=ret*a%m;
        b>>=1;
        a=a*a%m;
        //printf("b=%I64d\n",b);
    }
    return ret;
}

int main()
{
    lld n,a,b,c,d,e,f;
    lld m = 1000000007;
    while(scanf("%I64d",&n)!=EOF)
    {
        if(n==0) {printf("1\n");continue;}
        a = 2;
        b = 2*n-1;
        c = 1;
        b=mod(b,c,m-1);
        a=mod(a,b,m);
        d = 2;
        e = n-1;
        f = 1;
        e = mod(e,f,m-1);
        d = mod(d,e,m);
        printf("%I64d\n",(a+d)%m);
    }
    return 0;
}


#include<stdio.h>
typedef __int64 lld;
lld mod(lld a, lld b, lld m)
{
    lld ret=1;
    a%=m;
    while(b)
    {
        if(b&1)ret=ret*a%m;
        b>>=1;
        a=a*a%m;
        //printf("b=%I64d\n",b);
    }
    return ret;
}

int main()
{
    lld n,a,b,c,d,e,f;
    lld m = 1000000007;
    while(scanf("%I64d",&n)!=EOF)
    {
        a = 2;
        b = 2*n-1;
        c = 1;
        b=mod(b,c,m-1);
        a=mod(a,b,m);
        d = 2;
        e = n-1;
        f = 1;
        e = mod(e,f,m-1);
        d = mod(d,e,m);
        printf("%I64d\n",(a+d)%m);
    }
    return 0;
}


#include<stdio.h>
typedef __int64 lld;
lld mod(lld a, lld b, lld m)
{
    lld ret=1;
    a%=m;
    while(b)
    {
        if(b&1)ret=ret*a%m;
        b>>=1;
        a=a*a%m;
        //printf("b=%I64d\n",b);
    }
    return ret;
}

int main()
{
    lld n,a,b,c,d,e,f;
    lld m = 1000000007;
    while(scanf("%I64d",&n)!=EOF)
    {
        a = 2;
        b = 2*n-1;
        c = 1;
        b=mod(b,c,m-1);
        a=mod(a,b,m);
        d = 2;
        e = n-1;
        f = 1;
        e = mod(e,f,m-1);
        d = mod(d,e,m);
        printf("%I64d\n",a+d);
    }
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MOD 10000000007
typedef unsigned long long int ull;
ull calc(ull i)
{    ull a ;
    if(i==0)return(1);
    else
    {
    a = calc(i/2);
    if(i%2)
    return((a*a*2)%MOD);
    else return((a*a)%MOD);
    }
}
int main()
{
     ull n, a3;
    scanf("%I64u", &n);
    a3 = calc(n);
    a3 = a3*(a3+1)/2;
    printf("%I64u", a3);
    return (0);
    }