#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
const int p = 998244353;
long long n;
long long mul[N], inv[N], po[N];
void read(long long &x) {
  char ch = getchar();
  x = 0;
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 3) + (x << 1) + ch - '0';
}
long long qpow(long long a, long long n) {
  long long ans = 1;
  for (long long sum = a; n; n >>= 1, sum = sum * sum % p)
    if (n & 1) ans = ans * sum % p;
  return ans;
}
void init() {
  mul[0] = 1;
  for (int i = 1; i < N; i++) mul[i] = mul[i - 1] * i % p;
  inv[N - 1] = qpow(mul[N - 1], p - 2);
  for (int i = N - 2; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % p;
  po[0] = 1;
  for (int i = 1; i < N; i++) po[i] = po[i - 1] * 3 % p;
}
inline long long C(int n, int m) {
  return mul[n] * inv[m] % p * inv[n - m] % p;
}
void Add(long long &x, long long y) {
  x += y;
  while (x < 0) x += p;
  while (x >= 0) x -= p;
}
int main() {
  init();
  read(n);
  long long ans = 0, v = -1;
  for (int i = 1; i <= n; i++) {
    v = -v;
    Add(ans, v * C(n, i) * qpow(3, (n - i) * n + i) % p);
  }
  v = -1;
  for (int i = 1; i <= n; i++) {
    v = -v;
    Add(ans, v * C(n, i) * qpow(3, (n - i) * n + i) % p);
  }
  v = 1;
  for (int i = 1; i <= n; i++) {
    v *= -1;
    Add(ans,
        v * C(n, i) * 3 * (qpow(3, n * (n - i)) - qpow(po[n - i] - 1, n)) % p);
  }
  cout << (ans % p + p) % p << endl;
  return 0;
}
