#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long f = 1, sum = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * f;
}
const int MAXN = 1000010;
long long quickpow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans *= a, ans %= 998244353;
    b /= 2, a *= a, a %= 998244353;
  }
  return ans;
}
long long fac[MAXN], inv[MAXN];
long long comb(long long n, long long m) {
  long long ans = fac[n];
  ans *= inv[m], ans %= 998244353;
  ans *= inv[n - m], ans %= 998244353;
  return ans;
}
int main() {
  int n;
  scanf("%d", &n);
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % 998244353;
  inv[n] = quickpow(fac[n], 998244353 - 2);
  for (int i = n - 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % 998244353;
  long long ans = quickpow(3, (long long)n * n);
  ans -= quickpow((quickpow(3, n) - 3), n);
  if (ans < 998244353) ans += 998244353;
  for (int i = 1; i <= n; i++) {
    long long tmp = 1ll * 3 * quickpow((quickpow(3, n - i) - 1), n) % 998244353;
    tmp += (quickpow(3, i) - 3) * quickpow(3, (long long)(n - i) * n);
    tmp %= 998244353;
    tmp *= comb(n, i);
    tmp %= 998244353;
    if (i & 1)
      ans += tmp, ans %= 998244353;
    else
      ans = (ans - tmp + 998244353) % 998244353;
  }
  cout << ans;
  return 0;
}
